Skip to content

Commit

Permalink
build: apply log type flags in NewDefaultLogHandlers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ellemouton committed Oct 26, 2024
1 parent acbb33b commit 1e39e37
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
21 changes: 18 additions & 3 deletions build/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
// 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) {
func NewDefaultLogHandlers(cfg *LogConfig,
rotator *RotatingLogWriter) []btclog.Handler {

var handlers []btclog.Handler

consoleLogHandler := btclog.NewDefaultHandler(
os.Stdout, cfg.Console.HandlerOptions()...,
Expand All @@ -19,5 +21,18 @@ func NewDefaultLogHandlers(cfg *LogConfig, rotator *RotatingLogWriter) (
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
}
25 changes: 3 additions & 22 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btclog/v2"
flags "github.com/jessevdk/go-flags"
"github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lnd/autopilot"
Expand Down Expand Up @@ -1404,32 +1403,14 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
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) {
return nil, mkErr("invalid log compressor: %v",
cfg.LogCompressor)
}

cfg.SubLogMgr = build.NewSubLoggerManager(logHandlers...)
cfg.SubLogMgr = build.NewSubLoggerManager(build.NewDefaultLogHandlers(
cfg.LogConfig, cfg.LogRotator,
)...)

// Initialize logging at the default logging level.
SetupLoggers(cfg.SubLogMgr, interceptor)
Expand Down

0 comments on commit 1e39e37

Please sign in to comment.