-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #497 from mwdomino/rotate-file
add support for rotating file outputs
- Loading branch information
Showing
3 changed files
with
101 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package file | ||
|
||
import ( | ||
"gopkg.in/natefinch/lumberjack.v2" | ||
) | ||
|
||
// RotationConfig manages configuration around file rotation | ||
type rotationConfig struct { | ||
MaxSize int `mapstructure:"max-size,omitempty"` | ||
MaxBackups int `mapstructure:"max-backups,omitempty"` | ||
MaxAge int `mapstructure:"max-age,omitempty"` | ||
Compress bool `mapstructure:"compress,omitempty"` | ||
} | ||
|
||
func (r *rotationConfig) SetDefaults() { | ||
if r.MaxSize == 0 { | ||
r.MaxSize = 100 | ||
} | ||
if r.MaxBackups == 0 { | ||
r.MaxBackups = 3 | ||
} | ||
|
||
if r.MaxAge == 0 { | ||
r.MaxAge = 30 | ||
} | ||
} | ||
|
||
type rotatingFile struct { | ||
l *lumberjack.Logger | ||
} | ||
|
||
// newRotatingFile initialize the lumberjack instance | ||
func newRotatingFile(cfg *Config) *rotatingFile { | ||
cfg.Rotation.SetDefaults() | ||
|
||
lj := lumberjack.Logger{ | ||
Filename: cfg.FileName, | ||
MaxSize: cfg.Rotation.MaxSize, | ||
MaxBackups: cfg.Rotation.MaxBackups, | ||
MaxAge: cfg.Rotation.MaxAge, | ||
Compress: cfg.Rotation.Compress, | ||
} | ||
|
||
return &rotatingFile{l: &lj} | ||
} | ||
|
||
// Close closes the file | ||
func (r *rotatingFile) Close() error { | ||
return r.l.Close() | ||
} | ||
|
||
// Name returns the name of the file | ||
func (r *rotatingFile) Name() string { | ||
return r.l.Filename | ||
} | ||
|
||
// Write implements io.Writer | ||
func (r *rotatingFile) Write(b []byte) (int, error) { | ||
return r.l.Write(b) | ||
} |