Skip to content

Commit

Permalink
Redirect standard logs (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckenzie132 authored Apr 14, 2022
1 parent 78a63cb commit d2fc90a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Load config from Config file, and from env vars. Use viper for that
- Automatically alocates a random port, if the specified one is in-use

## [0.1.18] - 2022-04-13
## Changed
- Redirect standard output to configured logger

## [0.1.17] - 2022-04-12
## Changed
- Log request and response at INFO level
Expand Down
32 changes: 32 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
package logger

import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"

"github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -112,5 +115,34 @@ type ProxyLogger struct {
// Printf satisfies `goproxy` logging interface. Default logging level will be
// `Debug`.
func (pL *ProxyLogger) Printf(format string, v ...interface{}) {
format = strings.TrimSpace(format)
pL.Logger.Debuglnf(format, v...)
}

type StandardLogger struct {
Logger *sypl.Sypl
}

// Write satisfies the standard logging interface. Default logging level will be
// `Debug`.
func (pL *StandardLogger) Write(p []byte) (int, error) {
p = bytes.TrimSpace(p)
pL.Logger.Debug(string(p))
return len(p), nil
}

// RedirectStandardLogs redirects logs created with the standard library global logger
// to the ProxyLogger.
func RedirectStandardLogs() {
log.SetFlags(0)
log.SetPrefix("")
standardLogger := &StandardLogger{
Logger: Get().New("go"),
}
log.SetOutput(standardLogger)
}

// DisableStandardLogs disables logs created with the standard library global logger
func DisableStandardLogs() {
log.SetOutput(ioutil.Discard)
}
37 changes: 37 additions & 0 deletions internal/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
package logger

import (
"log"
"strings"
"testing"

"github.com/saucelabs/sypl"
"github.com/saucelabs/sypl/level"
"github.com/saucelabs/sypl/output"
"github.com/saucelabs/sypl/processor"
"github.com/saucelabs/sypl/shared"
)

func TestSetup(t *testing.T) {
Expand Down Expand Up @@ -58,3 +66,32 @@ func TestSetup(t *testing.T) {
})
}
}


func TestRedirectStandardLogs(t *testing.T) {
// set global proxy logger
proxyLogger = sypl.NewDefault("test", level.Debug)
defer func(){proxyLogger = nil}()

// proxyLogger sends output to a buffer
buffer, outputBuffer := output.SafeBuffer(level.Trace, processor.PrefixBasedOnMask(shared.DefaultTimestampFormat))
proxyLogger.AddOutputs(outputBuffer)

// test standard logger before and after redirect
beforeMsg := "Before redirect"
afterMsg := "After redirect"
log.Println(beforeMsg)

RedirectStandardLogs()
log.Println(afterMsg)

bufferStr := buffer.String()

if strings.Contains(bufferStr, beforeMsg) {
t.Errorf("%s should not appear in proxy logger: %s", beforeMsg, bufferStr)
}
if !strings.Contains(bufferStr, afterMsg) {
t.Errorf("%s should appear in proxy logger: %s", afterMsg, bufferStr)
}

}
5 changes: 5 additions & 0 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,11 @@ func New(

proxy.Logger = proxyLogger
proxy.Verbose = true

// Ensure any standard log messages use a configured logger
logger.RedirectStandardLogs()
} else {
logger.DisableStandardLogs()
}

proxy.KeepDestinationHeaders = true
Expand Down

0 comments on commit d2fc90a

Please sign in to comment.