-
Notifications
You must be signed in to change notification settings - Fork 6
/
log.go
222 lines (192 loc) · 6.23 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
package xlog
import (
"context"
"fmt"
"io"
llog "log"
"log/slog"
pkgerr "github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
)
type Logger = zerolog.Logger
type Level = zerolog.Level
type LevelWriter = zerolog.LevelWriter
type Context = zerolog.Context
type Event = zerolog.Event
type Hook = zerolog.Hook
type Sampler = zerolog.Sampler
const (
LevelDebug = zerolog.DebugLevel
LevelInfo = zerolog.InfoLevel
LevelWarn = zerolog.WarnLevel
LevelError = zerolog.ErrorLevel
LevelFatal = zerolog.FatalLevel
LevelPanic = zerolog.PanicLevel
LevelNone = zerolog.NoLevel
LevelSuppress = zerolog.Disabled
LevelTrace = zerolog.TraceLevel
)
var defaultOutput = StderrWriter()
type DefaultWriter struct{}
func (DefaultWriter) Write(p []byte) (n int, err error) { return defaultOutput.Write(p) }
func Default() *Logger { return &log.Logger }
// Not safe for concurrent use.
func SetDefaultOutput(w ...io.Writer) {
defaultOutput = zerolog.MultiLevelWriter(w...)
}
func WrapStackError(err error) error {
return pkgerr.WithStack(err)
}
func NewStackError(msg string) error {
return pkgerr.New(msg)
}
func NewStackErrorf(fmt string, args ...any) error {
return pkgerr.Errorf(fmt, args...)
}
// Replaces all defaults.
func init() {
log.Logger = *NewDomain("pmesh", DefaultWriter{})
passthrough, _ := ToTextWriter(&log.Logger, LevelInfo)
slog.SetDefault(ToSlog(&log.Logger))
llog.Default().SetOutput(passthrough)
zerolog.LevelFieldName = "l"
zerolog.TimestampFieldName = "t"
zerolog.MessageFieldName = "msg"
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
zerolog.CallerFieldName = DomainFieldName
zerolog.DefaultContextLogger = &log.Logger
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
}
// SetLoggerLevel sets the global logger level.
func SetLoggerLevel(level Level) {
zerolog.SetGlobalLevel(level)
}
// Output duplicates the global logger and sets w as its output.
func Output(w io.Writer) Logger {
return log.Logger.Output(w)
}
// With creates a child logger with the field added to its context.
func With() Context {
return log.Logger.With()
}
// 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) *Event {
return log.Logger.Err(err)
}
func ErrC(ctx context.Context, err error) *Event { return Ctx(ctx).Err(err) }
// ErrStack starts a new message with error level with err as a field if not nil or
// with info level if err is nil. The stack trace is attached to the event.
//
// You must call Msg on the returned event in order to send the event.
func ErrStack(err any) *Event {
if err != nil {
e, ok := err.(error)
if !ok {
e = NewStackError(fmt.Sprint(err))
} else {
e = WrapStackError(e)
}
return log.Logger.Error().Stack().Err(e)
}
return log.Logger.Info()
}
func ErrStackC(ctx context.Context, err any) *Event {
if err != nil {
e, ok := err.(error)
if !ok {
e = NewStackError(fmt.Sprint(err))
} else {
e = WrapStackError(e)
}
return Ctx(ctx).Error().Stack().Err(e)
}
return Ctx(ctx).Info()
}
// Trace starts a new message with trace level.
//
// You must call Msg on the returned event in order to send the event.
func Trace() *Event {
return log.Logger.Trace()
}
func TraceC(ctx context.Context) *Event { return Ctx(ctx).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() *Event {
return log.Logger.Debug()
}
func DebugC(ctx context.Context) *Event { return Ctx(ctx).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() *Event {
return log.Logger.Info()
}
func InfoC(ctx context.Context) *Event { return Ctx(ctx).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() *Event {
return log.Logger.Warn()
}
func WarnC(ctx context.Context) *Event { return Ctx(ctx).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() *Event {
return log.Logger.Error()
}
func ErrorC(ctx context.Context) *Event { return Ctx(ctx).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() *Event {
return log.Logger.Fatal()
}
func FatalC(ctx context.Context) *Event { return Ctx(ctx).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() *Event {
return log.Logger.Panic()
}
func PanicC(ctx context.Context) *Event { return Ctx(ctx).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 Level) *Event {
return log.Logger.WithLevel(level)
}
func WithLevelC(ctx context.Context, level Level) *Event { return Ctx(ctx).WithLevel(level) }
// Log starts a new message with no level. Setting GlobalLevel to
// 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() *Event {
return log.Logger.Log()
}
func LogC(ctx context.Context) *Event { return Ctx(ctx).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 ...any) {
log.Logger.Debug().CallerSkipFrame(1).Msg(fmt.Sprint(v...))
}
func PrintC(ctx context.Context, v ...any) { Ctx(ctx).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 ...any) {
log.Logger.Debug().CallerSkipFrame(1).Msgf(format, v...)
}
func PrintfC(ctx context.Context, format string, v ...any) { Ctx(ctx).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) *Logger {
return zerolog.Ctx(ctx)
}