-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
162 lines (135 loc) · 4.25 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
package dora
import (
"context"
"io"
"os"
"time"
"github.com/NoneBorder/dora/zlogwriter"
"github.com/rs/zerolog"
)
func init() {
zerolog.TimeFieldFormat = time.RFC3339
}
// Logger is the global logger.
var Logger = zerolog.New(os.Stderr).With().Timestamp().Logger()
// AutoWriter return logger which writer decider by adapterName
// @logLevel should be string as debug,info,warn,error,fatal,panic
func NewLogWithWriter(adapterName, writerConfig string, logLevel string, bufferSize ...int) zerolog.Logger {
bufferSize = append(bufferSize, 0)
logger := Logger
switch adapterName {
case "console":
logger = TextWriter(nil)
case "file":
logger = FileWriter(writerConfig, bufferSize[0])
}
l, _ := zerolog.ParseLevel(logLevel)
if l == zerolog.NoLevel {
l = zerolog.DebugLevel
}
return logger.Level(l)
}
// TextWriter set return logger to zerolog.ConsoleWriter
func TextWriter(w io.Writer, noColor ...bool) zerolog.Logger {
if w == nil {
w = os.Stderr
}
noColor = append(noColor, false)
return zerolog.New(zerolog.ConsoleWriter{Out: w, NoColor: noColor[0]}).With().Timestamp().Logger()
}
// FileWriter return logger to zlog/writer.FileWriter
func FileWriter(jsonConfig string, bufferSize ...int) zerolog.Logger {
bufferSize = append(bufferSize, 0)
w := zlogwriter.NewFileWriter()
if err := w.Init(jsonConfig); err != nil {
Logger.Fatal().Err(err).Msg("init zlogwriter.FileWriter failed")
return Logger
}
return zerolog.New(w).With().Timestamp().Logger()
}
// Output duplicates the global logger and sets w as its output.
func Output(w io.Writer) zerolog.Logger {
return Logger.Output(w)
}
// With creates a child logger with the field added to its context.
func With() zerolog.Context {
return Logger.With()
}
// Level creates a child logger with the minimum accepted level set to level.
func Level(level zerolog.Level) zerolog.Logger {
return Logger.Level(level)
}
// Sample returns a logger with the s sampler.
func Sample(s zerolog.Sampler) zerolog.Logger {
return Logger.Sample(s)
}
// Hook returns a logger with the h Hook.
func Hook(h zerolog.Hook) zerolog.Logger {
return Logger.Hook(h)
}
// 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 Logger.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 Logger.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 Logger.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 Logger.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 Logger.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 Logger.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 Logger.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 Logger.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{}) {
Logger.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{}) {
Logger.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)
}