-
Notifications
You must be signed in to change notification settings - Fork 8
/
logger.go
180 lines (149 loc) · 4.13 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
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
package logging
import (
"context"
"io"
"log/slog"
"os"
"gopkg.in/natefinch/lumberjack.v2"
)
const (
defaultLevel = LevelInfo
defaultAddSource = true
defaultIsJSON = true
defaultSetDefault = true
defaultLogFile = ""
defaultLogFileMaxSizeMB = 10
defaultLogFileMaxBackups = 3
defaultLogFileMaxAgeDays = 14
)
func NewLogger(opts ...LoggerOption) *Logger {
config := &LoggerOptions{
Level: defaultLevel,
AddSource: defaultAddSource,
IsJSON: defaultIsJSON,
SetDefault: defaultSetDefault,
LogFilePath: defaultLogFile,
LogFileMaxSizeMB: defaultLogFileMaxSizeMB,
LogFileMaxBackups: defaultLogFileMaxBackups,
LogFileMaxAgeDays: defaultLogFileMaxAgeDays,
}
for _, opt := range opts {
opt(config)
}
options := &HandlerOptions{
AddSource: config.AddSource,
Level: config.Level,
}
// by default we write to stdout.
var w io.Writer = os.Stdout
// file or stdout.
if config.LogFilePath != "" {
w = &lumberjack.Logger{
Filename: config.LogFilePath,
MaxSize: config.LogFileMaxSizeMB,
MaxBackups: config.LogFileMaxBackups,
MaxAge: config.LogFileMaxAgeDays,
Compress: config.LogFileCompress,
}
}
var h Handler = NewTextHandler(w, options)
if config.IsJSON {
h = NewJSONHandler(w, options)
}
logger := New(h)
if config.SetDefault {
SetDefault(logger)
}
return logger
}
type LoggerOptions struct {
Level Level
AddSource bool
IsJSON bool
SetDefault bool
LogFilePath string
LogFileMaxSizeMB int
LogFileMaxBackups int
LogFileMaxAgeDays int
LogFileCompress bool
}
type LoggerOption func(*LoggerOptions)
// WithLevel logger option sets the log level, if not set, the default level is Info.
func WithLevel(level string) LoggerOption {
return func(o *LoggerOptions) {
var l Level
if err := l.UnmarshalText([]byte(level)); err != nil {
l = LevelInfo
}
o.Level = l
}
}
// WithAddSource logger option sets the add source option, which will add source file and line number to the log record.
func WithAddSource(addSource bool) LoggerOption {
return func(o *LoggerOptions) {
o.AddSource = addSource
}
}
// WithIsJSON logger option sets the is json option, which will set JSON format for the log record.
func WithIsJSON(isJSON bool) LoggerOption {
return func(o *LoggerOptions) {
o.IsJSON = isJSON
}
}
// WithSetDefault logger option sets the set default option, which will set the created logger as default logger.
func WithSetDefault(setDefault bool) LoggerOption {
return func(o *LoggerOptions) {
o.SetDefault = setDefault
}
}
// WithLogFilePath logger option sets the file where logs will be written.
func WithLogFilePath(logFilePath string) LoggerOption {
return func(o *LoggerOptions) {
o.LogFilePath = logFilePath
}
}
// WithLogFileMaxSizeMB logger option sets the maximum file size for rotation.
func WithLogFileMaxSizeMB(maxSize int) LoggerOption {
return func(o *LoggerOptions) {
o.LogFileMaxSizeMB = maxSize
}
}
// WithLogFileMaxBackups logger option sets the number of backup files to retain.
func WithLogFileMaxBackups(maxBackups int) LoggerOption {
return func(o *LoggerOptions) {
o.LogFileMaxBackups = maxBackups
}
}
// WithLogFileMaxAgeDays logger option sets the maximum age of the log files.
func WithLogFileMaxAgeDays(maxAge int) LoggerOption {
return func(o *LoggerOptions) {
o.LogFileMaxAgeDays = maxAge
}
}
// WithLogFileCompress logger options set needs compression.
func WithLogFileCompress(compression bool) LoggerOption {
return func(o *LoggerOptions) {
o.LogFileCompress = compression
}
}
// WithAttrs returns logger with attributes.
func WithAttrs(ctx context.Context, attrs ...Attr) *Logger {
logger := L(ctx)
for _, attr := range attrs {
logger = logger.With(attr)
}
return logger
}
// WithDefaultAttrs returns logger with default attributes.
func WithDefaultAttrs(logger *Logger, attrs ...Attr) *Logger {
for _, attr := range attrs {
logger = logger.With(attr)
}
return logger
}
func L(ctx context.Context) *Logger {
return loggerFromContext(ctx)
}
func Default() *Logger {
return slog.Default()
}