Crates.io | ds-common-logger-rs-lib |
lib.rs | ds-common-logger-rs-lib |
version | 0.1.0 |
created_at | 2025-09-24 18:17:29.608787+00 |
updated_at | 2025-09-24 18:17:29.608787+00 |
description | A comprehensive Rust library for working with the DS Common Logger, providing high-level abstractions for logging |
homepage | |
repository | https://github.com/grasp-labs/ds-common-logger-rs-lib |
max_upload_size | |
id | 1853540 |
size | 59,897 |
A high-performance, production-ready logging library for Rust applications that provides structured logging with comprehensive tracing capabilities.
RUST_LOG
environment variableLOG_FORMAT
environment variableAdd this to your Cargo.toml
:
[dependencies]
ds-common-logger-rs-lib = "0.1.0"
Or use cargo add:
cargo add ds-common-logger-rs-lib
use ds_common_logger_rs_lib::init_tracing;
use tracing::{info, warn, error, debug};
fn main() {
// Initialize the global tracing subscriber
init_tracing();
// Start logging
info!("Application started");
warn!("This is a warning message");
error!("This is an error message");
debug!("This is a debug message");
}
use ds_common_logger_rs_lib::init_tracing;
use tracing::{info, span, Level};
fn main() {
init_tracing();
// Create a span for structured logging
let span = span!(Level::INFO, "user_operation", user_id = 42, operation = "login");
let _enter = span.enter();
info!("User login attempt");
info!("Login successful");
// Span automatically exits when _enter is dropped
}
use ds_common_logger_rs_lib::init_tracing;
use tracing::{error, info, instrument};
use std::io;
#[instrument]
fn process_file(filename: &str) -> Result<String, io::Error> {
info!("Processing file");
match std::fs::read_to_string(filename) {
Ok(content) => {
info!("File read successfully");
Ok(content)
}
Err(e) => {
error!("Failed to read file: {}", e);
Err(e)
}
}
}
fn main() {
init_tracing();
match process_file("config.toml") {
Ok(_) => info!("File processing completed"),
Err(e) => error!("File processing failed: {}", e),
}
}
Control log levels using the RUST_LOG
environment variable:
# Show all logs at info level and above
RUST_LOG=info cargo run
# Show only error logs
RUST_LOG=error cargo run
# Show debug logs for specific modules
RUST_LOG=debug,my_crate::module=info cargo run
# Show all logs for your crate, but only errors for dependencies
RUST_LOG=debug,my_crate=debug cargo run
Switch between compact and JSON output using the LOG_FORMAT
environment variable:
# Compact format (default) - human-readable
cargo run
# JSON format - machine-readable
LOG_FORMAT=json cargo run
Compact Format:
2024-01-15T10:30:45.123Z INFO my_app: Application started
2024-01-15T10:30:45.124Z WARN my_app: This is a warning message
2024-01-15T10:30:45.125Z ERROR my_app: This is an error message
JSON Format:
{"timestamp":"2024-01-15T10:30:45.123Z","level":"INFO","target":"my_app","message":"Application started"}
{"timestamp":"2024-01-15T10:30:45.124Z","level":"WARN","target":"my_app","message":"This is a warning message"}
{"timestamp":"2024-01-15T10:30:45.125Z","level":"ERROR","target":"my_app","message":"This is an error message"}
use ds_common_logger_rs_lib::init_tracing;
use tracing::{info, span, event, Level};
fn main() {
init_tracing();
// Create a span with custom fields
let span = span!(
Level::INFO,
"database_operation",
table = "users",
operation = "select",
query_id = "q123"
);
let _enter = span.enter();
// Log events within the span
event!(Level::DEBUG, "Executing database query");
event!(Level::INFO, "Query completed successfully");
}
The library automatically captures panics as structured log events:
use ds_common_logger_rs_lib::init_tracing;
fn main() {
init_tracing();
// This panic will be logged as a structured event
panic!("Something went wrong!");
}
The library is thread-safe and can be used in multi-threaded applications:
use ds_common_logger_rs_lib::init_tracing;
use tracing::info;
use std::thread;
fn main() {
init_tracing();
let handles: Vec<_> = (0..5)
.map(|i| {
thread::spawn(move || {
info!("Thread {} started", i);
// Do some work
info!("Thread {} finished", i);
})
})
.collect();
for handle in handles {
handle.join().unwrap();
}
}
The library uses the following environment variables:
RUST_LOG
- Controls log level filtering (e.g., info
, debug
, error
)LOG_FORMAT
- Controls output format (json
for JSON format, anything else for compact)This project is licensed under either of
at your option.