lazylog

Crates.iolazylog
lib.rslazylog
version0.1.4
created_at2026-01-14 08:58:35.440291+00
updated_at2026-01-14 08:58:35.440291+00
descriptionA flexible logging library with file rotation and structured output
homepage
repositoryhttps://github.com/lazywalker/lazylog
max_upload_size
id2042446
size139,814
(lazywalker)

documentation

README

Lazylog

A flexible logging library with file rotation and structured output for Rust applications.

Features

  • Console Logging: Output logs to stdout/stderr with customizable formatting
  • File Logging: Write logs to files with automatic rotation
  • Structured Logging: Support for JSON output format
  • Log Rotation: Rotate logs based on size, time, or both
  • Tracing Integration: Built on top of the tracing ecosystem

Installation

Add this to your Cargo.toml:

[dependencies]
lazylog = "0.1"

Optional features:

  • file: Enable file logging support
  • ansi: Enable ANSI color codes in console output
  • time: Enable time-based log rotation

Quick Start

use lazylog;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize logging
    lazylog::builder()
        .with_console(true)
        .with_level("info")
        .init()?;

    tracing::info!("Application started");
    Ok(())
}

Configuration

Basic Usage

// Console logging
lazylog::builder()
    .with_console(true)
    .with_level("debug")
    .init()?;

// File logging with size rotation
lazylog::builder()
    .with_file("app.log")
    .with_rotation(lazylog::RotationTrigger::size(1024 * 1024, 5)) // 1MB, keep 5 files
    .init()?;

// JSON format
lazylog::builder()
    .with_console(true)
    .with_format("json")
    .init()?;

Log Rotation

use lazylog::{RotationTrigger, RotationPeriod};

// Size-based rotation
RotationTrigger::size(10 * 1024 * 1024, 5) // 10MB, keep 5 files

// Time-based rotation (requires `time` feature)
RotationTrigger::time(RotationPeriod::Daily)

// Hybrid rotation
RotationTrigger::both(RotationPeriod::Daily, 10 * 1024 * 1024, 5)

YAML Configuration

log:
  console: true
  level: info
  format: text
  file:
    path: /var/log/app.log
    rotation:
      size: 10M

API Reference

Builder API

  • lazylog::builder() - Create a new builder
  • with_console(bool) - Enable console logging
  • with_level(&str) - Set log level
  • with_format(&str) - Set format ("text" or "json")
  • with_file(path) - Enable file logging
  • with_rotation(RotationTrigger) - Set rotation
  • init() - Initialize logging

RotationTrigger

  • size(max_size: u64, max_files: usize) - Size-based rotation
  • time(period: RotationPeriod) - Time-based rotation
  • both(period, max_size, max_files) - Hybrid rotation

RotationPeriod

  • Hourly, Daily, Weekly, Monthly

Testing

cargo test

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Commit count: 8

cargo fmt