diff --git a/build/config.go b/build/config.go index fcb363987..4a27f072c 100644 --- a/build/config.go +++ b/build/config.go @@ -1,11 +1,17 @@ package build -import "github.com/btcsuite/btclog/v2" +import ( + "fmt" + + "github.com/btcsuite/btclog/v2" +) const ( callSiteOff = "off" callSiteShort = "short" callSiteLong = "long" + + defaultLogCompressor = Gzip ) // LogConfig holds logging configuration options. @@ -16,6 +22,16 @@ type LogConfig struct { File *FileLoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."` } +// Validate validates the LogConfig struct values. +func (c *LogConfig) Validate() error { + if !SupportedLogCompressor(c.File.Compressor) { + return fmt.Errorf("invalid log compressor: %v", + c.File.Compressor) + } + + return nil +} + // LoggerConfig holds options for a particular logger. // //nolint:lll @@ -30,6 +46,7 @@ func DefaultLogConfig() *LogConfig { return &LogConfig{ Console: defaultConsoleLoggerCfg(), File: &FileLoggerConfig{ + Compressor: defaultLogCompressor, LoggerConfig: LoggerConfig{ CallSite: callSiteOff, }, @@ -63,6 +80,9 @@ func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption { } // FileLoggerConfig extends LoggerConfig with specific log file options. +// +//nolint:lll type FileLoggerConfig struct { LoggerConfig + Compressor string `long:"compressor" description:"Compression algorithm to use when rotating logs." choice:"gzip" choice:"zstd"` } diff --git a/build/log.go b/build/log.go index 568f9f618..37751ab46 100644 --- a/build/log.go +++ b/build/log.go @@ -52,9 +52,9 @@ var logCompressors = map[string]string{ Zstd: "zst", } -// SuportedLogCompressor returns whether or not logCompressor is a supported +// SupportedLogCompressor returns whether or not logCompressor is a supported // compression algorithm for log files. -func SuportedLogCompressor(logCompressor string) bool { +func SupportedLogCompressor(logCompressor string) bool { _, ok := logCompressors[logCompressor] return ok diff --git a/build/logrotator.go b/build/logrotator.go index 2ae105fde..120e3eaab 100644 --- a/build/logrotator.go +++ b/build/logrotator.go @@ -48,7 +48,7 @@ func (r *RotatingLogWriter) InitLogRotator(logFile, logCompressor string, } // Reject unknown compressors. - if !SuportedLogCompressor(logCompressor) { + if !SupportedLogCompressor(logCompressor) { return fmt.Errorf("unknown log compressor: %v", logCompressor) } diff --git a/config.go b/config.go index c8991cf60..f9192aadc 100644 --- a/config.go +++ b/config.go @@ -59,7 +59,6 @@ const ( defaultLogLevel = "info" defaultLogDirname = "logs" defaultLogFilename = "lnd.log" - defaultLogCompressor = build.Gzip defaultRPCPort = 10009 defaultRESTPort = 8080 defaultPeerPort = 9735 @@ -316,7 +315,6 @@ type Config struct { ReadMacPath string `long:"readonlymacaroonpath" description:"Path to write the read-only macaroon for lnd's RPC and REST services if it doesn't exist"` InvoiceMacPath string `long:"invoicemacaroonpath" description:"Path to the invoice-only macaroon for lnd's RPC and REST services if it doesn't exist"` LogDir string `long:"logdir" description:"Directory to log output."` - LogCompressor string `long:"logcompressor" description:"Compression algorithm to use when rotating logs." choice:"gzip" choice:"zstd"` MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation)"` MaxLogFileSize int `long:"maxlogfilesize" description:"Maximum logfile size in MB"` AcceptorTimeout time.Duration `long:"acceptortimeout" description:"Time after which an RPCAcceptor will time out and return false if it hasn't yet received a response"` @@ -564,7 +562,6 @@ func DefaultConfig() Config { LetsEncryptDir: defaultLetsEncryptDir, LetsEncryptListen: defaultLetsEncryptListen, LogDir: defaultLogDir, - LogCompressor: defaultLogCompressor, MaxLogFiles: defaultMaxLogFiles, MaxLogFileSize: defaultMaxLogFileSize, AcceptorTimeout: defaultAcceptorTimeout, @@ -1403,9 +1400,8 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser, lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name), ) - if !build.SuportedLogCompressor(cfg.LogCompressor) { - return nil, mkErr("invalid log compressor: %v", - cfg.LogCompressor) + if err := cfg.LogConfig.Validate(); err != nil { + return nil, mkErr("error validating logging config: %w", err) } cfg.SubLogMgr = build.NewSubLoggerManager(build.NewDefaultLogHandlers( @@ -1423,7 +1419,8 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser, } err = cfg.LogRotator.InitLogRotator( filepath.Join(cfg.LogDir, defaultLogFilename), - cfg.LogCompressor, cfg.MaxLogFileSize, cfg.MaxLogFiles, + cfg.LogConfig.File.Compressor, cfg.MaxLogFileSize, + cfg.MaxLogFiles, ) if err != nil { str := "log rotation setup failed: %v" diff --git a/docs/release-notes/release-notes-0.19.0.md b/docs/release-notes/release-notes-0.19.0.md index 3b20a054e..5116b4ba8 100644 --- a/docs/release-notes/release-notes-0.19.0.md +++ b/docs/release-notes/release-notes-0.19.0.md @@ -57,7 +57,9 @@ # Improvements ## Functional Updates -* [Allow](https://github.com/lightningnetwork/lnd/pull/9017) the compression of logs during rotation with ZSTD via the `logcompressor` startup argument. +* [Allow](https://github.com/lightningnetwork/lnd/pull/9017) the compression of + logs during rotation with ZSTD via the `logging.file.compressor` startup + argument. * The SCB file now [contains more data][https://github.com/lightningnetwork/lnd/pull/8183] that enable a last resort rescue for certain cases where the peer is no longer diff --git a/sample-lnd.conf b/sample-lnd.conf index eeb6a9d31..b9aee40e5 100644 --- a/sample-lnd.conf +++ b/sample-lnd.conf @@ -36,9 +36,6 @@ ; Max log file size in MB before it is rotated. ; maxlogfilesize=10 -; Compression algorithm to use when rotating logs. -; logcompressor=gzip - ; Time after which an RPCAcceptor will time out and return false if ; it hasn't yet received a response. ; acceptortimeout=15s @@ -988,6 +985,12 @@ ; Disable logging to the standard LND log file. ; logging.file.disable=false +; Compression algorithm to use when rotating logs. +; Default: +; logging.file.compressor=gzip +; Example: +; logging.file.compressor=zstd + ; Don't add timestamps to logs written to the standard LND log file. ; logging.file.no-timestamps=false