2023-08-14 18:53:05 +03:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2024-04-04 15:52:39 +03:00
|
|
|
"cmp"
|
2023-08-14 18:53:05 +03:00
|
|
|
"fmt"
|
2024-07-10 15:18:46 +03:00
|
|
|
"log/slog"
|
2023-08-14 18:53:05 +03:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2024-07-10 15:18:46 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2023-08-14 18:53:05 +03:00
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// configSyslog is used to indicate that syslog or eventlog (win) should be used
|
|
|
|
// for logger output.
|
|
|
|
const configSyslog = "syslog"
|
|
|
|
|
2024-07-10 15:18:46 +03:00
|
|
|
// newSlogLogger returns new [*slog.Logger] configured with the given settings.
|
|
|
|
func newSlogLogger(ls *logSettings) (l *slog.Logger) {
|
|
|
|
if !ls.Enabled {
|
|
|
|
return slogutil.NewDiscardLogger()
|
|
|
|
}
|
2023-08-14 18:53:05 +03:00
|
|
|
|
2024-10-04 19:10:32 +03:00
|
|
|
lvl := slog.LevelInfo
|
|
|
|
if ls.Verbose {
|
|
|
|
lvl = slog.LevelDebug
|
|
|
|
}
|
|
|
|
|
2024-07-10 15:18:46 +03:00
|
|
|
return slogutil.New(&slogutil.Config{
|
|
|
|
Format: slogutil.FormatAdGuardLegacy,
|
2024-10-04 19:10:32 +03:00
|
|
|
Level: lvl,
|
2024-07-10 15:18:46 +03:00
|
|
|
AddTimestamp: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// configureLogger configures logger level and output.
|
|
|
|
func configureLogger(ls *logSettings) (err error) {
|
2023-08-14 18:53:05 +03:00
|
|
|
// Configure logger level.
|
2024-06-20 19:44:51 +03:00
|
|
|
if !ls.Enabled {
|
|
|
|
log.SetLevel(log.OFF)
|
|
|
|
} else if ls.Verbose {
|
2023-08-14 18:53:05 +03:00
|
|
|
log.SetLevel(log.DEBUG)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that we see the microseconds in logs, as networking stuff can
|
|
|
|
// happen pretty quickly.
|
|
|
|
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
|
|
|
|
|
|
|
|
// Write logs to stdout by default.
|
|
|
|
if ls.File == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if ls.File == configSyslog {
|
|
|
|
// Use syslog where it is possible and eventlog on Windows.
|
|
|
|
err = aghos.ConfigureSyslog(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot initialize syslog: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logFilePath := ls.File
|
|
|
|
if !filepath.IsAbs(logFilePath) {
|
|
|
|
logFilePath = filepath.Join(Context.workDir, logFilePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetOutput(&lumberjack.Logger{
|
|
|
|
Filename: logFilePath,
|
|
|
|
Compress: ls.Compress,
|
|
|
|
LocalTime: ls.LocalTime,
|
|
|
|
MaxBackups: ls.MaxBackups,
|
|
|
|
MaxSize: ls.MaxSize,
|
|
|
|
MaxAge: ls.MaxAge,
|
|
|
|
})
|
|
|
|
|
2024-07-10 15:18:46 +03:00
|
|
|
return err
|
2023-08-14 18:53:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// getLogSettings returns a log settings object properly initialized from opts.
|
|
|
|
func getLogSettings(opts options) (ls *logSettings) {
|
|
|
|
configLogSettings := config.Log
|
|
|
|
|
|
|
|
ls = readLogSettings()
|
|
|
|
if ls == nil {
|
|
|
|
// Use default log settings.
|
|
|
|
ls = &configLogSettings
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command-line arguments can override config settings.
|
|
|
|
if opts.verbose {
|
|
|
|
ls.Verbose = true
|
|
|
|
}
|
2024-02-21 17:01:15 +03:00
|
|
|
|
2024-04-04 15:52:39 +03:00
|
|
|
ls.File = cmp.Or(opts.logFile, ls.File)
|
2023-08-14 18:53:05 +03:00
|
|
|
|
|
|
|
if opts.runningAsService && ls.File == "" && runtime.GOOS == "windows" {
|
|
|
|
// When running as a Windows service, use eventlog by default if
|
|
|
|
// nothing else is configured. Otherwise, we'll lose the log output.
|
|
|
|
ls.File = configSyslog
|
|
|
|
}
|
|
|
|
|
|
|
|
return ls
|
|
|
|
}
|
|
|
|
|
|
|
|
// readLogSettings reads logging settings from the config file. We do it in a
|
|
|
|
// separate method in order to configure logger before the actual configuration
|
|
|
|
// is parsed and applied.
|
|
|
|
func readLogSettings() (ls *logSettings) {
|
2024-06-20 19:44:51 +03:00
|
|
|
// TODO(s.chzhen): Add a helper function that returns default parameters
|
|
|
|
// for this structure and for the global configuration structure [config].
|
|
|
|
conf := &configuration{
|
|
|
|
Log: logSettings{
|
|
|
|
// By default, it is true if the property does not exist.
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
}
|
2023-08-14 18:53:05 +03:00
|
|
|
|
|
|
|
yamlFile, err := readConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(yamlFile, conf)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Couldn't get logging settings from the configuration: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &conf.Log
|
|
|
|
}
|