Skip to content

Commit 684041b

Browse files
authored
Merge pull request #9233 from ellemouton/moveLoggingCompressorOption
build+config: move file logger specific options to `logging.file`
2 parents 22c3cc5 + e547d21 commit 684041b

File tree

8 files changed

+149
-104
lines changed

8 files changed

+149
-104
lines changed

build/config.go

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
1-
//go:build !dev
2-
// +build !dev
3-
41
package build
52

6-
import "github.com/btcsuite/btclog/v2"
3+
import (
4+
"fmt"
5+
6+
"github.com/btcsuite/btclog/v2"
7+
)
78

89
const (
910
callSiteOff = "off"
1011
callSiteShort = "short"
1112
callSiteLong = "long"
13+
14+
defaultLogCompressor = Gzip
15+
16+
// DefaultMaxLogFiles is the default maximum number of log files to
17+
// keep.
18+
DefaultMaxLogFiles = 10
19+
20+
// DefaultMaxLogFileSize is the default maximum log file size in MB.
21+
DefaultMaxLogFileSize = 20
1222
)
1323

1424
// LogConfig holds logging configuration options.
1525
//
1626
//nolint:lll
1727
type LogConfig struct {
18-
Console *LoggerConfig `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."`
19-
File *LoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."`
28+
Console *consoleLoggerCfg `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."`
29+
File *FileLoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."`
2030
}
2131

