diff --git a/build/config.go b/build/config.go new file mode 100644 index 000000000..cee252203 --- /dev/null +++ b/build/config.go @@ -0,0 +1,38 @@ +package build + +import "github.com/btcsuite/btclog/v2" + +// LogConfig holds logging configuration options. +// +//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."` +} + +// DefaultLogConfig returns the default logging config options. +func DefaultLogConfig() *LogConfig { + return &LogConfig{ + Console: &LoggerConfig{}, + File: &LoggerConfig{}, + } +} + +// 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."` +} + +// HandlerOptions returns the set of btclog.HandlerOptions that the state of the +// config struct translates to. +func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption { + var opts []btclog.HandlerOption + if cfg.NoTimestamps { + opts = append(opts, btclog.WithNoTimestamp()) + } + + return opts +} diff --git a/config.go b/config.go index 2f989ec93..d0b72216a 100644 --- a/config.go +++ b/config.go @@ -499,6 +499,7 @@ type Config struct { // hooked up to. SubLogMgr *build.SubLoggerManager LogRotator *build.RotatingLogWriter + LogConfig *build.LogConfig `group:"logging" namespace:"logging"` // networkDir is the path to the directory of the currently active // network. This path will hold the files related to each different @@ -738,6 +739,7 @@ func DefaultConfig() Config { ServerPingTimeout: defaultGrpcServerPingTimeout, ClientPingMinWait: defaultGrpcClientPingMinWait, }, + LogConfig: build.DefaultLogConfig(), WtClient: lncfg.DefaultWtClientCfg(), HTTPHeaderTimeout: DefaultHTTPHeaderTimeout, } @@ -1402,18 +1404,26 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser, lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name), ) - var ( - consoleLogHander = btclog.NewDefaultHandler(os.Stdout) - logHandlers []btclog.Handler + var logCfg = cfg.LogConfig + consoleLogHandler := btclog.NewDefaultHandler( + os.Stdout, logCfg.Console.HandlerOptions()..., ) + 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) + } + } switch build.LoggingType { case build.LogTypeStdOut: - logHandlers = []btclog.Handler{consoleLogHander} + maybeAddLogger(logCfg.Console.Disable, consoleLogHandler) case build.LogTypeDefault: - logHandlers = []btclog.Handler{ - consoleLogHander, - btclog.NewDefaultHandler(cfg.LogRotator), - } + maybeAddLogger(logCfg.Console.Disable, consoleLogHandler) + maybeAddLogger(logCfg.File.Disable, logFileHandler) } if !build.SuportedLogCompressor(cfg.LogCompressor) { diff --git a/sample-lnd.conf b/sample-lnd.conf index b863e60bf..0a2dcfaae 100644 --- a/sample-lnd.conf +++ b/sample-lnd.conf @@ -970,6 +970,19 @@ ; Instructs lnd to encrypt the private key using the wallet's seed. ; tor.encryptkey=false +[logging] + +; Disable logging to stdout and stderror. +; logging.console.disable=false + +; Don't add timestamps to logs written to stdout and stderr. +; logging.console.no-timestamps=false + +; Disable logging to the standard LND log file. +; logging.file.disable=false + +; Don't add timestamps to logs written to the standard LND log file. +; logging.file.no-timestamps=false [watchtower]