ice-age 0.16.0

Simple logging kit for server programs
Documentation

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."),
};
});
}