-
Notifications
You must be signed in to change notification settings - Fork 9
/
logger.go
137 lines (110 loc) · 2.81 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
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
137
package zerodriver
import (
"io"
"os"
"time"
"github.com/rs/zerolog"
)
type Logger struct {
*zerolog.Logger
}
type Event struct {
*zerolog.Event
}
// See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
var logLevelSeverity = map[zerolog.Level]string{
zerolog.DebugLevel: "DEBUG",
zerolog.InfoLevel: "INFO",
zerolog.WarnLevel: "WARNING",
zerolog.ErrorLevel: "ERROR",
zerolog.PanicLevel: "CRITICAL",
zerolog.FatalLevel: "CRITICAL",
}
// NewProductionLogger returns a configured logger for production.
// It outputs info level and above logs with sampling.
func NewProductionLogger() *Logger {
logLevel := zerolog.InfoLevel
zerolog.SetGlobalLevel(logLevel)
zerolog.LevelFieldName = "severity"
zerolog.LevelFieldMarshalFunc = func(l zerolog.Level) string {
return logLevelSeverity[l]
}
zerolog.TimestampFieldName = "time"
zerolog.TimeFieldFormat = time.RFC3339Nano
// default sampler
sampler := &zerolog.BasicSampler{N: 1}
logger := zerolog.New(os.Stderr).Sample(sampler).With().Timestamp().Logger()
return &Logger{&logger}
}
// NewDevelopmentLogger returns a configured logger for development.
// It outputs debug level and above logs, and sampling is disabled.
func NewDevelopmentLogger() *Logger {
logLevel := zerolog.DebugLevel
zerolog.SetGlobalLevel(logLevel)
zerolog.LevelFieldName = "severity"
zerolog.LevelFieldMarshalFunc = func(l zerolog.Level) string {
return logLevelSeverity[l]
}
zerolog.TimestampFieldName = "time"
zerolog.TimeFieldFormat = time.RFC3339Nano
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
return &Logger{&logger}
}
// To use method chain we need followings
func (l *Logger) Trace() *Event {
e := l.Logger.Trace()
return &Event{e}
}
func (l *Logger) Debug() *Event {
e := l.Logger.Debug()
return &Event{e}
}
func (l *Logger) Info() *Event {
e := l.Logger.Info()
return &Event{e}
}
func (l *Logger) Warn() *Event {
e := l.Logger.Warn()
return &Event{e}
}
func (l *Logger) Error() *Event {
e := l.Logger.Error()
return &Event{e}
}
func (l *Logger) Err(err error) *Event {
e := l.Logger.Err(err)
return &Event{e}
}
func (l *Logger) Fatal() *Event {
e := l.Logger.Fatal()
return &Event{e}
}
func (l *Logger) Panic() *Event {
e := l.Logger.Panic()
return &Event{e}
}
func (l *Logger) WithLevel(level zerolog.Level) *Event {
e := l.Logger.WithLevel(level)
return &Event{e}
}
func (l *Logger) Log() *Event {
e := l.Logger.Log()
return &Event{e}
}
func (l *Logger) Print(v ...interface{}) {
l.Logger.Print(v...)
}
func (l *Logger) Printf(format string, v ...interface{}) {
l.Logger.Printf(format, v...)
}
func (l Logger) Write(p []byte) (n int, err error) {
n, err = l.Logger.Write(p)
return n, err
}
func (l Logger) Output(w io.Writer) Logger {
logger := l.Logger.Output(w)
l2 := Logger{
Logger: &logger,
}
return l2
}