build+mod: add zstd compressor support

This commit is contained in:
Jonathan Harvey-Buschel 2024-08-26 18:59:52 -04:00
parent 2f71822fe0
commit b3c25166ed
No known key found for this signature in database
GPG Key ID: D55307DC71F37212
4 changed files with 29 additions and 3 deletions

View File

@ -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,27 @@ 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)
}
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)
}
}
// Apply the compressor and its file suffix to the log rotator.
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

View File

@ -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"

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=