11package build
22
33import (
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
0 commit comments