all: partial revert slog logger usage

This commit is contained in:
Dimitry Kolyshev 2024-07-05 16:19:27 +03:00
parent f16cddbb8c
commit cdf9ccd371
6 changed files with 33 additions and 84 deletions

View File

@ -1,8 +1,6 @@
package aghos package aghos
import "io" // ConfigureSyslog reroutes standard logger output to syslog.
func ConfigureSyslog(serviceName string) error {
// ConfigureSyslog returns an output rerouted to syslog.
func ConfigureSyslog(serviceName string) (w io.Writer, err error) {
return configureSyslog(serviceName) return configureSyslog(serviceName)
} }

View File

@ -3,15 +3,16 @@
package aghos package aghos
import ( import (
"io"
"log/syslog" "log/syslog"
"github.com/AdguardTeam/golibs/log"
) )
func configureSyslog(serviceName string) (w io.Writer, err error) { func configureSyslog(serviceName string) error {
w, err = syslog.New(syslog.LOG_NOTICE|syslog.LOG_USER, serviceName) w, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_USER, serviceName)
if err != nil { if err != nil {
return nil, err return err
} }
log.SetOutput(w)
return w, nil return nil
} }

View File

@ -3,9 +3,9 @@
package aghos package aghos
import ( import (
"io"
"strings" "strings"
"github.com/AdguardTeam/golibs/log"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc/eventlog" "golang.org/x/sys/windows/svc/eventlog"
) )
@ -19,26 +19,23 @@ func (w *eventLogWriter) Write(b []byte) (int, error) {
return len(b), w.el.Info(1, string(b)) return len(b), w.el.Info(1, string(b))
} }
func configureSyslog(serviceName string) (w io.Writer, err error) { func configureSyslog(serviceName string) error {
// Note that the eventlog src is the same as the service name. Otherwise, // Note that the eventlog src is the same as the service name
// we will get "the description for event id cannot be found" warning in // Otherwise, we will get "the description for event id cannot be found" warning in every log record
// every log record.
// Continue if we receive "registry key already exists" or if we get // Continue if we receive "registry key already exists" or if we get
// ERROR_ACCESS_DENIED so that we can log without administrative permissions // ERROR_ACCESS_DENIED so that we can log without administrative permissions
// for pre-existing eventlog sources. // for pre-existing eventlog sources.
err = eventlog.InstallAsEventCreate(serviceName, eventlog.Info|eventlog.Warning|eventlog.Error) if err := eventlog.InstallAsEventCreate(serviceName, eventlog.Info|eventlog.Warning|eventlog.Error); err != nil {
if err != nil { if !strings.Contains(err.Error(), "registry key already exists") && err != windows.ERROR_ACCESS_DENIED {
if !strings.Contains(err.Error(), "registry key already exists") && return err
err != windows.ERROR_ACCESS_DENIED {
return nil, err
} }
} }
el, err := eventlog.Open(serviceName) el, err := eventlog.Open(serviceName)
if err != nil { if err != nil {
return nil, err return err
} }
return &eventLogWriter{el: el}, nil log.SetOutput(&eventLogWriter{el: el})
return nil
} }

View File

