| Crates.io | logfusion |
| lib.rs | logfusion |
| version | 0.1.0 |
| created_at | 2025-08-07 14:56:40.97367+00 |
| updated_at | 2025-08-07 14:56:40.97367+00 |
| description | Unified logging and error handling for Rust with structured data, tracing integration, and cross-language support |
| homepage | https://github.com/deepbrain/superconfig/tree/main/crates/logfusion |
| repository | https://github.com/deepbrain/superconfig |
| max_upload_size | |
| id | 1785415 |
| size | 548,387 |
Tracing-native logging macros with structured logging support and enhanced error handling for Rust.
LogFusion provides a clean, modern logging interface built on top of the tracing ecosystem, offering structured logging, automatic initialization, and powerful error handling capabilities.
tracing ecosystem for superior observabilitylog cratedefine_errors! macro with LogFusion format + thiserror compatibility#[instrument][dependencies]
logfusion = "0.2"
# Optional: Add tracing-subscriber for advanced configuration
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
use logfusion::{info, warn, error, debug, trace};
fn main() {
// LogFusion auto-initializes tracing - no setup needed!
info!("Application starting");
warn!("This is a warning");
error!("Something went wrong");
// Structured logging with fields
info!(
user_id = 12345,
action = "login",
ip_address = "192.168.1.1",
duration_ms = 145,
"User authentication successful"
);
}
New LogFusion Format (Simplified & Powerful):
use logfusion::define_errors;
// ๐ LogFusion Format - Clean, attribute-based syntax
define_errors! {
AppError {
DatabaseConnection { host: String, port: u16 } : "Database connection failed: {host}:{port}" [level = error, target = "app::db"],
UserNotFound { user_id: u64 } : "User not found: {user_id}" [level = warn],
InvalidConfig {} : "Invalid configuration detected" [level = error],
NetworkTimeout { source: std::io::Error } : "Network operation timed out" // Auto source chaining
}
}
// Multiple error types in one macro
define_errors! {
ApiError {
BadRequest { field: String } : "Invalid field: {field}" [level = warn],
Unauthorized {} : "Access denied" [level = error]
}
DatabaseError {
ConnectionFailed { host: String } : "Failed to connect to {host}" [level = error]
}
}
fn example() -> Result<(), AppError> {
// Errors are automatically logged with structured tracing
let err = AppError::UserNotFound { user_id: 12345 };
err.log(); // Logs: WARN app::module: [UserNotFound] User not found: 12345
// ๐ New in v0.2: Error introspection for monitoring & debugging
let (code, level, target) = err.error_info();
println!("Code: {}, Level: {}, Target: {}", code, level, target);
// Output: Code: UserNotFound, Level: warn, Target: app::module
Err(err)
}
Traditional Thiserror Syntax (Fully Compatible):
define_errors! {
pub enum AppError {
#[error("Database connection failed: {host}:{port}", level = error, target = "app::db")]
DatabaseConnection { host: String, port: u16 },
#[error("User not found: {user_id}", level = warn)]
UserNotFound { user_id: u64 },
}
}
New in v0.2: All generated error enums include an error_info() method for monitoring and debugging:
use logfusion::define_errors;
use std::collections::HashMap;
define_errors! {
ApiError {
DatabaseTimeout { query: String } : "Query timed out: {query}" [level = error, target = "db::query"],
RateLimited {} : "API rate limit exceeded" [level = warn, target = "api::rate"],
UserNotFound { user_id: u64 } : "User {user_id} not found" [level = info]
}
}
fn main() {
let error = ApiError::DatabaseTimeout { query: "SELECT * FROM users".to_string() };
// Get structured error information
let (code, level, target) = error.error_info();
println!("Error Code: {}", code); // "DatabaseTimeout"
println!("Log Level: {}", level); // "error"
println!("Target: {}", target); // "db::query"
// Perfect for metrics collection
let mut error_metrics = HashMap::new();
*error_metrics.entry(code).or_insert(0) += 1;
// Ideal for monitoring dashboards
match level {
"error" => send_alert_to_pagerduty(&error),
"warn" => increment_warning_counter(),
_ => log_for_debugging(&error)
}
}
Use Cases for error_info():
LogFusion v0.2 introduces a revolutionary dual-syntax approach to error definitions:
LogFusion Format Benefits:
#[error(...)] attributes[level = warn, target = "app::db"] syntaxsource automatically become #[source]{}) and struct variants in same enum"Failed to connect to {host}:{port}" syntaxPerformance & Maintainability:
Built on tracing, the modern standard for Rust observability:
#[instrument]No boilerplate setup required:
use logfusion::info;
fn main() {
// This works immediately - no initialization needed!
info!("Hello, world!");
}
use logfusion::info;
// Rich, structured metadata
info!(
request_id = "req-123",
user_id = 12345,
method = "POST",
path = "/api/users",
status_code = 201,
duration_ms = 45,
"API request completed"
);
Works seamlessly with existing log crate usage:
// These both work and are captured by LogFusion
log::info!("Legacy log message");
logfusion::info!("Modern LogFusion message");
LogFusion excels at structured logging, making your logs machine-readable and perfect for modern observability platforms:
use logfusion::{info, error, info_span};
// User authentication
info!(
user_id = 12345,
username = "alice",
ip_address = "192.168.1.100",
mfa_enabled = true,
"User login successful"
);
// Payment processing
error!(
transaction_id = "txn-abc-123",
amount_cents = 2999,
currency = "USD",
decline_reason = "insufficient_funds",
"Payment failed"
);
// Spans with structured context
let span = info_span!("process_order", order_id = "order-123", customer_id = 456);
let _enter = span.enter();
info!("Processing order");
info!("Order completed successfully");
Full support for tracing spans and automatic instrumentation:
use logfusion::{info, info_span};
use tracing::instrument;
#[instrument(level = "info")]
async fn process_user_request(user_id: u64, action: &str) -> Result<String, AppError> {
info!("Processing user request");
// Nested spans
let span = info_span!("database_query", table = "users");
let _enter = span.enter();
info!("Executing database query");
Ok("Success".to_string())
}
LogFusion respects standard tracing environment variables:
# Set log level
RUST_LOG=debug cargo run
# Target specific modules
RUST_LOG=myapp::database=debug,myapp::auth=info cargo run
# Filter by span names
RUST_LOG=process_order=trace cargo run
For advanced use cases, you can configure tracing manually:
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
fn main() {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().json())
.with(tracing_subscriber::EnvFilter::from_default_env())
.init();
// Now use LogFusion normally
logfusion::info!("Application started with custom config");
}
logfusion_format_showcase.rs - Complete LogFusion format demonstration with all featureslogfusion_source_chaining.rs - Automatic source chaining with fields named "source"field_interpolation_demo.rs - Dual-syntax field interpolation comparisonstructured_logging_demo.rs - Manual + automatic structured loggingadvanced_tracing_features.rs - Spans, instrumentation, and ecosystem integrationLogFusion includes optional FFI callback support for integrating with other languages:
[dependencies]
logfusion = { version = "0.2", features = ["callback"] }
With the callback feature enabled, LogFusion can route log messages to external callbacks, enabling integration with:
use logfusion::{info, error};
fn main() {
// These messages are sent to both tracing AND any registered callbacks
info!("This goes to tracing and FFI callbacks");
error!("Error messages are bridged to external systems");
}
The callback system allows external applications to receive structured log data while LogFusion continues to work normally with the tracing ecosystem.
LogFusion uses minimal feature flags:
[dependencies]
logfusion = { version = "0.2", features = ["callback"] }
callback - Enable FFI callback support (optional, for cross-language integrations)log to tracinglog + env_logger)LogFusion works seamlessly with the entire tracing ecosystem:
tracing-subscriber - Flexible subscriber implementationstracing-opentelemetry - OpenTelemetry integrationconsole-subscriber - Tokio Console integrationtracing-appender - File output and rotationtracing-flame - Flamegraph profilingThis project is licensed under the MIT License - see the LICENSE file for details.
Built on top of the excellent tracing and thiserror crates. Special thanks to the Rust logging ecosystem maintainers.