Skip to content
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

add a option to support custom time format #202

Open
wants to merge 1 commit into
base: v2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ _testmain.go

*.exe
*.test
.idea
22 changes: 16 additions & 6 deletions lumberjack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Note that this is v2.0 of lumberjack, and should be imported using gopkg.in
// thusly:
//
// import "gopkg.in/natefinch/lumberjack.v2"
// import "gopkg.in/natefinch/lumberjack.v2"
//
// The package name remains simply lumberjack, and the code resides at
// https://github.com/natefinch/lumberjack under the v2.0 branch.
Expand Down Expand Up @@ -66,7 +66,7 @@ var _ io.WriteCloser = (*Logger)(nil)
// `/var/log/foo/server.log`, a backup created at 6:30pm on Nov 11 2016 would
// use the filename `/var/log/foo/server-2016-11-04T18-30-00.000.log`
//
// Cleaning Up Old Log Files
// # Cleaning Up Old Log Files
//
// Whenever a new logfile gets created, old log files may be deleted. The most
// recent files according to the encoded timestamp will be retained, up to a
Expand Down Expand Up @@ -107,6 +107,10 @@ type Logger struct {
// using gzip. The default is not to perform compression.
Compress bool `json:"compress" yaml:"compress"`

// BackupTimeFormat specifies the formatting standard for time when creating backup log files.
// The default is backupTimeFormat
BackupTimeFormat string `json:"backuptimeformat" yaml:"backuptimeformat"`

size int64
file *os.File
mu sync.Mutex
Expand Down Expand Up @@ -218,7 +222,10 @@ func (l *Logger) openNew() error {
// Copy the mode off the old logfile.
mode = info.Mode()
// move the existing file
newname := backupName(name, l.LocalTime)
if len(l.BackupTimeFormat) == 0 {
l.BackupTimeFormat = backupTimeFormat
}
newname := backupName(name, l.LocalTime, l.BackupTimeFormat)
if err := os.Rename(name, newname); err != nil {
return fmt.Errorf("can't rename log file: %s", err)
}
Expand All @@ -244,7 +251,7 @@ func (l *Logger) openNew() error {
// backupName creates a new filename from the given name, inserting a timestamp
// between the filename and the extension, using the local time if requested
// (otherwise UTC).
func backupName(name string, local bool) string {
func backupName(name string, local bool, timeformat string) string {
dir := filepath.Dir(name)
filename := filepath.Base(name)
ext := filepath.Ext(filename)
Expand All @@ -254,7 +261,7 @@ func backupName(name string, local bool) string {
t = t.UTC()
}

timestamp := t.Format(backupTimeFormat)
timestamp := t.Format(timeformat)
return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, timestamp, ext))
}

Expand Down Expand Up @@ -438,7 +445,10 @@ func (l *Logger) timeFromName(filename, prefix, ext string) (time.Time, error) {
return time.Time{}, errors.New("mismatched extension")
}
ts := filename[len(prefix) : len(filename)-len(ext)]
return time.Parse(backupTimeFormat, ts)
if len(l.BackupTimeFormat) == 0 {
l.BackupTimeFormat = backupTimeFormat
}
return time.Parse(l.BackupTimeFormat, ts)
}

// max returns the maximum size in bytes of log files before rolling.
Expand Down