From 22c13790dfc548f2441ce2aa014fb4419277cf77 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 29 Oct 2024 18:08:08 +0200 Subject: [PATCH] build+config: move maxlogfiles and maxfilesize to logger config Add deprecation notices but continue to read both. --- build/config.go | 15 +++++++++++++-- build/logrotator.go | 14 +++++++------- config.go | 35 +++++++++++++++++++++++++++-------- sample-lnd.conf | 12 +++++++++++- 4 files changed, 58 insertions(+), 18 deletions(-) diff --git a/build/config.go b/build/config.go index 4a27f072c..e26168f80 100644 --- a/build/config.go +++ b/build/config.go @@ -12,6 +12,13 @@ const ( callSiteLong = "long" defaultLogCompressor = Gzip + + // DefaultMaxLogFiles is the default maximum number of log files to + // keep. + DefaultMaxLogFiles = 3 + + // DefaultMaxLogFileSize is the default maximum log file size in MB. + DefaultMaxLogFileSize = 10 ) // LogConfig holds logging configuration options. @@ -46,7 +53,9 @@ func DefaultLogConfig() *LogConfig { return &LogConfig{ Console: defaultConsoleLoggerCfg(), File: &FileLoggerConfig{ - Compressor: defaultLogCompressor, + Compressor: defaultLogCompressor, + MaxLogFiles: DefaultMaxLogFiles, + MaxLogFileSize: DefaultMaxLogFileSize, LoggerConfig: LoggerConfig{ CallSite: callSiteOff, }, @@ -84,5 +93,7 @@ func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption { //nolint:lll type FileLoggerConfig struct { LoggerConfig - Compressor string `long:"compressor" description:"Compression algorithm to use when rotating logs." choice:"gzip" choice:"zstd"` + Compressor string `long:"compressor" description:"Compression algorithm to use when rotating logs." choice:"gzip" choice:"zstd"` + MaxLogFiles int `long:"max-files" description:"Maximum logfiles to keep (0 for no rotation)"` + MaxLogFileSize int `long:"max-file-size" description:"Maximum logfile size in MB"` } diff --git a/build/logrotator.go b/build/logrotator.go index 120e3eaab..b9b16654c 100644 --- a/build/logrotator.go +++ b/build/logrotator.go @@ -31,8 +31,8 @@ func NewRotatingLogWriter() *RotatingLogWriter { // InitLogRotator initializes the log file rotator to write logs to logFile and // create roll files in the same directory. It should be called as early on // startup and possible and must be closed on shutdown by calling `Close`. -func (r *RotatingLogWriter) InitLogRotator(logFile, logCompressor string, - maxLogFileSize int, maxLogFiles int) error { +func (r *RotatingLogWriter) InitLogRotator(cfg *FileLoggerConfig, + logFile string) error { logDir, _ := filepath.Split(logFile) err := os.MkdirAll(logDir, 0700) @@ -41,19 +41,19 @@ func (r *RotatingLogWriter) InitLogRotator(logFile, logCompressor string, } r.rotator, err = rotator.New( - logFile, int64(maxLogFileSize*1024), false, maxLogFiles, + logFile, int64(cfg.MaxLogFileSize*1024), false, cfg.MaxLogFiles, ) if err != nil { return fmt.Errorf("failed to create file rotator: %w", err) } // Reject unknown compressors. - if !SupportedLogCompressor(logCompressor) { - return fmt.Errorf("unknown log compressor: %v", logCompressor) + if !SupportedLogCompressor(cfg.Compressor) { + return fmt.Errorf("unknown log compressor: %v", cfg.Compressor) } var c rotator.Compressor - switch logCompressor { + switch cfg.Compressor { case Gzip: c = gzip.NewWriter(nil) @@ -66,7 +66,7 @@ func (r *RotatingLogWriter) InitLogRotator(logFile, logCompressor string, } // Apply the compressor and its file suffix to the log rotator. - r.rotator.SetCompressor(c, logCompressors[logCompressor]) + r.rotator.SetCompressor(c, logCompressors[cfg.Compressor]) // Run rotator as a goroutine now but make sure we catch any errors // that happen in case something with the rotation goes wrong during diff --git a/config.go b/config.go index f9192aadc..5ac4dc307 100644 --- a/config.go +++ b/config.go @@ -71,8 +71,6 @@ const ( defaultChanEnableTimeout = 19 * time.Minute defaultChanDisableTimeout = 20 * time.Minute defaultHeightHintCacheQueryDisable = false - defaultMaxLogFiles = 3 - defaultMaxLogFileSize = 10 defaultMinBackoff = time.Second defaultMaxBackoff = time.Hour defaultLetsEncryptDirname = "letsencrypt" @@ -315,8 +313,8 @@ 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."` - MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation)"` - MaxLogFileSize int `long:"maxlogfilesize" description:"Maximum logfile size in MB"` + MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation). DEPRECATED: use --logging.file.max-files instead" hidden:"true"` + MaxLogFileSize int `long:"maxlogfilesize" description:"Maximum logfile size in MB. DEPRECATED: use --logging.file.max-file-size instead" hidden:"true"` 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"` LetsEncryptDir string `long:"letsencryptdir" description:"The directory to store Let's Encrypt certificates within"` @@ -562,8 +560,8 @@ func DefaultConfig() Config { LetsEncryptDir: defaultLetsEncryptDir, LetsEncryptListen: defaultLetsEncryptListen, LogDir: defaultLogDir, - MaxLogFiles: defaultMaxLogFiles, - MaxLogFileSize: defaultMaxLogFileSize, + MaxLogFiles: build.DefaultMaxLogFiles, + MaxLogFileSize: build.DefaultMaxLogFileSize, AcceptorTimeout: defaultAcceptorTimeout, WSPingInterval: lnrpc.DefaultPingInterval, WSPongWait: lnrpc.DefaultPongWait, @@ -1417,10 +1415,31 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser, cfg.SubLogMgr.SupportedSubsystems()) os.Exit(0) } + + if cfg.MaxLogFiles != build.DefaultMaxLogFiles { + if cfg.LogConfig.File.MaxLogFiles != + build.DefaultMaxLogFiles { + + return nil, mkErr("cannot set both maxlogfiles and "+ + "logging.file.max-files", err) + } + + cfg.LogConfig.File.MaxLogFiles = cfg.MaxLogFiles + } + if cfg.MaxLogFileSize != build.DefaultMaxLogFileSize { + if cfg.LogConfig.File.MaxLogFileSize != + build.DefaultMaxLogFileSize { + + return nil, mkErr("cannot set both maxlogfilesize and "+ + "logging.file.max-file-size", err) + } + + cfg.LogConfig.File.MaxLogFileSize = cfg.MaxLogFileSize + } + err = cfg.LogRotator.InitLogRotator( + cfg.LogConfig.File, filepath.Join(cfg.LogDir, defaultLogFilename), - cfg.LogConfig.File.Compressor, cfg.MaxLogFileSize, - cfg.MaxLogFiles, ) if err != nil { str := "log rotation setup failed: %v" diff --git a/sample-lnd.conf b/sample-lnd.conf index b9aee40e5..66a97b70f 100644 --- a/sample-lnd.conf +++ b/sample-lnd.conf @@ -30,9 +30,12 @@ ; Rotated logs are compressed in place. ; logdir=~/.lnd/logs -; Number of logfiles that the log rotation should keep. Setting it to 0 disables deletion of old log files. +; DEPRECATED: Use logging.file.max-files instead. +; Number of logfiles that the log rotation should keep. Setting it to 0 disables +; deletion of old log files. ; maxlogfiles=3 ; +; DEPRECATED: Use logging.file.max-file-size instead. ; Max log file size in MB before it is rotated. ; maxlogfilesize=10 @@ -985,6 +988,13 @@ ; Disable logging to the standard LND log file. ; logging.file.disable=false +; Number of log files that the log rotation should keep. Setting +; it to 0 disables deletion of old log files. +; logging.file.max-files=3 + +; Max log file size in MB before it is rotated. +; logging.file.max-file-size=10 + ; Compression algorithm to use when rotating logs. ; Default: ; logging.file.compressor=gzip