-
-
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
Open
aeris
wants to merge
5
commits into
caddyserver:master
Choose a base branch
from
aeris:timberjack-rotate-at
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+80
−13
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1af339c
Move to timberjack and add rotate-at methods
aeris bc350e7
Update modules/logging/filewriter.go
francislavoie 3852440
Update modules/logging/filewriter.go
francislavoie f228474
Update modules/logging/filewriter.go
francislavoie 310f7df
Merge branch 'master' into timberjack-rotate-at
mholt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,11 @@ import ( | |
"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 @@ type FileWriter struct { | |
// 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_at_minutes,omitempty"` | ||
|
||
// Roll log file at fix time | ||
RollAt []string `json:"roll_at,omitempty"` | ||
|
||
// Whether to compress rolled files. Default: true | ||
RollCompress *bool `json:"roll_gzip,omitempty"` | ||
|
||
|
@@ -109,6 +120,9 @@ type FileWriter struct { | |
|
||
// 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 @@ func (fw FileWriter) OpenWriter() (io.WriteCloser, error) { | |
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 @@ func (fw FileWriter) OpenWriter() (io.WriteCloser, error) { | |
} | ||
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 @@ func (fw FileWriter) OpenWriter() (io.WriteCloser, error) { | |
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 @@ func (fw *FileWriter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { | |
} | ||
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()) | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These godoc comments should cover some of the caveats mentioned in the timberjack README https://github.com/DeRuina/timberjack#%EF%B8%8F-rotation-notes--warnings (and eventually also in the Caddy website docs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also subtleties like using either of RollAt/RollAtMinutes will use a background goroutine to trigger rotation, whereas RollInterval triggers a check on each log write whether the interval has been crossed and causes a rotation at that point (no goroutine).