From 418f27c86e97be05552d42ccaaab090a326e6d3f Mon Sep 17 00:00:00 2001 From: Jonathan Harvey-Buschel Date: Mon, 26 Aug 2024 18:59:52 -0400 Subject: [PATCH] build+mod: add zstd compressor support --- build/logrotator.go | 28 ++++++++++++++++++++++++++-- config.go | 2 +- go.mod | 1 + go.sum | 2 ++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/build/logrotator.go b/build/logrotator.go index b6e697f9ba0..0684fa3f4bb 100644 --- a/build/logrotator.go +++ b/build/logrotator.go @@ -1,6 +1,7 @@ package build import ( + "compress/gzip" "fmt" "io" "os" @@ -9,6 +10,7 @@ import ( "github.com/btcsuite/btclog" "github.com/jrick/logrotate/rotator" + "github.com/klauspost/compress/zstd" ) // RotatingLogWriter is a wrapper around the LogWriter that supports log file @@ -58,8 +60,8 @@ func (r *RotatingLogWriter) RegisterSubLogger(subsystem string, // 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 string, maxLogFileSize int, - maxLogFiles int) error { +func (r *RotatingLogWriter) InitLogRotator(logFile, logCompressor string, + maxLogFileSize int, maxLogFiles int) error { logDir, _ := filepath.Split(logFile) err := os.MkdirAll(logDir, 0700) @@ -73,6 +75,28 @@ func (r *RotatingLogWriter) InitLogRotator(logFile string, maxLogFileSize int, return fmt.Errorf("failed to create file rotator: %w", err) } + // Reject unknown compressors. + if !SuportedLogCompressor(logCompressor) { + return fmt.Errorf("unknown log compressor: %v", logCompressor) + } + + // The selected compressor was validate by the caller, so we don't need + // to handle an unknown compressor here. + var c rotator.Compressor + switch logCompressor { + case "gzip": + c = gzip.NewWriter(nil) + + case "zstd": + c, err = zstd.NewWriter(nil) + if err != nil { + return fmt.Errorf("failed to create zstd compressor: "+ + "%w", err) + } + } + + r.logRotator.SetCompressor(c, logCompressors[logCompressor]) + // Run rotator as a goroutine now but make sure we catch any errors // that happen in case something with the rotation goes wrong during // runtime (like running out of disk space or not being allowed to diff --git a/config.go b/config.go index d099a670076..925d09312f8 100644 --- a/config.go +++ b/config.go @@ -1458,7 +1458,7 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser, SetupLoggers(cfg.LogWriter, interceptor) err = cfg.LogWriter.InitLogRotator( filepath.Join(cfg.LogDir, defaultLogFilename), - cfg.MaxLogFileSize, cfg.MaxLogFiles, + cfg.LogCompressor, cfg.MaxLogFileSize, cfg.MaxLogFiles, ) if err != nil { str := "log rotation setup failed: %v" diff --git a/go.mod b/go.mod index 649faafe7ff..65a4f991fc4 100644 --- a/go.mod +++ b/go.mod @@ -118,6 +118,7 @@ require ( github.com/json-iterator/go v1.1.11 // indirect github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4 // indirect github.com/juju/testing v0.0.0-20220203020004-a0ff61f03494 // indirect + github.com/klauspost/compress v1.17.9 github.com/lib/pq v1.10.9 // indirect github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index 2206c00e13f..59e0c9faf40 100644 --- a/go.sum +++ b/go.sum @@ -419,6 +419,8 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= github.com/kkdai/bstream v1.0.0 h1:Se5gHwgp2VT2uHfDrkbbgbgEvV9cimLELwrPJctSjg8= github.com/kkdai/bstream v1.0.0/go.mod h1:FDnDOHt5Yx4p3FaHcioFT0QjDOtgUpvjeZqAs+NVZZA= +github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= +github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=