Skip to content

Commit b3c2516

Browse files
committed
build+mod: add zstd compressor support
1 parent 2f71822 commit b3c2516

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
14581458
SetupLoggers(cfg.LogWriter, interceptor)
14591459
err = cfg.LogWriter.InitLogRotator(
14601460
filepath.Join(cfg.LogDir, defaultLogFilename),
1461-
cfg.MaxLogFileSize, cfg.MaxLogFiles,
1461+
cfg.LogCompressor, cfg.MaxLogFileSize, cfg.MaxLogFiles,
14621462
)
14631463
if err != nil {
14641464
str := "log rotation setup failed: %v"

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,8 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
419419
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
420420
github.com/kkdai/bstream v1.0.0 h1:Se5gHwgp2VT2uHfDrkbbgbgEvV9cimLELwrPJctSjg8=
421421
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=
422424
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
423425
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
424426
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=

0 commit comments

Comments
 (0)