config: add logging config options

This commit adds config options so that users can for both the console
logger and the file logger set the following things:

- disable the logger
- omit timestamps from log lines.
This commit is contained in:
Elle Mouton
2024-09-11 08:24:40 +02:00
parent cd697913ef
commit 5ed7bf1b71
3 changed files with 69 additions and 8 deletions

38
build/config.go Normal file
View File

@@ -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
}

View File

@@ -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) {

View File

@@ -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]