-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
230 lines (189 loc) · 5.76 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Package log provides a global logger for zerolog.
package log
import (
"context"
"fmt"
"io"
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"
)
// Current Logger is the global logger.
var Clogger = zerolog.New(os.Stderr).With().Timestamp().Logger()
type Logger struct {
log zerolog.Logger
}
// func New(log zerolog.Logger) Logger {
// return Logger{log: log}
// }
//InitLog initializes global logging settings
// level - debug, info, warn, error, fatal, panic, none
func SetupLogging(level string, isConsole, isCaller bool) {
//set log level
if level == "none" || level == "disabled" {
zerolog.SetGlobalLevel(zerolog.Disabled)
} else {
lvl, err := zerolog.ParseLevel(level)
if err != nil {
fmt.Printf("invalid log level: %s\n", err)
panic("log initialization failed")
}
zerolog.SetGlobalLevel(lvl)
}
//set log format
zerolog.TimeFieldFormat = time.RFC3339
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
zerolog.DurationFieldUnit = time.Millisecond
zerolog.DurationFieldInteger = true
//create logger
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
if isConsole {
logger = logger.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
}
if isCaller {
logger = logger.With().Caller().Logger()
}
Clogger = logger
}
// Service logs using info - source, msg, error, params
func Service(ctx context.Context, source, msg string, err error, params map[string]interface{}) {
if params == nil {
params = make(map[string]interface{})
}
params["svc"] = source
if source == "account" {
params["svc"] = "user"
}
if val, ok := params["took"].(time.Duration); ok {
params["duration"] = val
params["latency"] = val.String()
//remove took
delete(params, "took")
}
// if user, ok := ctx.Value("_authuser").(*models.AuthUser); ok {
// params["id"] = user.ID
// params["username"] = user.Username
// params["tenant_id"] = user.TenantID
// }
if err != nil {
params["error"] = err
Error().Fields(params).Msg(msg)
return
}
Info().Fields(params).Msg(msg)
}
// Output duplicates the global logger and sets w as its output.
func Output(w io.Writer) zerolog.Logger {
return Clogger.Output(w)
}
// With creates a child logger with the field added to its context.
func With() zerolog.Context {
return Clogger.With()
}
// Level creates a child logger with the minimum accepted level set to level.
func Level(level zerolog.Level) zerolog.Logger {
return Clogger.Level(level)
}
// Sample returns a logger with the s sampler.
func Sample(s zerolog.Sampler) zerolog.Logger {
return Clogger.Sample(s)
}
// Hook returns a logger with the h Hook.
func Hook(h zerolog.Hook) zerolog.Logger {
return Clogger.Hook(h)
}
// Err starts a new message with error level with err as a field if not nil or
// with info level if err is nil.
//
// You must call Msg on the returned event in order to send the event.
func Err(err error) *zerolog.Event {
return Clogger.Err(err)
}
// Trace starts a new message with trace level.
//
// You must call Msg on the returned event in order to send the event.
func Trace() *zerolog.Event {
return Clogger.Trace()
}
// Debug starts a new message with debug level.
//
// You must call Msg on the returned event in order to send the event.
func Debug() *zerolog.Event {
return Clogger.Debug()
}
// Info starts a new message with info level.
//
// You must call Msg on the returned event in order to send the event.
func Info() *zerolog.Event {
return Clogger.Info()
}
// Warn starts a new message with warn level.
//
// You must call Msg on the returned event in order to send the event.
func Warn() *zerolog.Event {
return Clogger.Warn()
}
// Error starts a new message with error level.
//
// You must call Msg on the returned event in order to send the event.
func Error() *zerolog.Event {
return Clogger.Error()
}
// Fatal starts a new message with fatal level. The os.Exit(1) function
// is called by the Msg method.
//
// You must call Msg on the returned event in order to send the event.
func Fatal() *zerolog.Event {
return Clogger.Fatal()
}
// Panic starts a new message with panic level. The message is also sent
// to the panic function.
//
// You must call Msg on the returned event in order to send the event.
func Panic() *zerolog.Event {
return Clogger.Panic()
}
// WithLevel starts a new message with level.
//
// You must call Msg on the returned event in order to send the event.
func WithLevel(level zerolog.Level) *zerolog.Event {
return Clogger.WithLevel(level)
}
// Log starts a new message with no level. Setting zerolog.GlobalLevel to
// zerolog.Disabled will still disable events produced by this method.
//
// You must call Msg on the returned event in order to send the event.
func Log() *zerolog.Event {
return Clogger.Log()
}
// Print sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
Clogger.Print(v...)
}
// Printf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
Clogger.Printf(format, v...)
}
// Ctx returns the Logger associated with the ctx. If no logger
// is associated, a disabled logger is returned.
func Ctx(ctx context.Context) *zerolog.Logger {
return zerolog.Ctx(ctx)
}
// UpdateContext updates the internal logger's context.
//
// Use this method with caution. If unsure, prefer the With method.
func UpdateContext(update func(c zerolog.Context) zerolog.Context) {
Clogger.UpdateContext(update)
}
// // Level creates a child logger with the minimum accepted level set to level.
// func Level(lvl Level) zerolog.Logger {
// Clogger.level = lvl
// return l
// }
// // GetLevel returns the current Level of l.
// func GetLevel() Level {
// return Clogger.level
// }