-
Notifications
You must be signed in to change notification settings - Fork 120
/
ytlog.go
88 lines (71 loc) · 1.93 KB
/
ytlog.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
package ytlog
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
logzap "go.ytsaurus.tech/library/go/core/log/zap"
"go.ytsaurus.tech/library/go/core/log/zap/asynczap"
"go.ytsaurus.tech/yt/go/ytlog/selfrotate"
)
// New returns synchronous stderr logger configured with YT defaults.
func New() (*logzap.Logger, error) {
conf := zap.NewProductionConfig()
conf.Level.SetLevel(zap.DebugLevel)
conf.Sampling = nil
conf.DisableStacktrace = true
conf.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
conf.OutputPaths = []string{"stderr"}
return logzap.New(conf)
}
// Must does the same as New but panics on error.
func Must() *logzap.Logger {
l, err := New()
if err != nil {
panic(err)
}
return l
}
var defaultRotationOptions = selfrotate.Options{
MaxKeep: 0,
MaxSize: 0,
MinFreeSpace: 0.05,
Compress: selfrotate.CompressDelayed,
RotateInterval: selfrotate.RotateHourly,
}
type (
logLevel struct {
level zapcore.Level
}
Option interface {
isOption()
}
)
func (logLevel) isOption() {}
func WithLogLevel(level zapcore.Level) Option {
return logLevel{level: level}
}
// NewSelfrotate returns logger configured with YT defaults.
func NewSelfrotate(logPath string, options ...Option) (l *logzap.Logger, stop func(), err error) {
rotateOptions := defaultRotationOptions
rotateOptions.Name = logPath
w, err := selfrotate.New(rotateOptions)
if err != nil {
return nil, nil, err
}
encoder := zap.NewProductionEncoderConfig()
encoder.EncodeTime = zapcore.ISO8601TimeEncoder
level := zap.DebugLevel
for _, opt := range options {
switch v := opt.(type) {
case logLevel:
level = v.level
}
}
core := asynczap.NewCore(zapcore.NewJSONEncoder(encoder), w, level, asynczap.Options{})
stop = func() {
core.Stop()
_ = w.Close()
}
// TODO(prime@): make callerSkip from library/go/core/log public.
zl := zap.New(core, zap.AddCallerSkip(1), zap.AddCaller())
return &logzap.Logger{L: zl}, stop, nil
}