This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
80 lines (66 loc) · 1.77 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
package service
import (
"io"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
LogFormatText LogFormat = "text"
LogFormatJSON LogFormat = "json"
defaultLogFormat = LogFormatText
defaultLogLevel = zapcore.InfoLevel
)
// LogFormat represents a format of logs.
type LogFormat string
// LoggerOption represents a logger configuration option.
type LoggerOption interface {
apply(*loggerConfig)
}
// loggerOptionFunc is a wrapper for configuration function, which satisfies
// LoggerOption interface.
type loggerOptionFunc func(*loggerConfig)
func (fn loggerOptionFunc) apply(cfg *loggerConfig) {
fn(cfg)
}
// loggerConfig contains logger configuration.
type loggerConfig struct {
format LogFormat
level zapcore.Level
out io.Writer
opts []zap.Option
}
// newLoggerConfig constructs logger configuration object with default configuration.
func newLoggerConfig() *loggerConfig {
return &loggerConfig{
format: defaultLogFormat,
level: defaultLogLevel,
}
}
// WithLevel returns log level configuration option.
func WithLevel(l zapcore.Level) LoggerOption {
return loggerOptionFunc(func(cfg *loggerConfig) {
cfg.level = l
})
}
// WithFormat returns log format configuration option.
func WithFormat(f LogFormat) LoggerOption {
return loggerOptionFunc(func(cfg *loggerConfig) {
cfg.format = f
})
}
// WithOptions returns configuration option, which sets custom options
// to the logger.
//
// See: https://pkg.go.dev/go.uber.org/zap#Option
func WithOptions(opts ...zap.Option) LoggerOption {
return loggerOptionFunc(func(cfg *loggerConfig) {
cfg.opts = opts
})
}
// WithOutput returns configuration option, which sets log output.
// Default is os.Stdout.
func WithOutput(w io.Writer) LoggerOption {
return loggerOptionFunc(func(cfg *loggerConfig) {
cfg.out = w
})
}