From 0512357c17e37cb7ff79b9ca063f897ed3229067 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 29 Oct 2024 17:50:52 +0200 Subject: [PATCH] build: dedup logging config Move all shared code to config.go which has no build tags. --- build/config.go | 61 ++++++++++++++++++++++++++++++++++++++ build/config_dev.go | 70 +++++++------------------------------------- build/config_prod.go | 61 ++++++-------------------------------- 3 files changed, 80 insertions(+), 112 deletions(-) create mode 100644 build/config.go diff --git a/build/config.go b/build/config.go new file mode 100644 index 000000000..1cfce7a45 --- /dev/null +++ b/build/config.go @@ -0,0 +1,61 @@ +package build + +import "github.com/btcsuite/btclog/v2" + +const ( + callSiteOff = "off" + callSiteShort = "short" + callSiteLong = "long" +) + +// LogConfig holds logging configuration options. +// +//nolint:lll +type LogConfig struct { + Console *consoleLoggerCfg `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."` + File *LoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."` +} + +// LoggerConfig holds options for a particular logger. +// +//nolint:lll +type LoggerConfig struct { + Disable bool `long:"disable" description:"Disable this logger."` + NoTimestamps bool `long:"no-timestamps" description:"Omit timestamps from log lines."` + CallSite string `long:"call-site" description:"Include the call-site of each log line." choice:"off" choice:"short" choice:"long"` +} + +// DefaultLogConfig returns the default logging config options. +func DefaultLogConfig() *LogConfig { + return &LogConfig{ + Console: defaultConsoleLoggerCfg(), + File: &LoggerConfig{ + CallSite: callSiteOff, + }, + } +} + +// HandlerOptions returns the set of btclog.HandlerOptions that the state of the +// config struct translates to. +func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption { + opts := []btclog.HandlerOption{ + // The default skip depth used by the logging library is 6 but + // since we wrap the logging handlers with another level of + // abstraction with the handlerSet, we increase the skip depth + // to 7 here. + btclog.WithCallSiteSkipDepth(7), + } + + if cfg.NoTimestamps { + opts = append(opts, btclog.WithNoTimestamp()) + } + + switch cfg.CallSite { + case callSiteShort: + opts = append(opts, btclog.WithCallerFlags(btclog.Lshortfile)) + case callSiteLong: + opts = append(opts, btclog.WithCallerFlags(btclog.Llongfile)) + } + + return opts +} diff --git a/build/config_dev.go b/build/config_dev.go index daa5b1fde..09df7d618 100644 --- a/build/config_dev.go +++ b/build/config_dev.go @@ -17,68 +17,8 @@ const ( faintSeq = "2" esc = '\x1b' csi = string(esc) + "[" - - callSiteOff = "off" - callSiteShort = "short" - callSiteLong = "long" ) -// LogConfig holds logging configuration options. -// -//nolint:lll -type LogConfig struct { - Console *consoleLoggerCfg `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."` - File *LoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."` -} - -// DefaultLogConfig returns the default logging config options. -func DefaultLogConfig() *LogConfig { - return &LogConfig{ - Console: &consoleLoggerCfg{ - LoggerConfig: LoggerConfig{ - CallSite: callSiteShort, - }, - }, - File: &LoggerConfig{ - CallSite: callSiteOff, - }, - } -} - -// LoggerConfig holds options for a particular logger. -// -//nolint:lll -type LoggerConfig struct { - Disable bool `long:"disable" description:"Disable this logger."` - NoTimestamps bool `long:"no-timestamps" description:"Omit timestamps from log lines."` - CallSite string `long:"call-site" description:"Include the call-site of each log line." choice:"off" choice:"short" choice:"long"` -} - -// HandlerOptions returns the set of btclog.HandlerOptions that the state of the -// config struct translates to. -func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption { - opts := []btclog.HandlerOption{ - // The default skip depth used by the logging library is 6 but - // since we wrap the logging handlers with another level of - // abstraction with the handlerSet, we increase the skip depth - // to 7 here. - btclog.WithCallSiteSkipDepth(7), - } - - if cfg.NoTimestamps { - opts = append(opts, btclog.WithNoTimestamp()) - } - - switch cfg.CallSite { - case callSiteShort: - opts = append(opts, btclog.WithCallerFlags(btclog.Lshortfile)) - case callSiteLong: - opts = append(opts, btclog.WithCallerFlags(btclog.Llongfile)) - } - - return opts -} - // consoleLoggerCfg extends the LoggerConfig struct by adding a Color option // which is only available for a console logger. // @@ -88,6 +28,16 @@ type consoleLoggerCfg struct { Style bool `long:"style" description:"If set, the output will be styled with color and fonts"` } +// defaultConsoleLoggerCfg returns the default consoleLoggerCfg for the dev +// console logger. +func defaultConsoleLoggerCfg() *consoleLoggerCfg { + return &consoleLoggerCfg{ + LoggerConfig: LoggerConfig{ + CallSite: callSiteShort, + }, + } +} + // HandlerOptions returns the set of btclog.HandlerOptions that the state of the // config struct translates to. func (cfg *consoleLoggerCfg) HandlerOptions() []btclog.HandlerOption { diff --git a/build/config_prod.go b/build/config_prod.go index 66a0b3144..67a43f8ac 100644 --- a/build/config_prod.go +++ b/build/config_prod.go @@ -3,63 +3,20 @@ package build -import "github.com/btcsuite/btclog/v2" - -const ( - callSiteOff = "off" - callSiteShort = "short" - callSiteLong = "long" -) - -// LogConfig holds logging configuration options. +// consoleLoggerCfg embeds the LoggerConfig struct along with any extensions +// specific to a production deployment. // //nolint:lll -type LogConfig struct { - Console *LoggerConfig `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."` - File *LoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."` +type consoleLoggerCfg struct { + LoggerConfig } -// DefaultLogConfig returns the default logging config options. -func DefaultLogConfig() *LogConfig { - return &LogConfig{ - Console: &LoggerConfig{ - CallSite: callSiteOff, - }, - File: &LoggerConfig{ +// defaultConsoleLoggerCfg returns the default consoleLoggerCfg for the prod +// console logger. +func defaultConsoleLoggerCfg() *consoleLoggerCfg { + return &consoleLoggerCfg{ + LoggerConfig: LoggerConfig{ CallSite: callSiteOff, }, } } - -// LoggerConfig holds options for a particular logger. -// -//nolint:lll -type LoggerConfig struct { - Disable bool `long:"disable" description:"Disable this logger."` - NoTimestamps bool `long:"no-timestamps" description:"Omit timestamps from log lines."` - CallSite string `long:"call-site" description:"Include the call-site of each log line." choice:"off" choice:"short" choice:"long"` -} - -// HandlerOptions returns the set of btclog.HandlerOptions that the state of the -// config struct translates to. -func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption { - opts := []btclog.HandlerOption{ - // The default skip depth used by the logging library is 6 but - // since we wrap the logging handlers with another level of - // abstraction with the handlerSet, we increase the skip depth - // to 7 here. - btclog.WithCallSiteSkipDepth(7), - } - if cfg.NoTimestamps { - opts = append(opts, btclog.WithNoTimestamp()) - } - - switch cfg.CallSite { - case callSiteShort: - opts = append(opts, btclog.WithCallerFlags(btclog.Lshortfile)) - case callSiteLong: - opts = append(opts, btclog.WithCallerFlags(btclog.Llongfile)) - } - - return opts -}