cli/docs/pkg/log.md
Snider 38db43bbfb Implement log retention policy (#306)
* Implement log retention policy

- Added Append method to io.Medium interface and implementations.
- Defined RotationOptions and updated log.Options to support log rotation.
- Implemented RotatingWriter in pkg/log/rotation.go with size and age-based retention.
- Updated Logger to use RotatingWriter when configured.
- Added comprehensive tests for log rotation and retention.
- Documented the log retention policy in docs/pkg/log.md and docs/configuration.md.
- Fixed MockMedium to return current time for Stat to avoid premature cleanup in tests.

* Fix formatting issues in pkg/io/local/client.go

The CI failed due to formatting issues. This commit fixes them and ensures all modified files are properly formatted.

* Fix auto-merge workflow CI failure

Inlined the auto-merge logic and added actions/checkout and --repo flag to gh command to provide the necessary git context. This resolves the 'fatal: not a git repository' error in CI.

* Address feedback on log retention policy

- Made cleanup synchronous in RotatingWriter for better reliability.
- Improved rotation error handling with recovery logic.
- Fixed size tracking to only increment on successful writes.
- Updated MockMedium to support and preserve ModTimes for age-based testing.
- Added TestRotatingWriter_AgeRetention and TestLogger_RotationIntegration.
- Implemented negative MaxAge to disable age-based retention.
- Updated documentation for clarity on Output priority and MaxAge behavior.
- Fixed typo in test comments.
- Fixed CI failure in auto-merge workflow.

---------

Co-authored-by: Claude <developers@lethean.io>
2026-02-05 10:26:32 +00:00

1.9 KiB

Log Retention Policy

The log package provides structured logging with automatic log rotation and retention management.

Retention Policy

By default, the following log retention policy is applied when log rotation is enabled:

  • Max Size: 100 MB per log file.
  • Max Backups: 5 old log files are retained.
  • Max Age: 28 days. Old log files beyond this age are automatically deleted. (Set to -1 to disable age-based retention).
  • Compression: Rotated log files can be compressed (future feature).

Configuration

Logging can be configured using the log.Options struct. To enable log rotation to a file, provide a RotationOptions struct. If both Output and Rotation are provided, Rotation takes precedence and Output is ignored.

Standalone Usage

logger := log.New(log.Options{
    Level: log.LevelInfo,
    Rotation: &log.RotationOptions{
        Filename:   "app.log",
        MaxSize:    100, // MB
        MaxBackups: 5,
        MaxAge:     28, // days
    },
})

logger.Info("application started")

Framework Integration

When using the Core framework, logging is usually configured during application initialization:

app := core.New(
    framework.WithName("my-app", log.NewService(log.Options{
        Level: log.LevelDebug,
        Rotation: &log.RotationOptions{
            Filename: "/var/log/my-app.log",
        },
    })),
)

How It Works

  1. Rotation: When the current log file exceeds MaxSize, it is rotated. The current file is renamed to filename.1, filename.1 is renamed to filename.2, and so on.
  2. Retention:
    • Files beyond MaxBackups are automatically deleted during rotation.
    • Files older than MaxAge days are automatically deleted during the cleanup process.
  3. Appends: When an application restarts, it appends to the existing log file instead of truncating it.