feat: persist diagnostic logs with daily rotation and retention

This commit is contained in:
Ed1s0nZ
2026-09-08 14:41:46 +08:00
parent 6ad9ea2d13
commit 94cdf760af
10 changed files with 255 additions and 6 deletions
+15 -1
View File
@@ -11,7 +11,7 @@ type Logger struct {
*zap.Logger
}
func New(level, output string) *Logger {
func New(level, output string, diagnostics ...DiagnosticOptions) *Logger {
var zapLevel zapcore.Level
switch level {
case "debug":
@@ -34,6 +34,8 @@ func New(level, output string) *Logger {
var writeSyncer zapcore.WriteSyncer
if output == "stdout" {
writeSyncer = zapcore.AddSync(os.Stdout)
} else if output == "stderr" {
writeSyncer = zapcore.AddSync(os.Stderr)
} else {
file, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
@@ -49,6 +51,18 @@ func New(level, output string) *Logger {
zapLevel,
)
options := DiagnosticOptions{}
if len(diagnostics) > 0 {
options = diagnostics[0]
}
if !options.Disabled {
// The diagnostic threshold is independent of the primary output level.
core = zapcore.NewTee(core, zapcore.NewCore(
zapcore.NewJSONEncoder(config.EncoderConfig),
newDailyWriter(options), zapcore.WarnLevel,
))
}
logger := zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
return &Logger{Logger: logger}