-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlog.go
136 lines (113 loc) · 2.77 KB
/
log.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package log
import (
"fmt"
"io"
"os"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/theplant/appkit/kerrs"
)
type Logger struct {
log.Logger
}
func (l Logger) With(keysvals ...interface{}) Logger {
l.Logger = log.With(l.Logger, keysvals...)
return l
}
/*
WithError can log kerrs type of err to structured log
*/
func (l Logger) WithError(err error) log.Logger {
keysvals, msg, stacktrace := kerrs.Extract(err)
keysvals = append(keysvals, "msg", msg)
if len(stacktrace) > 0 {
keysvals = append(keysvals, "stacktrace", stacktrace)
}
l.Logger = level.Error(log.With(l.Logger, keysvals...))
return l
}
func (l Logger) Debug() log.Logger {
l.Logger = level.Debug(l.Logger)
return l
}
func (l Logger) Info() log.Logger {
l.Logger = level.Info(l.Logger)
return l
}
func (l Logger) Crit() log.Logger {
l.Logger = level.Error(l.Logger)
return l
}
func (l Logger) Error() log.Logger {
l.Logger = level.Error(l.Logger)
return l
}
func (l Logger) Warn() log.Logger {
l.Logger = level.Warn(l.Logger)
return l
}
func Default() Logger {
var timer log.Valuer = func() interface{} { return time.Now().Format(time.RFC3339Nano) }
l := log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
lg := Logger{
Logger: l,
}
lg = lg.With("ts", timer, "caller", log.DefaultCaller)
return lg
}
// NewNopLogger returns a logger that doesn't do anything. This just a wrapper of
// `go-kit/log.nopLogger`.
func NewNopLogger() Logger {
return Logger{Logger: log.NewNopLogger()}
}
type logWriter struct {
log.Logger
}
func (l logWriter) Write(p []byte) (int, error) {
err := l.Log("msg", string(p))
return len(p), err
}
func LogWriter(logger log.Logger) io.Writer {
return &logWriter{logger}
}
type GormLogger struct {
Logger
}
func (l GormLogger) Print(values ...interface{}) {
if len(values) > 1 {
level, source := values[0], values[1]
log := l.With("type", level, "source", source)
if level == "sql" {
dur, sql, values := values[2].(time.Duration), values[3].(string), values[4].([]interface{})
sqlLog(log, dur, sql, values)
return
} else if level == "log" {
logLog(log, values[2:])
}
} else {
l.Info().Log("msg", fmt.Sprintf("%+v", values))
}
}
func sqlLog(l Logger, dur time.Duration, query string, values []interface{}) {
logger := l.Debug()
if dur > 100*time.Millisecond {
logger = l.Warn()
} else if dur > 50*time.Millisecond {
logger = l.Info()
}
logger.Log("query_us", int64(dur/time.Microsecond), "query", query, "values", fmt.Sprintf("%+v", values))
}
func logLog(l Logger, values ...interface{}) {
msg := ""
if len(values) == 1 {
if err, ok := values[0].(error); ok {
l.Error().Log("msg", err)
} else {
msg = fmt.Sprintf("%+v", values[0])
}
} else {
msg = fmt.Sprintf("%+v", values)
}
l.Info().Log("msg", msg)
}