From 139d7743acba3926612747d8826e54439a0b6117 Mon Sep 17 00:00:00 2001 From: Fahed DORGAA Date: Sat, 23 Apr 2022 19:02:00 +0200 Subject: [PATCH] Add MaxBytes and deprecate MaxSize Signed-off-by: Fahed DORGAA fixes Signed-off-by: Fahed DORGAA fixes Signed-off-by: fahed dorgaa fixes Signed-off-by: fahed dorgaa --- lumberjack.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lumberjack.go b/lumberjack.go index 3447cdc..665b66d 100644 --- a/lumberjack.go +++ b/lumberjack.go @@ -47,8 +47,8 @@ var _ io.WriteCloser = (*Logger)(nil) // Logger is an io.WriteCloser that writes to the specified filename. // // Logger opens or creates the logfile on first Write. If the file exists and -// is less than MaxSize megabytes, lumberjack will open and append to that file. -// If the file exists and its size is >= MaxSize megabytes, the file is renamed +// is less than MaxBytes, lumberjack will open and append to that file. +// If the file exists and its size is >= MaxBytes, the file is renamed // by putting the current time in a timestamp in the name immediately before the // file's extension (or the end of the filename if there's no extension). A new // log file is then created using original filename. @@ -82,6 +82,12 @@ type Logger struct { // os.TempDir() if empty. Filename string `json:"filename" yaml:"filename"` + // MaxBytes is the maximum size in bytes of the log file before it gets + // rotated. It defaults to 104857600 (100 megabytes). + MaxBytes int64 `json:"maxbytes" yaml:"maxbytes"` + + // Deprecated: use MaxBytes instead. + // This field is mutually exclusive with the “MaxBytes” field. // MaxSize is the maximum size in megabytes of the log file before it gets // rotated. It defaults to 100 megabytes. MaxSize int `json:"maxsize" yaml:"maxsize"` @@ -129,9 +135,9 @@ var ( ) // Write implements io.Writer. If a write would cause the log file to be larger -// than MaxSize, the file is closed, renamed to include a timestamp of the +// than MaxBytes, the file is closed, renamed to include a timestamp of the // current time, and a new log file is created using the original log file name. -// If the length of the write is greater than MaxSize, an error is returned. +// If the length of the write is greater than MaxBytes, an error is returned. func (l *Logger) Write(p []byte) (n int, err error) { l.mu.Lock() defer l.mu.Unlock() @@ -259,8 +265,8 @@ func backupName(name string, local bool) string { } // openExistingOrNew opens the logfile if it exists and if the current write -// would not put it over MaxSize. If there is no such file or the write would -// put it over the MaxSize, a new file is created. +// would not put it over MaxBytes. If there is no such file or the write would +// put it over the MaxBytes, a new file is created. func (l *Logger) openExistingOrNew(writeLen int) error { l.mill() @@ -443,6 +449,9 @@ func (l *Logger) timeFromName(filename, prefix, ext string) (time.Time, error) { // max returns the maximum size in bytes of log files before rolling. func (l *Logger) max() int64 { + if l.MaxBytes != 0 { + return l.MaxBytes + } if l.MaxSize == 0 { return int64(defaultMaxSize * megabyte) }