-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger.go
40 lines (33 loc) · 996 Bytes
/
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
package gott
import (
"net/url"
"go.uber.org/zap"
"gopkg.in/natefinch/lumberjack.v2"
)
type lumberjackSink struct {
*lumberjack.Logger
}
// Sync implements zap.Sink. The remaining methods are implemented
// by the embedded *lumberjack.Logger.
func (lumberjackSink) Sync() error { return nil }
// NewLogger initializes a new zap.Logger with lumberjack support
// to write to log files with rotation.
func NewLogger(cnf loggingConfig) *zap.Logger {
_ = zap.RegisterSink("lumberjack", func(u *url.URL) (zap.Sink, error) {
return lumberjackSink{
Logger: &lumberjack.Logger{
Filename: u.Opaque,
MaxSize: cnf.MaxSize, // megabytes
MaxBackups: cnf.MaxBackups,
MaxAge: cnf.MaxAge, //days
Compress: cnf.EnableCompression,
},
}, nil
})
config := zap.NewDevelopmentConfig()
config.OutputPaths = []string{"lumberjack:logs/" + cnf.Filename}
config.DisableCaller = true
config.Level.SetLevel(cnf.logLevel)
logger, _ := config.Build()
return logger
}