Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

File Rotation & Retention

Automatic log file rotation with retention policies to manage disk space.

Overview

Logly-rs provides comprehensive file rotation and retention:

  • Time-based rotation: hourly, daily, weekly, monthly, yearly
  • Size-based rotation: Rotate when file reaches specified size
  • Combined rotation: Rotate on either time OR size threshold
  • Retention policies: Automatically delete old log files
  • Timestamped files: Rotated files include timestamps

Rotation Intervals

Logly supports time-based rotation:

  • hourly - Rotate every hour
  • daily - Rotate every day
  • weekly - Rotate every week
  • monthly - Rotate every 30 days
  • yearly - Rotate every 365 days

Time-Based Rotation

#![allow(unused)]
fn main() {
SinkConfig {
    path: Some(PathBuf::from("logs/app.log")),
    rotation: Some("daily".to_string()),
    retention: Some(7),  // Keep 7 days
    ..Default::default()
}
}

Size-Based Rotation

#![allow(unused)]
fn main() {
SinkConfig {
    path: Some(PathBuf::from("logs/app.log")),
    size_limit: Some(10 * 1024 * 1024),  // 10MB
    retention: Some(5),  // Keep 5 files
    ..Default::default()
}
}

Combined Rotation

Rotate when EITHER condition is met:

#![allow(unused)]
fn main() {
SinkConfig {
    path: Some(PathBuf::from("logs/app.log")),
    rotation: Some("daily".to_string()),
    size_limit: Some(10 * 1024 * 1024),  // Daily OR 10MB
    retention: Some(7),
    ..Default::default()
}
}

Retention Policies

Retention automatically deletes old rotated log files to prevent disk space issues.

How Retention Works

  1. Count-based: Keeps only the N most recent files
  2. Automatic cleanup: Runs during rotation
  3. Sorted by time: Oldest files deleted first
  4. Unlimited option: Set to None for no limit

Basic Retention

#![allow(unused)]
fn main() {
SinkConfig {
    path: Some(PathBuf::from("logs/app.log")),
    rotation: Some("daily".to_string()),
    retention: Some(7),  // Keep only 7 most recent files
    ..Default::default()
}
}

Retention Examples

#![allow(unused)]
fn main() {
// Keep 24 hours of hourly logs
SinkConfig {
    rotation: Some("hourly".to_string()),
    retention: Some(24),
    ..Default::default()
}

// Keep 30 days of daily logs
SinkConfig {
    rotation: Some("daily".to_string()),
    retention: Some(30),
    ..Default::default()
}

// Keep 12 months of monthly logs
SinkConfig {
    rotation: Some("monthly".to_string()),
    retention: Some(12),
    ..Default::default()
}

// Unlimited retention (never delete)
SinkConfig {
    rotation: Some("daily".to_string()),
    retention: None,  // Keep all files forever
    ..Default::default()
}
}

Rotated File Naming

Files are renamed with timestamps:

app.log -> app_20240101_120000.log

Examples

Hourly Rotation

#![allow(unused)]
fn main() {
SinkConfig {
    path: Some(PathBuf::from("logs/hourly.log")),
    rotation: Some("hourly".to_string()),
    retention: Some(24),  // Keep 24 hours
    ..Default::default()
}
}

Daily Rotation

#![allow(unused)]
fn main() {
SinkConfig {
    path: Some(PathBuf::from("logs/daily.log")),
    rotation: Some("daily".to_string()),
    retention: Some(7),  // Keep 7 days
    ..Default::default()
}
}

Weekly Rotation

#![allow(unused)]
fn main() {
SinkConfig {
    path: Some(PathBuf::from("logs/weekly.log")),
    rotation: Some("weekly".to_string()),
    retention: Some(4),  // Keep 4 weeks
    ..Default::default()
}
}

Monthly Rotation

#![allow(unused)]
fn main() {
SinkConfig {
    path: Some(PathBuf::from("logs/monthly.log")),
    rotation: Some("monthly".to_string()),
    retention: Some(12),  // Keep 12 months
    ..Default::default()
}
}

Yearly Rotation

#![allow(unused)]
fn main() {
SinkConfig {
    path: Some(PathBuf::from("logs/yearly.log")),
    rotation: Some("yearly".to_string()),
    retention: Some(5),  // Keep 5 years
    ..Default::default()
}
}

Size-Based (100MB)

#![allow(unused)]
fn main() {
SinkConfig {
    path: Some(PathBuf::from("logs/app.log")),
    size_limit: Some(100 * 1024 * 1024),
    retention: Some(10),
    ..Default::default()
}
}

Best Practices

  1. Development: Hourly or daily rotation
  2. Production: Daily or weekly rotation
  3. Archives: Monthly or yearly rotation
  4. High-Volume: Size-based rotation
  5. Compliance: Long retention periods

Automatic Cleanup

Old files are automatically deleted based on retention policy:

  • Sorted by modification time
  • Oldest files deleted first
  • Happens during rotation