Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add RotationInterval & FileNameFormat support #215

Open
wants to merge 1 commit into
base: v2.0
Choose a base branch
from

Conversation

ReaVNaiL
Copy link

@ReaVNaiL ReaVNaiL commented Aug 5, 2024

Use Cases

  • 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.

  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.
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant