use firo_logger::{
init, log_debug, log_error, log_info, log_success, log_warning, LogLevel, LoggerConfig,
OutputFormat,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = LoggerConfig::builder()
.level(LogLevel::Debug)
.format(OutputFormat::Json)
.console(true)
.colors(false) .include_caller(true)
.include_thread(true)
.datetime_format("%Y-%m-%dT%H:%M:%S%.3fZ")
.metadata("service", "json-demo")
.metadata("version", "1.0.0")
.metadata("environment", "development")
.build();
init(config)?;
println!("=== JSON Logging Demo ===");
println!("Each log entry will be a JSON object with structured data.\n");
log_info!("Application started with JSON logging enabled")?;
log_success!("JSON formatter initialized successfully")?;
log_debug!("Debug information: processing user request")?;
log_warning!("Warning: API rate limit approaching threshold")?;
log_error!("Error: Failed to connect to external service")?;
simulate_user_events()?;
simulate_api_requests()?;
simulate_database_operations()?;
log_info!("JSON logging demo completed")?;
println!("\n=== Pretty JSON Example ===");
println!("Now let's try with pretty-printed JSON:");
let _pretty_config = LoggerConfig::builder()
.level(LogLevel::Info)
.format(OutputFormat::Json) .console(true)
.include_caller(false) .include_thread(false)
.metadata("format", "pretty")
.build();
log_info!(
"This would be pretty-printed JSON if we extended JsonFormatter with a pretty option"
)?;
Ok(())
}
fn simulate_user_events() -> Result<(), Box<dyn std::error::Error>> {
log_info!("Simulating user events")?;
log_success!("User authentication successful")?;
log_info!("User performed search operation")?;
log_debug!("Search query processed with 42 results")?;
log_info!("User session terminated gracefully")?;
Ok(())
}
fn simulate_api_requests() -> Result<(), Box<dyn std::error::Error>> {
log_info!("Simulating API requests")?;
log_success!("GET /api/users returned 200 OK")?;
log_success!("POST /api/orders returned 201 Created")?;
log_warning!("GET /api/deprecated-endpoint returned 404 Not Found")?;
log_error!("POST /api/payment failed with 500 Internal Server Error")?;
log_warning!("Rate limit warning: 90% of quota used")?;
Ok(())
}
fn simulate_database_operations() -> Result<(), Box<dyn std::error::Error>> {
log_info!("Simulating database operations")?;
log_debug!("Executing SELECT query on users table")?;
log_success!("Database query completed successfully")?;
log_warning!("Slow query detected: took 2.5 seconds to complete")?;
log_error!("Database connection timeout after 30 seconds")?;
log_success!("Database connection restored automatically")?;
Ok(())
}