From f360532eb12c92546ec88672508482c5389dab19 Mon Sep 17 00:00:00 2001 From: Jonathan Harvey-Buschel Date: Mon, 26 Aug 2024 18:43:42 -0400 Subject: [PATCH] lnd+build: add logcompressor flag --- build/log.go | 26 ++++++++++++++++++++++++++ config.go | 8 ++++++++ sample-lnd.conf | 3 +++ 3 files changed, 37 insertions(+) diff --git a/build/log.go b/build/log.go index 21c8b953e..484b48e42 100644 --- a/build/log.go +++ b/build/log.go @@ -36,6 +36,32 @@ func (t LogType) String() string { } } +// Declare the supported log file compressors as exported consts for easier use +// from other projects. +const ( + // Gzip is the default compressor. + Gzip = "gzip" + + // Zstd is a modern compressor that compresses better than Gzip, in less + // time. + Zstd = "zstd" +) + +// logCompressors maps the identifier for each supported compression algorithm +// to the extension used for the compressed log files. +var logCompressors = map[string]string{ + Gzip: "gz", + Zstd: "zst", +} + +// SuportedLogCompressor returns whether or not logCompressor is a supported +// compression algorithm for log files. +func SuportedLogCompressor(logCompressor string) bool { + _, ok := logCompressors[logCompressor] + + return ok +} + // LogWriter is a stub type whose behavior can be changed using the build flags // "stdlog" and "nolog". The default behavior is to write to both stdout and the // RotatorPipe. Passing "stdlog" will cause it only to write to stdout, and diff --git a/config.go b/config.go index 34d84720c..dada30ee0 100644 --- a/config.go +++ b/config.go @@ -59,6 +59,7 @@ const ( defaultLogLevel = "info" defaultLogDirname = "logs" defaultLogFilename = "lnd.log" + defaultLogCompressor = build.Gzip defaultRPCPort = 10009 defaultRESTPort = 8080 defaultPeerPort = 9735 @@ -315,6 +316,7 @@ 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"` @@ -560,6 +562,7 @@ func DefaultConfig() Config { LetsEncryptDir: defaultLetsEncryptDir, LetsEncryptListen: defaultLetsEncryptListen, LogDir: defaultLogDir, + LogCompressor: defaultLogCompressor, MaxLogFiles: defaultMaxLogFiles, MaxLogFileSize: defaultMaxLogFileSize, AcceptorTimeout: defaultAcceptorTimeout, @@ -1446,6 +1449,11 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser, os.Exit(0) } + if !build.SuportedLogCompressor(cfg.LogCompressor) { + return nil, mkErr("invalid log compressor: %v", + cfg.LogCompressor) + } + // Initialize logging at the default logging level. SetupLoggers(cfg.LogWriter, interceptor) err = cfg.LogWriter.InitLogRotator( diff --git a/sample-lnd.conf b/sample-lnd.conf index c9fb0e4f8..56d66b595 100644 --- a/sample-lnd.conf +++ b/sample-lnd.conf @@ -36,6 +36,9 @@ ; 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