-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.go
43 lines (34 loc) · 943 Bytes
/
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
package logger
import (
"context"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
)
type Logger struct {
conf *config
}
func NewLogger(opts ...Option) *Logger {
return &Logger{
conf: newConfig(opts...),
}
}
func (l *Logger) ctx(ctx context.Context) *zerolog.Logger {
var (
zctx zerolog.Context = log.Ctx(ctx).With()
)
return lo.ToPtr(zctx.Logger())
}
func (l *Logger) ReadLog(ctx context.Context, format string, v ...any) {
if l.conf.outputQueryLogType.AllowReadLog() {
l.ctx(ctx).Info().Fields(l.conf.fields).Msgf(format, v...)
}
}
func (l *Logger) WriteLog(ctx context.Context, format string, v ...any) {
if l.conf.outputQueryLogType.AllowWriteLog() {
l.ctx(ctx).Info().Fields(l.conf.fields).Msgf(format, v...)
}
}
func (l *Logger) ErrorLog(ctx context.Context, err error, format string, v ...any) {
l.ctx(ctx).Error().Fields(l.conf.fields).AnErr("error", err).Msgf(format, v...)
}