Expand description
§FlashLog
A blazingly fast Rust logging library with lazy evaluation.
§Features
- Lazy Evaluation: Most evaluations are performed in the logger thread, resulting in exceptional performance.
- JSON Output: Log messages are printed in
JSONformat for easy parsing and analysis. - LazyString: Provides
LazyStringfor optimized string interpolation. - Customizable: Flexible configuration options for file output, console reporting, buffer size, and more.
- Timezone Support: Ability to set local or custom timezones for log timestamps.
§Quick Start
Add FlashLog to your Cargo.toml:
[dependencies]
flashlog = "0.1"
Basic usage example:
use flashlog::{Logger, LogLevel, info};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let logger = Logger::initialize()
.with_file("logs", "message")?
.with_max_log_level(LogLevel::Info)
.launch();
info!("Hello, FlashLog!");
Ok(())
}§Advanced Usage
§Logging Structs
FlashLog can easily log custom structs:
use serde::{Deserialize, Serialize};
use flashlog::{Logger, LogLevel, log_info};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LogStruct {
data: [u64; 10],
}
impl Default for LogStruct {
fn default() -> Self {
LogStruct { data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let logger = Logger::initialize()
.with_file("logs", "message")?
.with_max_log_level(LogLevel::Info)
.launch();
let log_struct = LogStruct::default();
log_info!("Log message", log_struct = log_struct);
Ok(())
}§Using LazyString for Optimization
use flashlog::{lazy_string::LazyString, log_info};
//The format in the LazyString is evaluated in the logger thread.
//The creation takes around 1.5 ns regardless of the interpolation number
let lazy_msg = LazyString::new(|| format!("{} {} {}", 1, 2, 3));
log_info!("LazyOne", msg = lazy_msg);§Configuration Options
FlashLog offers various configuration options:
use flashlog::{Logger, LogLevel, TimeZone};
let logger = Logger::initialize()
.with_file("logs", "message").expect("faied to create log file")
.with_console_report(false)
.with_msg_buffer_size(1_000_000)
.with_msg_flush_interval(1_000_000)
.with_max_log_level(LogLevel::Info)
.with_timezone(TimeZone::Local)
.launch();§Output Format
Logs are outputted in JSON format for easy parsing:
{
"data": {"text": "Warm up"},
"date": "20240829",
"level": "Info",
"offset": 9,
"src": "src/main.rs:135",
"time": "20:08:21.071:409:907",
"topic": "not given"
}
For more detailed information about each module and function, please refer to the individual documentation pages.
Re-exports§
pub use crate::timer::get_unix_nano;pub use crate::timer::convert_unix_nano_to_date_and_time;pub use crate::logger::LazyMessage;pub use crate::logger::LogLevel;pub use crate::logger::LogMessage;pub use crate::logger::TimeZone;pub use crate::logger::Logger;pub use crate::logger::LOG_SENDER;pub use crate::logger::TIMEZONE;pub use crate::logger::MAX_LOG_LEVEL;pub use serde_json;