natefinch / lumberjack

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

feat: Add RotationInterval & FileNameFormat support #215

Open ReaVNaiL opened 1 month ago

ReaVNaiL commented 1 month ago

Use Cases

Main changes

Changes are fully backwards compatible since we add optional values. Existing configurations without RotationInterval and FileNameFormat will continue to work as before.

  1. RotationInterval Implementation

    • Added a RotationInterval field to the Logger struct.
    • Implemented the updateNextRotateTime helper function to initialize and update nextRotateTime.
    • Modified the rotate and shouldRotate methods to incorporate time-based rotation checks.
  2. Helper Function

    • updateNextRotateTime: Sets or updates nextRotateTime based on the current time and RotationInterval.
  3. Tests

    • Added TestRotationInterval to verify log rotation based on RotationInterval.
    • Added TestFileNameFormat to verify custom log file names.

Sample Usage

logger := &Logger{
    Filename:         "/path/to/logfile.log",
    MaxSize:          100,                       // megabytes
    RotationInterval: time.Hour,                 // Rotate hourly
    FileNameFormat:   "%FILENAME%.2006-01%EXT%", // Custom log file names, only year and month.
}