Skip to content

Commit 599ae1a

Browse files
committed
logging: Support zstd roll compression
1 parent 294dfff commit 599ae1a

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

caddytest/integration/caddyfile_adapt/log_roll_days.caddyfiletest

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ log one {
66
dir_mode 0755
77
roll_size 1gb
88
roll_uncompressed
9+
roll_compression none
910
roll_local_time
1011
roll_keep 5
1112
roll_keep_for 90d
@@ -16,6 +17,7 @@ log two {
1617
mode 0777
1718
dir_mode from_file
1819
roll_size 1gib
20+
roll_compression zstd
1921
roll_interval 12h
2022
roll_at 00:00 06:00 12:00,18:00
2123
roll_minutes 10 40 45,46
@@ -39,6 +41,7 @@ log two {
3941
"filename": "/var/log/access.log",
4042
"mode": "0644",
4143
"output": "file",
44+
"roll_compression": "none",
4245
"roll_gzip": false,
4346
"roll_keep": 5,
4447
"roll_keep_days": 90,
@@ -61,6 +64,7 @@ log two {
6164
"12:00",
6265
"18:00"
6366
],
67+
"roll_compression": "zstd",
6468
"roll_interval": 43200000000000,
6569
"roll_keep": 10,
6670
"roll_keep_days": 90,

modules/logging/filewriter.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,16 @@ type FileWriter struct {
122122
// See https://github.com/DeRuina/timberjack#%EF%B8%8F-rotation-notes--warnings for caveats
123123
RollAt []string `json:"roll_at,omitempty"`
124124

125-
// Whether to compress rolled files. Default: true
125+
// Whether to compress rolled files.
126+
// Default: true.
127+
// Deprecated: Use RollCompression instead, setting it to "none".
126128
RollCompress *bool `json:"roll_gzip,omitempty"`
127129

130+
// RollCompression selects the compression algorithm for rolled files.
131+
// Accepted values: "none", "gzip", "zstd".
132+
// Default: gzip
133+
RollCompression string `json:"roll_compression,omitempty"`
134+
128135
// Whether to use local timestamps in rolled filenames.
129136
// Default: false
130137
RollLocalTime bool `json:"roll_local_time,omitempty"`
@@ -254,13 +261,32 @@ func (fw FileWriter) OpenWriter() (io.WriteCloser, error) {
254261
if fw.RollKeepDays == 0 {
255262
fw.RollKeepDays = 90
256263
}
264+
265+
// Determine compression algorithm to use. Priority:
266+
// 1) explicit RollCompression (none|gzip|zstd)
267+
// 2) if RollCompress is unset or true -> gzip
268+
// 3) if RollCompress is false -> none
269+
var compression string
270+
if fw.RollCompression != "" {
271+
compression = strings.ToLower(strings.TrimSpace(fw.RollCompression))
272+
if compression != "none" && compression != "gzip" && compression != "zstd" {
273+
return nil, fmt.Errorf("invalid roll_compression: %s", fw.RollCompression)
274+
}
275+
} else {
276+
if fw.RollCompress == nil || *fw.RollCompress {
277+
compression = "gzip"
278+
} else {
279+
compression = "none"
280+
}
281+
}
282+
257283
return &timberjack.Logger{
258284
Filename: fw.Filename,
259285
MaxSize: fw.RollSizeMB,
260286
MaxAge: fw.RollKeepDays,
261287
MaxBackups: fw.RollKeep,
262288
LocalTime: fw.RollLocalTime,
263-
Compress: *fw.RollCompress,
289+
Compression: compression,
264290
RotationInterval: fw.RollInterval,
265291
RotateAtMinutes: fw.RollAtMinutes,
266292
RotateAt: fw.RollAt,
@@ -332,6 +358,7 @@ func mkdirAllFromFile(dir string, fileMode os.FileMode) error {
332358
// roll_disabled
333359
// roll_size <size>
334360
// roll_uncompressed
361+
// roll_compression <none|gzip|zstd>
335362
// roll_local_time
336363
// roll_keep <num>
337364
// roll_keep_for <days>
@@ -413,6 +440,19 @@ func (fw *FileWriter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
413440
return d.ArgErr()
414441
}
415442

443+
case "roll_compression":
444+
var comp string
445+
if !d.AllArgs(&comp) {
446+
return d.ArgErr()
447+
}
448+
comp = strings.ToLower(strings.TrimSpace(comp))
449+
switch comp {
450+
case "none", "gzip", "zstd":
451+
fw.RollCompression = comp
452+
default:
453+
return d.Errf("parsing roll_compression: must be 'none', 'gzip' or 'zstd'")
454+
}
455+
416456
case "roll_local_time":
417457
fw.RollLocalTime = true
418458
if d.NextArg() {

0 commit comments

Comments
 (0)