beautiful_logs_custom/
beautiful_logs_custom.rs1use clamber_core::{LogConfig, Result, logger_start_with_config};
2use tracing::metadata::LevelFilter;
3use tracing::{debug, error, info, trace, warn};
4
5fn main() -> Result<()> {
6 println!("=== Clamber Core 自定义美化日志示例 ===\n");
7
8 example_custom_logging()?;
10
11 Ok(())
12}
13
14fn example_custom_logging() -> Result<()> {
15 println!("使用自定义美化配置:");
16
17 let config = LogConfig::new()
19 .time_format("%Y-%m-%d %H:%M:%S%.3f") .ansi(true) .target(true) .thread_ids(false) .compact(false) .console_level(LevelFilter::DEBUG) .file_level(LevelFilter::INFO); let _guards = logger_start_with_config("custom_demo", None, config)?;
28
29 println!("📝 开始记录不同级别的美化日志...\n");
30
31 trace!("🔍 这是TRACE级别日志 - 通常用于非常详细的调试信息");
33 debug!("🐛 这是DEBUG级别日志 - 用于调试信息");
34 info!("ℹ️ 这是INFO级别日志 - 用于一般信息");
35 warn!("⚠️ 这是WARN级别日志 - 用于警告信息");
36 error!("❌ 这是ERROR级别日志 - 用于错误信息");
37
38 println!();
39
40 info!(
42 event = "user_registration",
43 user_id = 12345,
44 email = "user@example.com",
45 registration_time = "2024-01-15T10:30:00Z",
46 source = "web_app",
47 "🎉 新用户注册成功"
48 );
49
50 info!(
51 event = "payment_processed",
52 user_id = 67890,
53 amount = 299.99,
54 currency = "CNY",
55 payment_method = "credit_card",
56 transaction_id = "TXN-ABC123",
57 merchant_id = "MERCHANT_001",
58 "💳 支付处理完成"
59 );
60
61 debug!(
63 memory_usage = "256MB",
64 cpu_usage = "15%",
65 active_connections = 42,
66 response_time_ms = 120,
67 "📊 系统状态检查"
68 );
69
70 warn!(
72 error_code = "AUTH_FAILED",
73 attempts = 3,
74 max_attempts = 5,
75 ip_address = "203.0.113.42",
76 user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
77 blocked_duration = "5min",
78 "🚫 认证失败,剩余尝试次数: {}",
79 2
80 );
81
82 error!(
84 error_type = "database_connection",
85 error_message = "Connection timeout after 30s",
86 database_host = "db.example.com",
87 port = 5432,
88 retry_count = 3,
89 "🔌 数据库连接失败"
90 );
91
92 info!(
94 order_id = "ORD-2024-001",
95 customer_id = "CUST-9876",
96 items_count = 3,
97 total_amount = 599.97,
98 shipping_address = "北京市朝阳区xxx街道",
99 estimated_delivery = "2024-01-18",
100 "📦 订单创建成功"
101 );
102
103 debug!(
105 api_endpoint = "/api/v1/users/profile",
106 method = "GET",
107 status_code = 200,
108 response_time_ms = 85,
109 request_id = "req-uuid-12345",
110 user_id = 98765,
111 "🌐 API调用成功"
112 );
113
114 println!("\n✅ 自定义配置日志记录完成!");
115 println!("📁 日志文件保存在: ./logs/");
116 println!("🎨 配置特性:");
117 println!(" • 时间格式: yyyy-MM-dd HH:mm:ss.SSS (包含毫秒)");
118 println!(" • 控制台: 彩色输出,显示DEBUG级别及以上");
119 println!(" • 文件: 无颜色,记录INFO级别及以上");
120 println!(" • 格式: 完整格式,显示模块路径");
121 println!(" • 线程: 不显示线程ID");
122
123 Ok(())
124}