Skip to content

Commit edd9ade

Browse files
authored
Merge pull request lightningnetwork#9017 from jharveyb/log_comress_zstd
log: bump logrotate dep, switch to zstd compressor
2 parents da7f8ae + 0a451b6 commit edd9ade

File tree

7 files changed

+73
-5
lines changed

7 files changed

+73
-5
lines changed

build/log.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ func (t LogType) String() string {
3636
}
3737
}
3838

39+
// Declare the supported log file compressors as exported consts for easier use
40+
// from other projects.
41+
const (
42+
// Gzip is the default compressor.
43+
Gzip = "gzip"
44+
45+
// Zstd is a modern compressor that compresses better than Gzip, in less
46+
// time.
47+
Zstd = "zstd"
48+
)
49+
50+
// logCompressors maps the identifier for each supported compression algorithm
51+
// to the extension used for the compressed log files.
52+
var logCompressors = map[string]string{
53+
Gzip: "gz",
54+
Zstd: "zst",
55+
}
56+
57+
// SuportedLogCompressor returns whether or not logCompressor is a supported
58+
// compression algorithm for log files.
59+
func SuportedLogCompressor(logCompressor string) bool {
60+
_, ok := logCompressors[logCompressor]
61+
62+
return ok
63+
}
64+
3965
// LogWriter is a stub type whose behavior can be changed using the build flags
4066
// "stdlog" and "nolog". The default behavior is to write to both stdout and the
4167
// RotatorPipe. Passing "stdlog" will cause it only to write to stdout, and

build/logrotator.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package build
22

