natefinch / lumberjack

lumberjack is a log rolling package for Go
MIT License
4.79k stars 591 forks source link

Is it possible to disable rotation? #96

Closed pims closed 4 years ago

pims commented 4 years ago

In some environments, it might be advisable to disable log rotation and let another process handle that work.

I originally thought that setting MaxSize: 0 would disable log rotation, but the func (l *Logger) max() function returns the defaultMaxSize:

// max returns the maximum size in bytes of log files before rolling.
func (l *Logger) max() int64 {
    if l.MaxSize == 0 {
        return int64(defaultMaxSize * megabyte)
    }
    return int64(l.MaxSize) * int64(megabyte)
}

to preserve existing behavior, would you be open to adding a disableFileRotation field to the Logger struct and replacing the various:

if info.Size()+int64(writeLen) >= l.max() {
    return l.rotate()
}

with:

func(l *Logger) shouldRotate(currentSize, writeLen int64) bool {
   if l.disableFileRotation {
        return false
   }
   return currentSize + writeLen >= l.max()
}

This should be backward compatible behavior, since the default value for disableFileRotation would be false and only interested parties would set it to true.

Another alternative would be to set MaxSize to -1 and change behavior accordingly, but many consumer of lumberjack already error on negative values.

xiegeo commented 4 years ago

The point of lumberjack is to rotate logs, if you don't need rotation, don't use lumberjack, or make it optional to use lumberjack in your codebase.

pims commented 4 years ago

@xiegeo that's fair. I was hoping to find an alternative to removing lumberjack from github.com/kubernetes/kubernetes by having a disable rotation flag, but as you pointed out, it's better to make it optional.