Crates.io | loggix |
lib.rs | loggix |
version | 1.0.2 |
source | src |
created_at | 2024-12-06 21:29:27.38125 |
updated_at | 2024-12-06 21:41:31.786598 |
description | A powerful, structured logging library for Rust inspired by Logrus. Features thread-safe logging, structured fields, custom formatters, and beautiful terminal output. |
homepage | |
repository | https://github.com/cploutarchou/loggix |
max_upload_size | |
id | 1474754 |
size | 65,202 |
A powerful structured logger for Rust, inspired by Logrus. Loggix combines structured logging with Rust's safety and performance guarantees.
std::io::Write
)Add to your Cargo.toml
:
[dependencies]
loggix = "1.0.2"
use loggix::{info, debug, warn, error};
fn main() {
debug!("Debug message");
info!("Info message");
warn!("Warning message");
error!("Error message");
}
use loggix::with_fields;
fn main() {
// Log with structured fields
with_fields!(
"user_id" => "12345",
"action" => "login",
"ip" => "192.168.1.1"
)
.info("User login successful")
.unwrap();
}
use loggix::{Logger, JSONFormatter, with_fields};
fn main() {
let logger = Logger::new()
.formatter(JSONFormatter::new().pretty(true))
.build();
with_fields!(
"transaction_id" => "tx-9876",
"amount" => 150.50,
"currency" => "USD"
)
.info("Payment processed")
.unwrap();
}
Output:
{
"timestamp": "2024-12-06T20:30:21.103Z",
"level": "info",
"message": "Payment processed",
"transaction_id": "tx-9876",
"amount": 150.50,
"currency": "USD"
}
use loggix::with_error;
use std::fs::File;
fn main() {
let result = File::open("non_existent.txt");
if let Err(error) = result {
with_error(&error)
.error("Failed to open file")
.unwrap();
}
}
use loggix::{Logger, Level, TextFormatter};
fn main() {
let logger = Logger::new()
.level(Level::Debug)
.formatter(TextFormatter::new()
.timestamp_format("%Y-%m-%d %H:%M:%S")
.colors(true)
.build())
.build();
logger.with_fields(Default::default())
.with_field("component", "auth")
.info("Authentication successful")
.unwrap();
}
Implement the Formatter
trait to create your own log format:
use loggix::{Formatter, Entry};
use std::error::Error;
struct MyFormatter;
impl Formatter for MyFormatter {
fn format(&self, entry: &Entry) -> Result<Vec<u8>, Box<dyn Error>> {
let mut output = Vec::new();
// Format the log entry however you want
write!(&mut output, "MY-LOG: {} - {}", entry.level, entry.message)?;
Ok(output)
}
}
Implement the Hook
trait to process log entries:
use loggix::{Hook, Entry, Level};
struct MetricsHook;
impl Hook for MetricsHook {
fn fire(&self, entry: &Entry) -> Result<(), Box<dyn Error>> {
// Send metrics to your metrics system
if entry.level == Level::Error {
// Record error metrics
}
Ok(())
}
fn levels(&self) -> Vec<Level> {
vec![Level::Error, Level::Fatal]
}
}
Check out the examples directory for more detailed examples:
Loggix is designed for high performance while maintaining flexibility. Here are some key performance characteristics:
Basic logging: 813.57 ns/iter
Structured logging: 1.34 ยตs/iter (with 2 fields)
Multiple fields: 2.23 ยตs/iter (with 4 fields)
Key performance features:
Run the benchmarks yourself with:
cargo bench
The benchmarks use Criterion.rs for statistical analysis and reliable measurements.
Loggix is designed to be thread-safe by default. All logging operations are atomic and can be safely used across multiple threads. The library uses Arc
and Mutex
internally to protect shared state.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Here are the planned features and enhancements for Loggix:
These features are in various stages of planning and development. Contributions and feedback are welcome!
This project is licensed under the MIT License - see the LICENSE file for details.