forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
58 lines (50 loc) · 1.21 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package pop
import (
"fmt"
stdlog "log"
"os"
"github.com/fatih/color"
"github.com/gobuffalo/pop/v5/logging"
)
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 defaultStdLogger = stdlog.New(os.Stdout, "[POP] ", stdlog.LstdFlags)
var defaultLogger = func(lvl logging.Level, s string, args ...interface{}) {
if !Debug && lvl <= logging.Debug {
return
}
if lvl == logging.SQL {
if len(args) > 0 {
xargs := make([]string, len(args))
for i, a := range args {
switch a.(type) {
case string:
xargs[i] = fmt.Sprintf("%q", a)
default:
xargs[i] = fmt.Sprintf("%v", a)
}
}
s = fmt.Sprintf("%s - %s | %s", lvl, s, xargs)
} else {
s = fmt.Sprintf("%s - %s", lvl, s)
}
} else {
s = fmt.Sprintf(s, args...)
s = fmt.Sprintf("%s - %s", lvl, s)
}
if Color {
s = color.YellowString(s)
}
defaultStdLogger.Println(s)
}
// 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) {
log = l
}