-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.go
61 lines (54 loc) · 1.42 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
package main
import (
"io"
"log/slog"
"os"
"time"
"github.com/docker/go-units"
"github.com/gitsang/ddns/pkg/logi"
timex "github.com/gitsang/ddns/pkg/util/time"
"github.com/natefinch/lumberjack"
)
func NewLogHandler(defaultConf LogConfig, fancoutConfs ...LogConfig) []slog.Handler {
configs := []LogConfig{defaultConf}
configs = append(configs, fancoutConfs...)
handlers := make([]slog.Handler, 0)
for _, config := range configs {
writers := make([]io.Writer, 0)
if config.Output.Stdout.Enable {
writers = append(writers, os.Stdout)
}
if config.Output.Stderr.Enable {
writers = append(writers, os.Stderr)
}
if config.Output.File.Enable {
maxSize, err := units.FromHumanSize(config.Output.File.MaxSize)
if err != nil {
panic(err)
}
maxAgeDur, err := timex.ParseDuration(config.Output.File.MaxAge)
if err != nil {
panic(err)
}
writers = append(writers, &lumberjack.Logger{
Filename: config.Output.File.Path,
MaxSize: int(maxSize / units.MB),
MaxAge: int(maxAgeDur / (24 * time.Hour)),
MaxBackups: config.Output.File.MaxBackups,
LocalTime: false,
Compress: config.Output.File.Compress,
})
}
handler := logi.NewHandler(
logi.HandlerOptions{
Format: config.Format,
Level: config.Level,
Writers: writers,
Verbosity: config.Verbosity,
CallerSkip: 7,
},
)
handlers = append(handlers, handler)
}
return handlers
}