Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve custom logging #622

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var lookupPaths = []string{"", "./config", "/config", "../", "../config", "../..
var ConfigName = "database.yml"

func init() {
SetStandardLogger(defaultStdLogger)
SetLogger(defaultLogger)

ap := os.Getenv("APP_PATH")
Expand Down
2 changes: 1 addition & 1 deletion connection_instrumented_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func testInstrumentedDriver(p *suite.Suite) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
defer cancel()

// The WaitGroup and channel ensures that the logger is properly called. This can only happen
// The WaitGroup and channel ensures that the Logger is properly called. This can only happen
// when the instrumented driver is working as expected and returns the expected query.
var (
queryMySQL = "SELECT 1 FROM DUAL WHERE 1=?"
Expand Down
21 changes: 14 additions & 7 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"github.com/gobuffalo/pop/v5/logging"
)

type logger func(lvl logging.Level, s string, args ...interface{})
type Logger func(lvl logging.Level, s string, args ...interface{})

// Debug mode, to toggle verbose log traces
var Debug = false

// Color mode, to toggle colored logs
var Color = true

var log logger
var log Logger
var standardLogger *stdlog.Logger
sio4 marked this conversation as resolved.
Show resolved Hide resolved

var defaultStdLogger = stdlog.New(os.Stdout, "[POP] ", stdlog.LstdFlags)
var defaultLogger = func(lvl logging.Level, s string, args ...interface{}) {
Expand Down Expand Up @@ -46,13 +47,19 @@ var defaultLogger = func(lvl logging.Level, s string, args ...interface{}) {
if Color {
s = color.YellowString(s)
}
defaultStdLogger.Println(s)
if standardLogger != nil {
standardLogger.Println(s)
}
sio4 marked this conversation as resolved.
Show resolved Hide resolved
}

// SetLogger overrides the default logger.
// SetLogger overrides the default Logger.
//
// The logger must implement the following interface:
// type logger func(lvl logging.Level, s string, args ...interface{})
func SetLogger(l logger) {
// The Logger must implement the following interface:
// type Logger func(lvl logging.Level, s string, args ...interface{})
func SetLogger(l Logger) {
log = l
}

func SetStandardLogger(logger *stdlog.Logger) {
standardLogger = logger
}