@ -39,7 +39,6 @@ import (
"github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/hostsfile" "github.com/AdguardTeam/golibs/hostsfile"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/osutil" "github.com/AdguardTeam/golibs/osutil"
) )
@ -559,15 +558,14 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
err = configureLogger(ls) err = configureLogger(ls)
fatalOnError(err) fatalOnError(err)
// Configure slog logger. // TODO(a.garipov): Use slog everywhere.
l, err := initLogger(ls) l := initLogger(ls)
fatalOnError(err)
// Print the first message after logger is configured. // Print the first message after logger is configured.
l.Info(version.Full()) log.Info(version.Full())
l.Debug("current working directory is set", "work_dir", Context.workDir) log.Debug("current working directory is %s", Context.workDir)
if opts.runningAsService { if opts.runningAsService {
l.Info("AdGuard Home is running as a service") log.Info("AdGuard Home is running as a service")
} }
err = setupContext(opts, l) err = setupContext(opts, l)
@ -598,7 +596,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
} }
confPath := configFilePath() confPath := configFilePath()
l.Debug("using config path for updater", "path", confPath) log.Debug("using config path %q for updater", confPath)
upd := updater.NewUpdater(&updater.Config{ upd := updater.NewUpdater(&updater.Config{
Client: config.Filtering.HTTPClient, Client: config.Filtering.HTTPClient,
@ -640,7 +638,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
Context.tls, err = newTLSManager(config.TLS, config.DNS.ServePlainDNS) Context.tls, err = newTLSManager(config.TLS, config.DNS.ServePlainDNS)
if err != nil { if err != nil {
l.Error("initializing tls", slogutil.KeyError, err) log.Error("initializing tls: %s", err)
onConfigModified() onConfigModified()
} }
@ -664,7 +662,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
if Context.dhcpServer != nil { if Context.dhcpServer != nil {
err = Context.dhcpServer.Start() err = Context.dhcpServer.Start()
if err != nil { if err != nil {
l.Error("starting dhcp server", slogutil.KeyError, err) log.Error("starting dhcp server: %s", err)
} }
} }
} }

View File

@ -3,7 +3,6 @@ package home
import ( import (
"cmp" "cmp"
"fmt" "fmt"
"io"
"log/slog" "log/slog"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -20,53 +19,16 @@ import (
const configSyslog = "syslog" const configSyslog = "syslog"
// initLogger returns new [*slog.Logger] configured with the given settings. // initLogger returns new [*slog.Logger] configured with the given settings.
func initLogger(ls *logSettings) (l *slog.Logger, err error) { func initLogger(ls *logSettings) (l *slog.Logger) {
if !ls.Enabled { if !ls.Enabled {
return slogutil.NewDiscardLogger(), nil return slogutil.NewDiscardLogger()
}
w, err := logOutput(ls)
if err != nil {
return nil, fmt.Errorf("cannot initialize log output: %w", err)
} }
return slogutil.New(&slogutil.Config{ return slogutil.New(&slogutil.Config{
Output: w, Format: slogutil.FormatAdGuardLegacy,
Format: slogutil.FormatDefault,
AddTimestamp: true, AddTimestamp: true,
Verbose: ls.Verbose, Verbose: ls.Verbose,
}), nil })
}
// logOutput returns a log output [io.Writer] configured with the settings.
func logOutput(ls *logSettings) (w io.Writer, err error) {
if ls.File == "" {
return nil, nil
}
if ls.File == configSyslog {
// Use syslog where it is possible and eventlog on Windows.
w, err = aghos.ConfigureSyslog(serviceName)
if err != nil {
return nil, fmt.Errorf("cannot initialize syslog: %w", err)
}
return w, nil
}
logFilePath := ls.File
if !filepath.IsAbs(logFilePath) {
logFilePath = filepath.Join(Context.workDir, logFilePath)
}
return &lumberjack.Logger{
Filename: logFilePath,
Compress: ls.Compress,
LocalTime: ls.LocalTime,
MaxBackups: ls.MaxBackups,
MaxSize: ls.MaxSize,
MaxAge: ls.MaxAge,
}, nil
} }
// configureLogger configures logger level and output. // configureLogger configures logger level and output.
@ -89,14 +51,11 @@ func configureLogger(ls *logSettings) (err error) {
if ls.File == configSyslog { if ls.File == configSyslog {
// Use syslog where it is possible and eventlog on Windows. // Use syslog where it is possible and eventlog on Windows.
var w io.Writer err = aghos.ConfigureSyslog(serviceName)
w, err = aghos.ConfigureSyslog(serviceName)
if err != nil { if err != nil {
return fmt.Errorf("cannot initialize syslog: %w", err) return fmt.Errorf("cannot initialize syslog: %w", err)
} }
log.SetOutput(w)
return nil return nil
} }

View File

@ -2,7 +2,6 @@ package cmd
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"github.com/AdguardTeam/AdGuardHome/internal/aghos" "github.com/AdguardTeam/AdGuardHome/internal/aghos"
@ -23,13 +22,10 @@ func setLog(opts *options) (err error) {
case "stderr": case "stderr":
log.SetOutput(os.Stderr) log.SetOutput(os.Stderr)
case "syslog": case "syslog":
var w io.Writer err = aghos.ConfigureSyslog(syslogServiceName)
w, err = aghos.ConfigureSyslog(syslogServiceName)
if err != nil { if err != nil {
return fmt.Errorf("initializing syslog: %w", err) return fmt.Errorf("initializing syslog: %w", err)
} }
log.SetOutput(w)
default: default:
// TODO(a.garipov): Use the path. // TODO(a.garipov): Use the path.
} }