-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlog.go
77 lines (62 loc) · 2.38 KB
/
stdlog.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
package clog
import (
"fmt"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// StdLogger is a logger that implements most methods used by "log" stdlib users. Print-ed logs are
// recorded at Info level.
type StdLogger struct{ log *zap.Logger }
// NewStdLogger creats a new StdLogger attached to the given zap.Logger. If logger is nil,
// NewStdLogger returns nil as well.
func NewStdLogger(logger *zap.Logger) *StdLogger {
if logger == nil {
return nil
}
return &StdLogger{logger.WithOptions(zap.AddCallerSkip(1))}
}
// With returns a copy of this StdLogger with additional Zap fields.
func (l *StdLogger) With(fields ...zapcore.Field) *StdLogger {
return &StdLogger{l.log.With(fields...)}
}
// WithOptions returns a copy of this StdLogger with additional Zap options.
func (l *StdLogger) WithOptions(opts ...zap.Option) *StdLogger {
return &StdLogger{l.log.WithOptions(opts...)}
}
// Panic writes a panic-level fmt.Sprint-formatted log message.
func (l *StdLogger) Panic(args ...interface{}) {
l.log.Panic(fmt.Sprint(args...))
}
// Panicln writes a panic-level fmt.Sprintln-formatted log message.
func (l *StdLogger) Panicln(args ...interface{}) {
l.log.Panic(strings.TrimSuffix(fmt.Sprintln(args...), "\n"))
}
// Panicf writes a panic-level fmt.Sprintf-formatted log message.
func (l *StdLogger) Panicf(format string, args ...interface{}) {
l.log.Panic(fmt.Sprintf(format, args...))
}
// Fatal writes a fatal-level fmt.Sprint-formatted log message.
func (l *StdLogger) Fatal(args ...interface{}) {
l.log.Fatal(fmt.Sprint(args...))
}
// Fatalln writes a fatal-level fmt.Sprintln-formatted log message.
func (l *StdLogger) Fatalln(args ...interface{}) {
l.log.Fatal(strings.TrimSuffix(fmt.Sprintln(args...), "\n"))
}
// Fatalf writes a fatal-level fmt.Sprintf-formatted log message.
func (l *StdLogger) Fatalf(format string, args ...interface{}) {
l.log.Fatal(fmt.Sprintf(format, args...))
}
// Print writes an info-level fmt.Sprint-formatted log message.
func (l *StdLogger) Print(args ...interface{}) {
l.log.Info(fmt.Sprint(args...))
}
// Println writes an info-level fmt.Sprintln-formatted log message.
func (l *StdLogger) Println(args ...interface{}) {
l.log.Info(strings.TrimSuffix(fmt.Sprintln(args...), "\n"))
}
// Printf writes an info-level fmt.Sprintf-formatted log message.
func (l *StdLogger) Printf(format string, args ...interface{}) {
l.log.Info(fmt.Sprintf(format, args...))
}