22-
// DefaultLogConfig returns the default logging config options.
23-
func DefaultLogConfig() *LogConfig {
24-
return &LogConfig{
25-
Console: &LoggerConfig{
26-
CallSite: callSiteOff,
27-
},
28-
File: &LoggerConfig{
29-
CallSite: callSiteOff,
30-
},
32+
// Validate validates the LogConfig struct values.
33+
func (c *LogConfig) Validate() error {
34+
if !SupportedLogCompressor(c.File.Compressor) {
35+
return fmt.Errorf("invalid log compressor: %v",
36+
c.File.Compressor)
3137
}
38+
39+
return nil
3240
}
3341

3442
// LoggerConfig holds options for a particular logger.
@@ -40,6 +48,21 @@ type LoggerConfig struct {
4048
CallSite string `long:"call-site" description:"Include the call-site of each log line." choice:"off" choice:"short" choice:"long"`
4149
}
4250

51+
// DefaultLogConfig returns the default logging config options.
52+
func DefaultLogConfig() *LogConfig {
53+
return &LogConfig{
54+
Console: defaultConsoleLoggerCfg(),
55+
File: &FileLoggerConfig{
56+
Compressor: defaultLogCompressor,
57+
MaxLogFiles: DefaultMaxLogFiles,
58+
MaxLogFileSize: DefaultMaxLogFileSize,
59+
LoggerConfig: LoggerConfig{
60+
CallSite: callSiteOff,
61+
},
62+
},
63+
}
64+
}
65+
4366
// HandlerOptions returns the set of btclog.HandlerOptions that the state of the
4467
// config struct translates to.
4568
func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption {
@@ -50,6 +73,7 @@ func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption {
5073
// to 7 here.
5174
btclog.WithCallSiteSkipDepth(7),
5275
}
76+
5377
if cfg.NoTimestamps {
5478
opts = append(opts, btclog.WithNoTimestamp())
5579
}
@@ -63,3 +87,13 @@ func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption {
6387

6488
return opts
6589
}
90+
91+
// FileLoggerConfig extends LoggerConfig with specific log file options.
92+
//
93+
//nolint:lll
94+
type FileLoggerConfig struct {
95+
LoggerConfig
96+
Compressor string `long:"compressor" description:"Compression algorithm to use when rotating logs." choice:"gzip" choice:"zstd"`
97+
MaxLogFiles int `long:"max-files" description:"Maximum logfiles to keep (0 for no rotation)"`
98+
MaxLogFileSize int `long:"max-file-size" description:"Maximum logfile size in MB"`
99+
}

build/config_dev.go

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,8 @@ const (
1717
faintSeq = "2"
1818
esc = '\x1b'
1919
csi = string(esc) + "["
20-
21-
callSiteOff = "off"
22-
callSiteShort = "short"
23-
callSiteLong = "long"
2420
)
2521

26-
// LogConfig holds logging configuration options.
27-
//
28-
//nolint:lll
29-
type LogConfig struct {
30-
Console *consoleLoggerCfg `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."`
31-
File *LoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."`
32-
}
33-
34-
// DefaultLogConfig returns the default logging config options.
35-
func DefaultLogConfig() *LogConfig {
36-
return &LogConfig{
37-
Console: &consoleLoggerCfg{
38-
LoggerConfig: LoggerConfig{
39-
CallSite: callSiteShort,
40-
},
41-
},
42-
File: &LoggerConfig{
43-
CallSite: callSiteOff,
44-
},
45-
}
46-
}
47-
48-
// LoggerConfig holds options for a particular logger.
49-
//
50-
//nolint:lll
51-
type LoggerConfig struct {
52-
Disable bool `long:"disable" description:"Disable this logger."`
53-
NoTimestamps bool `long:"no-timestamps" description:"Omit timestamps from log lines."`
54-
CallSite string `long:"call-site" description:"Include the call-site of each log line." choice:"off" choice:"short" choice:"long"`
55-
}
56-
57-
// HandlerOptions returns the set of btclog.HandlerOptions that the state of the
58-
// config struct translates to.
59-
func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption {
60-
opts := []btclog.HandlerOption{
61-
// The default skip depth used by the logging library is 6 but
62-
// since we wrap the logging handlers with another level of
63-
// abstraction with the handlerSet, we increase the skip depth
64-
// to 7 here.
65-
btclog.WithCallSiteSkipDepth(7),
66-
}
67-
68-
if cfg.NoTimestamps {
69-
opts = append(opts, btclog.WithNoTimestamp())
70-
}
71-
72-
switch cfg.CallSite {
73-
case callSiteShort:
74-
opts = append(opts, btclog.WithCallerFlags(btclog.Lshortfile))
75-
case callSiteLong:
76-
opts = append(opts, btclog.WithCallerFlags(btclog.Llongfile))
77-
}
78-
79-
return opts
80-
}
81-
8222
// consoleLoggerCfg extends the LoggerConfig struct by adding a Color option
8323
// which is only available for a console logger.
8424
//
@@ -88,6 +28,16 @@ type consoleLoggerCfg struct {
8828
Style bool `long:"style" description:"If set, the output will be styled with color and fonts"`
8929
}
9030

31+
// defaultConsoleLoggerCfg returns the default consoleLoggerCfg for the dev
32+
// console logger.
33+
func defaultConsoleLoggerCfg() *consoleLoggerCfg {
34+
return &consoleLoggerCfg{
35+
LoggerConfig: LoggerConfig{
36+
CallSite: callSiteShort,
37+
},
38+
}
39+
}
40+
9141
// HandlerOptions returns the set of btclog.HandlerOptions that the state of the
9242
// config struct translates to.
9343
func (cfg *consoleLoggerCfg) HandlerOptions() []btclog.HandlerOption {

build/config_prod.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build !dev
2+
// +build !dev
3+
4+
package build
5+
6+
// consoleLoggerCfg embeds the LoggerConfig struct along with any extensions
7+
// specific to a production deployment.
8+
//
9+
//nolint:lll
10+
type consoleLoggerCfg struct {
11+
LoggerConfig
12+
}
13+
14+
// defaultConsoleLoggerCfg returns the default consoleLoggerCfg for the prod
15+
// console logger.
16+
func defaultConsoleLoggerCfg() *consoleLoggerCfg {
17+
return &consoleLoggerCfg{
18+
LoggerConfig: LoggerConfig{
19+
CallSite: callSiteOff,
20+
},
21+
}
22+
}

build/log.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ var logCompressors = map[string]string{
5252
Zstd: "zst",
5353
}
5454

55-
// SuportedLogCompressor returns whether or not logCompressor is a supported
55+
// SupportedLogCompressor returns whether or not logCompressor is a supported
5656
// compression algorithm for log files.
57-
func SuportedLogCompressor(logCompressor string) bool {
57+
func SupportedLogCompressor(logCompressor string) bool {
5858
_, ok := logCompressors[logCompressor]
5959

6060
return ok

build/logrotator.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func NewRotatingLogWriter() *RotatingLogWriter {
3131
// InitLogRotator initializes the log file rotator to write logs to logFile and
3232
// create roll files in the same directory. It should be called as early on
3333
// startup and possible and must be closed on shutdown by calling `Close`.
34-
func (r *RotatingLogWriter) InitLogRotator(logFile, logCompressor string,
35-
maxLogFileSize int, maxLogFiles int) error {
34+
func (r *RotatingLogWriter) InitLogRotator(cfg *FileLoggerConfig,
35+
logFile string) error {
3636

3737
logDir, _ := filepath.Split(logFile)
3838
err := os.MkdirAll(logDir, 0700)
@@ -41,19 +41,19 @@ func (r *RotatingLogWriter) InitLogRotator(logFile, logCompressor string,
4141
}
4242

4343
r.rotator, err = rotator.New(
44-
logFile, int64(maxLogFileSize*1024), false, maxLogFiles,
44+
logFile, int64(cfg.MaxLogFileSize*1024), false, cfg.MaxLogFiles,
4545
)
4646
if err != nil {
4747
return fmt.Errorf("failed to create file rotator: %w", err)
4848
}
4949

5050
// Reject unknown compressors.
51-
if !SuportedLogCompressor(logCompressor) {
52-
return fmt.Errorf("unknown log compressor: %v", logCompressor)
51+
if !SupportedLogCompressor(cfg.Compressor) {
52+
return fmt.Errorf("unknown log compressor: %v", cfg.Compressor)
5353
}
5454

5555
var c rotator.Compressor
56-
switch logCompressor {
56+
switch cfg.Compressor {
5757
case Gzip:
5858
c = gzip.NewWriter(nil)
5959

@@ -66,7 +66,7 @@ func (r *RotatingLogWriter) InitLogRotator(logFile, logCompressor string,
6666
}
6767

6868
// Apply the compressor and its file suffix to the log rotator.
69-
r.rotator.SetCompressor(c, logCompressors[logCompressor])
69+
r.rotator.SetCompressor(c, logCompressors[cfg.Compressor])
7070

7171
// Run rotator as a goroutine now but make sure we catch any errors
7272
// that happen in case something with the rotation goes wrong during

config.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ const (
5959
defaultLogLevel = "info"
6060
defaultLogDirname = "logs"
6161
defaultLogFilename = "lnd.log"
62-
defaultLogCompressor = build.Gzip
6362
defaultRPCPort = 10009
6463
defaultRESTPort = 8080
6564
defaultPeerPort = 9735
@@ -72,8 +71,6 @@ const (
7271
defaultChanEnableTimeout = 19 * time.Minute
7372
defaultChanDisableTimeout = 20 * time.Minute
7473
defaultHeightHintCacheQueryDisable = false
75-
defaultMaxLogFiles = 3
76-
defaultMaxLogFileSize = 10
7774
defaultMinBackoff = time.Second
7875
defaultMaxBackoff = time.Hour
7976
defaultLetsEncryptDirname = "letsencrypt"
@@ -316,9 +313,8 @@ type Config struct {
316313
ReadMacPath string `long:"readonlymacaroonpath" description:"Path to write the read-only macaroon for lnd's RPC and REST services if it doesn't exist"`
317314
InvoiceMacPath string `long:"invoicemacaroonpath" description:"Path to the invoice-only macaroon for lnd's RPC and REST services if it doesn't exist"`
318315
LogDir string `long:"logdir" description:"Directory to log output."`
319-
LogCompressor string `long:"logcompressor" description:"Compression algorithm to use when rotating logs." choice:"gzip" choice:"zstd"`
320-
MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation)"`
321-
MaxLogFileSize int `long:"maxlogfilesize" description:"Maximum logfile size in MB"`
316+
MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation). DEPRECATED: use --logging.file.max-files instead" hidden:"true"`
317+
MaxLogFileSize int `long:"maxlogfilesize" description:"Maximum logfile size in MB. DEPRECATED: use --logging.file.max-file-size instead" hidden:"true"`
322318
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"`
323319

324320
LetsEncryptDir string `long:"letsencryptdir" description:"The directory to store Let's Encrypt certificates within"`
@@ -564,9 +560,8 @@ func DefaultConfig() Config {
564560
LetsEncryptDir: defaultLetsEncryptDir,
565561
LetsEncryptListen: defaultLetsEncryptListen,
566562
LogDir: defaultLogDir,
567-
LogCompressor: defaultLogCompressor,
568-
MaxLogFiles: defaultMaxLogFiles,
569-
MaxLogFileSize: defaultMaxLogFileSize,
563+
MaxLogFiles: build.DefaultMaxLogFiles,
564+
MaxLogFileSize: build.DefaultMaxLogFileSize,
570565
AcceptorTimeout: defaultAcceptorTimeout,
571566
WSPingInterval: lnrpc.DefaultPingInterval,
572567
WSPongWait: lnrpc.DefaultPongWait,
@@ -1403,9 +1398,8 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
14031398
lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
14041399
)
14051400

1406-
if !build.SuportedLogCompressor(cfg.LogCompressor) {
1407-
return nil, mkErr("invalid log compressor: %v",
1408-
cfg.LogCompressor)
1401+
if err := cfg.LogConfig.Validate(); err != nil {
1402+
return nil, mkErr("error validating logging config: %w", err)
14091403
}
14101404

14111405
cfg.SubLogMgr = build.NewSubLoggerManager(build.NewDefaultLogHandlers(
@@ -1421,9 +1415,31 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
14211415
cfg.SubLogMgr.SupportedSubsystems())
14221416
os.Exit(0)
14231417
}
1418+
1419+
if cfg.MaxLogFiles != build.DefaultMaxLogFiles {
1420+
if cfg.LogConfig.File.MaxLogFiles !=
1421+
build.DefaultMaxLogFiles {
1422+
1423+
return nil, mkErr("cannot set both maxlogfiles and "+
1424+
"logging.file.max-files", err)
1425+
}
1426+
1427+
cfg.LogConfig.File.MaxLogFiles = cfg.MaxLogFiles
1428+
}
1429+
if cfg.MaxLogFileSize != build.DefaultMaxLogFileSize {
1430+
if cfg.LogConfig.File.MaxLogFileSize !=
1431+
build.DefaultMaxLogFileSize {
1432+
1433+
return nil, mkErr("cannot set both maxlogfilesize and "+
1434+
"logging.file.max-file-size", err)
1435+
}
1436+
1437+
cfg.LogConfig.File.MaxLogFileSize = cfg.MaxLogFileSize
1438+
}
1439+
14241440
err = cfg.LogRotator.InitLogRotator(
1441+
cfg.LogConfig.File,
14251442
filepath.Join(cfg.LogDir, defaultLogFilename),
1426-
cfg.LogCompressor, cfg.MaxLogFileSize, cfg.MaxLogFiles,
14271443
)
14281444
if err != nil {
14291445
str := "log rotation setup failed: %v"

docs/release-notes/release-notes-0.19.0.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
# Improvements
5858
## Functional Updates
5959

60-
* [Allow](https://github.com/lightningnetwork/lnd/pull/9017) the compression of logs during rotation with ZSTD via the `logcompressor` startup argument.
60+
* [Allow](https://github.com/lightningnetwork/lnd/pull/9017) the compression of
61+
logs during rotation with ZSTD via the `logging.file.compressor` startup
62+
argument.
6163

6264
* The SCB file now [contains more data][https://github.com/lightningnetwork/lnd/pull/8183]
6365
that enable a last resort rescue for certain cases where the peer is no longer
@@ -88,6 +90,14 @@
8890
Finally, the new `--logging.console.style` option can be used under the `dev`
8991
build tag to add styling to console logging.
9092

93+
* [Add max files and max file size](https://github.com/lightningnetwork/lnd/pull/9233)
94+
options to the `logging` config namespace under new `--logging.file.max-files`
95+
and `--logging.files.max-file-size` options. The old options (`--maxlogfiles`
96+
and `--maxlogfilesize`) will still work but deprecation notices have been
97+
added and they will be removed in a future release. The defaults values for
98+
these options have also been increased from max 3 log files to 10 and from
99+
max 10 MB to 20 MB.
100+
91101
## Breaking Changes
92102
## Performance Improvements
93103

0 commit comments

Comments
 (0)