-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
logger.go
97 lines (80 loc) · 2.04 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package pop
import (
"fmt"
stdlog "log"
"os"
"github.com/fatih/color"
"github.com/gobuffalo/pop/v6/logging"
)
// Debug mode, to toggle verbose log traces
var Debug = false
// Color mode, to toggle colored logs
var Color = true
// SetLogger overrides the default logger.
func SetLogger(logger func(level logging.Level, s string, args ...interface{})) {
log = logger
}
// SetLogger overrides the default logger.
func SetTxLogger(logger func(level logging.Level, anon interface{}, s string, args ...interface{})) {
txlog = logger
}
var defaultStdLogger = stdlog.New(os.Stderr, "[POP] ", stdlog.LstdFlags)
var log = func(lvl logging.Level, s string, args ...interface{}) {
txlog(lvl, nil, s, args...)
}
var txlog = func(lvl logging.Level, anon interface{}, 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)
}
connID := ""
txID := 0
extra := ""
switch typed := anon.(type) {
case *Connection:
connID = typed.ID
if typed.TX != nil {
txID = typed.TX.ID
}
extra = printStats(&typed.Store)
case *Tx:
txID = typed.ID
case store:
if t, ok := typed.(*Tx); ok {
txID = t.ID
}
extra = printStats(&typed)
}
s = fmt.Sprintf("%s (conn=%v, tx=%v%v)", s, connID, txID, extra)
} else {
s = fmt.Sprintf(s, args...)
s = fmt.Sprintf("%s - %s", lvl, s)
}
if Color {
s = color.YellowString(s)
}
defaultStdLogger.Println(s)
}
// printStats returns a string represent connection pool information from
// the given store.
func printStats(s *store) string {
if db, ok := (*s).(*dB); ok {
s := db.Stats()
return fmt.Sprintf(", maxconn: %d, openconn: %d, in-use: %d, idle: %d", s.MaxOpenConnections, s.OpenConnections, s.InUse, s.Idle)
}
return ""
}