-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
logging: Switch from lumberjack
to timberjack
, add time-rolling options
#7244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,11 @@ | |
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/DeRuina/timberjack" | ||
"github.com/dustin/go-humanize" | ||
"gopkg.in/natefinch/lumberjack.v2" | ||
|
||
"github.com/caddyserver/caddy/v2" | ||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" | ||
|
@@ -96,6 +98,15 @@ | |
// it will be rotated. | ||
RollSizeMB int `json:"roll_size_mb,omitempty"` | ||
|
||
// Roll log file after some time | ||
RollInterval time.Duration `json:"roll_interval,omitempty` | ||
|
||
// Roll log file at fix minutes | ||
RollAtMinutes []int `json:"roll_minutes,omitempty` | ||
francislavoie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Roll log file at fix time | ||
RollAt []string `json:"roll_at,omitempty` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really understand what the inputs are here. What are the values supposed to be? The godoc comment here should be fleshed out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Answering my own question, it looks something like this in timberjack
francislavoie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Whether to compress rolled files. Default: true | ||
RollCompress *bool `json:"roll_gzip,omitempty"` | ||
|
||
|
@@ -109,6 +120,9 @@ | |
|
||
// How many days to keep rolled log files. Default: 90 | ||
RollKeepDays int `json:"roll_keep_days,omitempty"` | ||
|
||
// Rotated file will have format <logfilename>-<format>-<criterion>.log | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should explain that the default from timberjack is:
Also mention that the |
||
BackupTimeFormat string `json:"backup_time_format,omitempty"` | ||
} | ||
|
||
// CaddyModule returns the Caddy module information. | ||
|
@@ -156,7 +170,7 @@ | |
roll := fw.Roll == nil || *fw.Roll | ||
|
||
// create the file if it does not exist; create with the configured mode, or default | ||
// to restrictive if not set. (lumberjack will reuse the file mode across log rotation) | ||
// to restrictive if not set. (timberjack will reuse the file mode across log rotation) | ||
if err := os.MkdirAll(filepath.Dir(fw.Filename), 0o700); err != nil { | ||
return nil, err | ||
} | ||
|
@@ -166,7 +180,7 @@ | |
} | ||
info, err := file.Stat() | ||
if roll { | ||
file.Close() // lumberjack will reopen it on its own | ||
file.Close() // timberjack will reopen it on its own | ||
} | ||
|
||
// Ensure already existing files have the right mode, since OpenFile will not set the mode in such case. | ||
|
@@ -201,13 +215,17 @@ | |
if fw.RollKeepDays == 0 { | ||
fw.RollKeepDays = 90 | ||
} | ||
return &lumberjack.Logger{ | ||
Filename: fw.Filename, | ||
MaxSize: fw.RollSizeMB, | ||
MaxAge: fw.RollKeepDays, | ||
MaxBackups: fw.RollKeep, | ||
LocalTime: fw.RollLocalTime, | ||
Compress: *fw.RollCompress, | ||
return &timberjack.Logger{ | ||
Filename: fw.Filename, | ||
MaxSize: fw.RollSizeMB, | ||
MaxAge: fw.RollKeepDays, | ||
MaxBackups: fw.RollKeep, | ||
LocalTime: fw.RollLocalTime, | ||
Compress: *fw.RollCompress, | ||
RotationInterval: fw.RollInterval, | ||
RotateAtMinutes: fw.RollAtMinutes, | ||
RotateAt: fw.RollAt, | ||
BackupTimeFormat: fw.BackupTimeFormat, | ||
}, nil | ||
} | ||
|
||
|
@@ -314,6 +332,53 @@ | |
} | ||
fw.RollKeepDays = int(math.Ceil(keepFor.Hours() / 24)) | ||
|
||
case "roll_interval": | ||
var durationStr string | ||
if !d.AllArgs(&durationStr) { | ||
return d.ArgErr() | ||
} | ||
duration, err := time.ParseDuration(durationStr) | ||
if err != nil { | ||
return d.Errf("parsing roll_interval duration: %v", err) | ||
} | ||
fw.RollInterval = duration | ||
|
||
case "roll_minutes": | ||
var minutesArrayStr string | ||
if !d.AllArgs(&minutesArrayStr) { | ||
return d.ArgErr() | ||
} | ||
minutesStr := strings.Split(minutesArrayStr, ",") | ||
minutes := make([]int, len(minutesStr)) | ||
for i := range minutesStr { | ||
ms := strings.Trim(minutesStr[i], " ") | ||
m, err := strconv.Atoi(ms) | ||
if err != nil { | ||
return d.Errf("parsing roll_minutes number: %v", err) | ||
} | ||
minutes[i] = m | ||
} | ||
fw.RollAtMinutes = minutes | ||
|
||
case "roll_at": | ||
var timeArrayStr string | ||
if !d.AllArgs(&timeArrayStr) { | ||
return d.ArgErr() | ||
} | ||
timeStr := strings.Split(timeArrayStr, ",") | ||
times := make([]string, len(timeStr)) | ||
for i := range timeStr { | ||
times[i] = strings.Trim(timeStr[i], " ") | ||
} | ||
fw.RollAt = times | ||
|
||
case "backup_time_format": | ||
var format string | ||
if !d.AllArgs(&format) { | ||
return d.ArgErr() | ||
} | ||
fw.BackupTimeFormat = format | ||
|
||
default: | ||
return d.Errf("unrecognized subdirective '%s'", d.Val()) | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.