#log #logger #sqlite #logging

ice-age

Simple logging kit for server programs

14 breaking releases

0.16.0 May 3, 2020
0.15.0 Jan 10, 2020
0.14.0 Sep 17, 2019
0.13.0 Jun 12, 2019
0.3.0 Sep 23, 2018

#1346 in Web programming

Custom license

20KB
298 lines

Simple logging kit

Project


Design

It uses synchronous channels for communication. Log records are stored in RAM, and will be flushed to disk based on some configurable conditions: a period of time, or when maximum number of records reached.

Backends: SQLite.

The crate's own log messages are prefixed with TAG.

Examples

use std::{
    env,
    sync::mpsc::TrySendError,
    thread,
    time::{UNIX_EPOCH, Duration, SystemTime},
};
use ice_age::{Config, Cmd, Log, Logger};

let config = Config {
    // Directory to save log files
    work_dir: env::temp_dir(),
    // For this example, max file length is 1 MiB
    max_file_len: 1024 * 1024,
    // Keep log files at most 3 days
    log_files_reserved: Duration::from_secs(3 * 24 * 60 * 60),
    // Maximum log records to be kept in RAM
    buf_len: 5_000,
    // Flush to disk every 30 minutes
    disk_flush_interval: Duration::from_secs(30 * 60),
};

let logger = Logger::make(config).unwrap();
for _ in 0..3 {
    let logger = logger.clone();
    thread::spawn(move || {
        let log = Log {
            time: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
            remote_ip: String::from("127.0.0.1"),
            url: String::from("/api/statistics"),
            response_size: Some(512),
            code: 200,
            runtime: Duration::from_secs(1),
            notes: None,
        };

        // Use ::try_send() to not block the thread.
        // This example's strategy is to discard failed calls.
        match logger.try_send(Cmd::StoreLog(log)) {
            Ok(()) => (),
            Err(TrySendError::Full(_)) =>
                eprintln!("Log buffer is full, discarding..."),
            Err(TrySendError::Disconnected(_)) =>
                eprintln!("Failed to store log. Perhaps log server is down."),
        };
    });
}

Dependencies

~21MB
~405K SLoC