build: apply log type flags in NewDefaultLogHandlers

So that the logging config "Disable" options and log type flags are all
handled in one place. Other repo's can then re-use this nicely.
This commit is contained in:
Elle Mouton 2024-10-23 10:09:27 +02:00
parent acbb33bb7b
commit 1e39e3758e
No known key found for this signature in database
GPG key ID: D7D916376026F177
2 changed files with 21 additions and 25 deletions

View file

@ -9,8 +9,10 @@ import (
// NewDefaultLogHandlers returns the standard console logger and rotating log // NewDefaultLogHandlers returns the standard console logger and rotating log
// writer handlers that we generally want to use. It also applies the various // writer handlers that we generally want to use. It also applies the various
// config options to the loggers. // config options to the loggers.
func NewDefaultLogHandlers(cfg *LogConfig, rotator *RotatingLogWriter) ( func NewDefaultLogHandlers(cfg *LogConfig,
btclog.Handler, btclog.Handler) { rotator *RotatingLogWriter) []btclog.Handler {
var handlers []btclog.Handler
consoleLogHandler := btclog.NewDefaultHandler( consoleLogHandler := btclog.NewDefaultHandler(
os.Stdout, cfg.Console.HandlerOptions()..., os.Stdout, cfg.Console.HandlerOptions()...,
@ -19,5 +21,18 @@ func NewDefaultLogHandlers(cfg *LogConfig, rotator *RotatingLogWriter) (
rotator, cfg.File.HandlerOptions()..., rotator, cfg.File.HandlerOptions()...,
) )
return consoleLogHandler, logFileHandler maybeAddLogger := func(cmdOptionDisable bool, handler btclog.Handler) {
if !cmdOptionDisable {
handlers = append(handlers, handler)
}
}
switch LoggingType {
case LogTypeStdOut:
maybeAddLogger(cfg.Console.Disable, consoleLogHandler)
case LogTypeDefault:
maybeAddLogger(cfg.Console.Disable, consoleLogHandler)
maybeAddLogger(cfg.File.Disable, logFileHandler)
}
return handlers
} }

View file

@ -21,7 +21,6 @@ import (
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btclog/v2"
flags "github.com/jessevdk/go-flags" flags "github.com/jessevdk/go-flags"
"github.com/lightninglabs/neutrino" "github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lnd/autopilot" "github.com/lightningnetwork/lnd/autopilot"
@ -1404,32 +1403,14 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name), lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
) )
var (
logCfg = cfg.LogConfig
logHandlers []btclog.Handler
consoleLogHandler, logFileHandler = build.NewDefaultLogHandlers(
logCfg, cfg.LogRotator,
)
)
maybeAddLogger := func(cmdOptionDisable bool, handler btclog.Handler) {
if !cmdOptionDisable {
logHandlers = append(logHandlers, handler)
}
}
switch build.LoggingType {
case build.LogTypeStdOut:
maybeAddLogger(logCfg.Console.Disable, consoleLogHandler)
case build.LogTypeDefault:
maybeAddLogger(logCfg.Console.Disable, consoleLogHandler)
maybeAddLogger(logCfg.File.Disable, logFileHandler)
}
if !build.SuportedLogCompressor(cfg.LogCompressor) { if !build.SuportedLogCompressor(cfg.LogCompressor) {
return nil, mkErr("invalid log compressor: %v", return nil, mkErr("invalid log compressor: %v",
cfg.LogCompressor) cfg.LogCompressor)
} }
cfg.SubLogMgr = build.NewSubLoggerManager(logHandlers...) cfg.SubLogMgr = build.NewSubLoggerManager(build.NewDefaultLogHandlers(
cfg.LogConfig, cfg.LogRotator,
)...)
// Initialize logging at the default logging level. // Initialize logging at the default logging level.
SetupLoggers(cfg.SubLogMgr, interceptor) SetupLoggers(cfg.SubLogMgr, interceptor)