build+config: add default handler constructor

Since most of our projects will use the same handler duo and apply the
config options the same way, let's make our lives easier and add a
default handler constructor.
This commit is contained in:
Elle Mouton 2024-10-15 14:44:47 +02:00
parent 5ed7bf1b71
commit cfa7fceb0b
No known key found for this signature in database
GPG Key ID: D7D916376026F177
2 changed files with 29 additions and 8 deletions

23
build/handlers.go Normal file
View File

@ -0,0 +1,23 @@
package build
import (
"os"
"github.com/btcsuite/btclog/v2"
)
// NewDefaultLogHandlers returns the standard console logger and rotating log
// writer handlers that we generally want to use. It also applies the various
// config options to the loggers.
func NewDefaultLogHandlers(cfg *LogConfig, rotator *RotatingLogWriter) (
btclog.Handler, btclog.Handler) {
consoleLogHandler := btclog.NewDefaultHandler(
os.Stdout, cfg.Console.HandlerOptions()...,
)
logFileHandler := btclog.NewDefaultHandler(
rotator, cfg.File.HandlerOptions()...,
)
return consoleLogHandler, logFileHandler
}

View File

@ -1404,15 +1404,13 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
)
var logCfg = cfg.LogConfig
consoleLogHandler := btclog.NewDefaultHandler(
os.Stdout, logCfg.Console.HandlerOptions()...,
var (
logCfg = cfg.LogConfig
logHandlers []btclog.Handler
consoleLogHandler, logFileHandler = build.NewDefaultLogHandlers(
logCfg, cfg.LogRotator,
)
)
logFileHandler := btclog.NewDefaultHandler(
cfg.LogRotator, logCfg.File.HandlerOptions()...,
)
var logHandlers []btclog.Handler
maybeAddLogger := func(cmdOptionDisable bool, handler btclog.Handler) {
if !cmdOptionDisable {
logHandlers = append(logHandlers, handler)