-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
adaptor.go
41 lines (35 loc) · 988 Bytes
/
adaptor.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
package lgr
import (
"log"
"strings"
)
// Writer holds lgr.L and wraps with io.Writer interface
type Writer struct {
L
level string // if defined added to each message
}
// Write to lgr.L
func (w *Writer) Write(p []byte) (n int, err error) {
w.Logf(w.level + string(p))
return len(p), nil
}
// ToWriter makes io.Writer for given lgr.L with optional level
func ToWriter(l L, level string) *Writer {
if level != "" && !strings.HasSuffix(level, " ") {
level += " "
}
return &Writer{l, level}
}
// ToStdLogger makes standard logger
func ToStdLogger(l L, level string) *log.Logger {
return log.New(ToWriter(l, level), "", 0)
}
// SetupStdLogger makes the default std logger with lgr.L
func SetupStdLogger(opts ...Option) {
logOpts := append([]Option{CallerDepth(3)}, opts...) // skip 3 more frames to compensate stdlog calls
l := New(logOpts...)
l.reTrace = reTraceStd // std logger split on log/ path
log.SetOutput(ToWriter(l, ""))
log.SetPrefix("")
log.SetFlags(0)
}