Skip to content

Commit

Permalink
Add MaxBytes and deprecate MaxSize
Browse files Browse the repository at this point in the history
Signed-off-by: Fahed DORGAA <fahed_dorgaa@carrefour.com>

fixes

Signed-off-by: Fahed DORGAA <fahed_dorgaa@carrefour.com>

fixes

Signed-off-by: fahed dorgaa <fahed.dorgaa@gmail.com>

fixes

Signed-off-by: fahed dorgaa <fahed.dorgaa@gmail.com>
  • Loading branch information
Fahed DORGAA authored and fahedouch committed Apr 23, 2022
1 parent 47ffae2 commit 139d774
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lumberjack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 139d774

Please sign in to comment.