mirror of
https://github.com/miniflux/v2.git
synced 2025-08-06 17:41:00 +00:00
Implement structured logging using log/slog package
This commit is contained in:
parent
54cb8fa028
commit
c0e954f19d
77 changed files with 1868 additions and 892 deletions
46
internal/cli/logger.go
Normal file
46
internal/cli/logger.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package cli // import "miniflux.app/v2/internal/cli"
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
func InitializeDefaultLogger(logLevel string, logFile io.Writer, logFormat string, logTime bool) error {
|
||||
var programLogLevel = new(slog.LevelVar)
|
||||
switch logLevel {
|
||||
case "debug":
|
||||
programLogLevel.Set(slog.LevelDebug)
|
||||
case "info":
|
||||
programLogLevel.Set(slog.LevelInfo)
|
||||
case "warning":
|
||||
programLogLevel.Set(slog.LevelWarn)
|
||||
case "error":
|
||||
programLogLevel.Set(slog.LevelError)
|
||||
}
|
||||
|
||||
logHandlerOptions := &slog.HandlerOptions{Level: programLogLevel}
|
||||
if !logTime {
|
||||
logHandlerOptions.ReplaceAttr = func(groups []string, a slog.Attr) slog.Attr {
|
||||
if a.Key == slog.TimeKey {
|
||||
return slog.Attr{}
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
var logger *slog.Logger
|
||||
switch logFormat {
|
||||
case "json":
|
||||
logger = slog.New(slog.NewJSONHandler(logFile, logHandlerOptions))
|
||||
default:
|
||||
logger = slog.New(slog.NewTextHandler(logFile, logHandlerOptions))
|
||||
}
|
||||
|
||||
slog.SetDefault(logger)
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue