#log #logger #sqlite

ice-age

Simple logging kit for server programs

14 releases (breaking)

✓ Uses Rust 2018 edition

0.13.0 Jun 12, 2019
0.11.0 Jun 1, 2019
0.9.0 Mar 28, 2019
0.3.0 Sep 23, 2018

#21 in Caching

Download history 23/week @ 2019-02-27 29/week @ 2019-03-06 7/week @ 2019-03-13 66/week @ 2019-03-20 24/week @ 2019-03-27 15/week @ 2019-04-03 4/week @ 2019-04-10 13/week @ 2019-04-24 1/week @ 2019-05-01 10/week @ 2019-05-08 25/week @ 2019-05-15 42/week @ 2019-05-22 45/week @ 2019-05-29 30/week @ 2019-06-05

98 downloads per month

Custom license

20KB
303 lines

Ice Age




lib.rs:

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

~10MB
~181K SLoC