2023-06-20 14:11:34 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-11-13 15:44:21 +03:00
|
|
|
"io"
|
|
|
|
"log/slog"
|
2023-06-20 14:11:34 +03:00
|
|
|
"os"
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2023-06-20 14:11:34 +03:00
|
|
|
)
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// newBaseLogger constructs a base logger based on the command-line options.
|
|
|
|
// opts must not be nil.
|
|
|
|
func newBaseLogger(opts *options) (baseLogger *slog.Logger) {
|
|
|
|
var output io.Writer
|
2023-06-20 14:11:34 +03:00
|
|
|
switch opts.confFile {
|
|
|
|
case "stdout":
|
2024-11-13 15:44:21 +03:00
|
|
|
output = os.Stdout
|
2023-06-20 14:11:34 +03:00
|
|
|
case "stderr":
|
2024-11-13 15:44:21 +03:00
|
|
|
output = os.Stderr
|
2023-06-20 14:11:34 +03:00
|
|
|
case "syslog":
|
2024-11-13 15:44:21 +03:00
|
|
|
// TODO(a.garipov): Add a syslog handler to golibs.
|
2023-06-20 14:11:34 +03:00
|
|
|
default:
|
2024-11-13 15:44:21 +03:00
|
|
|
// TODO(a.garipov): Use the path.
|
2023-06-20 14:11:34 +03:00
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
lvl := slog.LevelInfo
|
2023-06-20 14:11:34 +03:00
|
|
|
if opts.verbose {
|
2024-11-13 15:44:21 +03:00
|
|
|
lvl = slog.LevelDebug
|
2023-06-20 14:11:34 +03:00
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
return slogutil.New(&slogutil.Config{
|
|
|
|
Output: output,
|
|
|
|
// TODO(a.garipov): Get from config?
|
|
|
|
Format: slogutil.FormatText,
|
|
|
|
Level: lvl,
|
|
|
|
// TODO(a.garipov): Get from config.
|
|
|
|
AddTimestamp: true,
|
|
|
|
})
|
2023-06-20 14:11:34 +03:00
|
|
|
}
|