-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
zap_disk_logging.go
95 lines (77 loc) · 2.47 KB
/
zap_disk_logging.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
package logger
import (
"time"
"github.com/smartcontractkit/chainlink/core/utils"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
const (
// `Fatal` is the max log level allowed, so log levels like `Panic` or `Critical` won't be logged to disk if this is set.
disabledLevel = zapcore.FatalLevel + 1
diskPollInterval = 1 * time.Minute
)
type zapDiskPollConfig struct {
stop func()
pollChan <-chan time.Time
}
func newDiskPollConfig(interval time.Duration) zapDiskPollConfig {
ticker := time.NewTicker(utils.WithJitter(interval))
return zapDiskPollConfig{
pollChan: ticker.C,
stop: ticker.Stop,
}
}
func (cfg zapLoggerConfig) newDiskCore() (zapcore.Core, error) {
availableSpace, err := cfg.diskStats.AvailableSpace(cfg.local.Dir)
if err != nil || availableSpace < cfg.local.RequiredDiskSpace() {
// Won't log to disk if the directory is not found or there's not enough disk space
cfg.diskLogLevel.SetLevel(disabledLevel)
}
var (
encoder = zapcore.NewConsoleEncoder(makeEncoderConfig(cfg.local))
sink = zapcore.AddSync(&lumberjack.Logger{
Filename: logFileURI(cfg.local.Dir),
MaxSize: cfg.local.FileMaxSizeMB,
MaxAge: cfg.local.FileMaxAgeDays,
MaxBackups: cfg.local.FileMaxBackups,
Compress: true,
})
allLogLevels = zap.LevelEnablerFunc(cfg.diskLogLevel.Enabled)
)
return zapcore.NewCore(encoder, sink, allLogLevels), nil
}
func (l *zapLogger) pollDiskSpace() {
defer l.config.diskPollConfig.stop()
defer close(l.pollDiskSpaceDone)
for {
select {
case <-l.pollDiskSpaceStop:
return
case <-l.config.diskPollConfig.pollChan:
lvl := zapcore.DebugLevel
diskUsage, err := l.config.diskStats.AvailableSpace(l.config.local.Dir)
if err != nil {
// Will no longer log to disk
lvl = disabledLevel
l.Warnw("Error getting disk space available for logging", "err", err)
} else if diskUsage < l.config.local.RequiredDiskSpace() {
// Will no longer log to disk
lvl = disabledLevel
l.Warnf(
"Disk space is not enough to log into disk any longer, required disk space: %s, Available disk space: %s",
l.config.local.RequiredDiskSpace(),
diskUsage,
)
}
lvlBefore := l.config.diskLogLevel.Level()
l.config.diskLogLevel.SetLevel(lvl)
if lvlBefore == disabledLevel && lvl == zapcore.DebugLevel {
l.Info("Resuming disk logs, disk has enough space")
}
if l.config.testDiskLogLvlChan != nil {
l.config.testDiskLogLvlChan <- lvl
}
}
}
}