33
import (
4+
"compress/gzip"
45
"fmt"
56
"io"
67
"os"
@@ -9,6 +10,7 @@ import (
910

1011
"github.com/btcsuite/btclog"
1112
"github.com/jrick/logrotate/rotator"
13+
"github.com/klauspost/compress/zstd"
1214
)
1315

1416
// RotatingLogWriter is a wrapper around the LogWriter that supports log file
@@ -58,8 +60,8 @@ func (r *RotatingLogWriter) RegisterSubLogger(subsystem string,
5860
// InitLogRotator initializes the log file rotator to write logs to logFile and
5961
// create roll files in the same directory. It should be called as early on
6062
// startup and possible and must be closed on shutdown by calling `Close`.
61-
func (r *RotatingLogWriter) InitLogRotator(logFile string, maxLogFileSize int,
62-
maxLogFiles int) error {
63+
func (r *RotatingLogWriter) InitLogRotator(logFile, logCompressor string,
64+
maxLogFileSize int, maxLogFiles int) error {
6365

6466
logDir, _ := filepath.Split(logFile)
6567
err := os.MkdirAll(logDir, 0700)
@@ -73,6 +75,27 @@ func (r *RotatingLogWriter) InitLogRotator(logFile string, maxLogFileSize int,
7375
return fmt.Errorf("failed to create file rotator: %w", err)
7476
}
7577

78+
// Reject unknown compressors.
79+
if !SuportedLogCompressor(logCompressor) {
80+
return fmt.Errorf("unknown log compressor: %v", logCompressor)
81+
}
82+
83+
var c rotator.Compressor
84+
switch logCompressor {
85+
case Gzip:
86+
c = gzip.NewWriter(nil)
87+
88+
case Zstd:
89+
c, err = zstd.NewWriter(nil)
90+
if err != nil {
91+
return fmt.Errorf("failed to create zstd compressor: "+
92+
"%w", err)
93+
}
94+
}
95+
96+
// Apply the compressor and its file suffix to the log rotator.
97+
r.logRotator.SetCompressor(c, logCompressors[logCompressor])
98+
7699
// Run rotator as a goroutine now but make sure we catch any errors
77100
// that happen in case something with the rotation goes wrong during
78101
// runtime (like running out of disk space or not being allowed to

config.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const (
5959
defaultLogLevel = "info"
6060
defaultLogDirname = "logs"
6161
defaultLogFilename = "lnd.log"
62+
defaultLogCompressor = build.Gzip
6263
defaultRPCPort = 10009
6364
defaultRESTPort = 8080
6465
defaultPeerPort = 9735
@@ -315,6 +316,7 @@ type Config struct {
315316
ReadMacPath string `long:"readonlymacaroonpath" description:"Path to write the read-only macaroon for lnd's RPC and REST services if it doesn't exist"`
316317
InvoiceMacPath string `long:"invoicemacaroonpath" description:"Path to the invoice-only macaroon for lnd's RPC and REST services if it doesn't exist"`
317318
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"`
318320
MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation)"`
319321
MaxLogFileSize int `long:"maxlogfilesize" description:"Maximum logfile size in MB"`
320322
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 {
560562
LetsEncryptDir: defaultLetsEncryptDir,
561563
LetsEncryptListen: defaultLetsEncryptListen,
562564
LogDir: defaultLogDir,
565+
LogCompressor: defaultLogCompressor,
563566
MaxLogFiles: defaultMaxLogFiles,
564567
MaxLogFileSize: defaultMaxLogFileSize,
565568
AcceptorTimeout: defaultAcceptorTimeout,
@@ -1446,11 +1449,16 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
14461449
os.Exit(0)
14471450
}
14481451

1452+
if !build.SuportedLogCompressor(cfg.LogCompressor) {
1453+
return nil, mkErr("invalid log compressor: %v",
1454+
cfg.LogCompressor)
1455+
}
1456+
14491457
// Initialize logging at the default logging level.
14501458
SetupLoggers(cfg.LogWriter, interceptor)
14511459
err = cfg.LogWriter.InitLogRotator(
14521460
filepath.Join(cfg.LogDir, defaultLogFilename),
1453-
cfg.MaxLogFileSize, cfg.MaxLogFiles,
1461+
cfg.LogCompressor, cfg.MaxLogFileSize, cfg.MaxLogFiles,
14541462
)
14551463
if err != nil {
14561464
str := "log rotation setup failed: %v"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
# Improvements
3737
## Functional Updates
3838

39+
* [Allow](https://github.com/lightningnetwork/lnd/pull/9017) the compression of logs during rotation with ZSTD via the `logcompressor` startup argument.
40+
3941
## RPC Updates
4042

4143
## lncli Updates
@@ -45,6 +47,8 @@
4547
## Breaking Changes
4648
## Performance Improvements
4749

50+
* Log rotation can now use ZSTD
51+
4852
# Technical and Architectural Updates
4953
## BOLT Spec Updates
5054

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require (
2828
github.com/jackpal/go-nat-pmp v0.0.0-20170405195558-28a68d0c24ad
2929
github.com/jedib0t/go-pretty/v6 v6.2.7
3030
github.com/jessevdk/go-flags v1.4.0
31-
github.com/jrick/logrotate v1.0.0
31+
github.com/jrick/logrotate v1.1.2
3232
github.com/kkdai/bstream v1.0.0
3333
github.com/lightninglabs/neutrino v0.16.1-0.20240425105051-602843d34ffd
3434
github.com/lightninglabs/neutrino/cache v1.1.2
@@ -118,6 +118,7 @@ require (
118118
github.com/json-iterator/go v1.1.11 // indirect
119119
github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4 // indirect
120120
github.com/juju/testing v0.0.0-20220203020004-a0ff61f03494 // indirect
121+
github.com/klauspost/compress v1.17.9
121122
github.com/lib/pq v1.10.9 // indirect
122123
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf // indirect
123124
github.com/mattn/go-isatty v0.0.20 // indirect

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,9 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
384384
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
385385
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
386386
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
387-
github.com/jrick/logrotate v1.0.0 h1:lQ1bL/n9mBNeIXoTUoYRlK4dHuNJVofX9oWqBtPnSzI=
388387
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
388+
github.com/jrick/logrotate v1.1.2 h1:6ePk462NCX7TfKtNp5JJ7MbA2YIslkpfgP03TlTYMN0=
389+
github.com/jrick/logrotate v1.1.2/go.mod h1:f9tdWggSVK3iqavGpyvegq5IhNois7KXmasU6/N96OQ=
389390
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
390391
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
391392
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
@@ -418,6 +419,8 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
418419
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
419420
github.com/kkdai/bstream v1.0.0 h1:Se5gHwgp2VT2uHfDrkbbgbgEvV9cimLELwrPJctSjg8=
420421
github.com/kkdai/bstream v1.0.0/go.mod h1:FDnDOHt5Yx4p3FaHcioFT0QjDOtgUpvjeZqAs+NVZZA=
422+
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
423+
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
421424
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
422425
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
423426
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=

sample-lnd.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
; Max log file size in MB before it is rotated.
3737
; maxlogfilesize=10
3838

39+
; Compression algorithm to use when rotating logs.
40+
; logcompressor=gzip
41+
3942
; Time after which an RPCAcceptor will time out and return false if
4043
; it hasn't yet received a response.
4144
; acceptortimeout=15s

0 commit comments

Comments
 (0)