Daily Log Rotation: Rotate logs at the end of each day regardless of their size.
Hourly Log Rotation: Useful for high-frequency log updates where logs need to be rotated every hour.
Custom Log File Names: Define custom log file names with date and time placeholders.
Main changes
Changes are fully backwards compatible since we add optional values. Existing configurations without RotationInterval and FileNameFormat will continue to work as before.
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.
Helper Function
updateNextRotateTime: Sets or updates nextRotateTime based on the current time and RotationInterval.
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.
}
Use Cases
Main changes
Changes are fully backwards compatible since we add optional values. Existing configurations without
RotationInterval
andFileNameFormat
will continue to work as before.RotationInterval Implementation
RotationInterval
field to theLogger
struct.updateNextRotateTime
helper function to initialize and updatenextRotateTime
.rotate
andshouldRotate
methods to incorporate time-based rotation checks.Helper Function
updateNextRotateTime
: Sets or updatesnextRotateTime
based on the current time andRotationInterval
.Tests
TestRotationInterval
to verify log rotation based onRotationInterval
.TestFileNameFormat
to verify custom log file names.Sample Usage