beautiful_logs_custom/
beautiful_logs_custom.rs

1use 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    // 使用自定义配置
9    example_custom_logging()?;
10
11    Ok(())
12}
13
14fn example_custom_logging() -> Result<()> {
15    println!("使用自定义美化配置:");
16
17    // 创建自定义配置
18    let config = LogConfig::new()
19        .time_format("%Y-%m-%d %H:%M:%S%.3f") // 包含毫秒
20        .ansi(true) // 启用颜色
21        .target(true) // 显示模块路径
22        .thread_ids(false) // 不显示线程ID
23        .compact(false) // 使用完整格式
24        .console_level(LevelFilter::DEBUG) // 控制台显示DEBUG级别
25        .file_level(LevelFilter::INFO); // 文件只记录INFO及以上
26
27    let _guards = logger_start_with_config("custom_demo", None, config)?;
28
29    println!("📝 开始记录不同级别的美化日志...\n");
30
31    // 记录各种级别的日志
32    trace!("🔍 这是TRACE级别日志 - 通常用于非常详细的调试信息");
33    debug!("🐛 这是DEBUG级别日志 - 用于调试信息");
34    info!("ℹ️  这是INFO级别日志 - 用于一般信息");
35    warn!("⚠️  这是WARN级别日志 - 用于警告信息");
36    error!("❌ 这是ERROR级别日志 - 用于错误信息");
37
38    println!();
39
40    // 记录结构化数据
41    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    // 记录系统状态
62    debug!(
63        memory_usage = "256MB",
64        cpu_usage = "15%",
65        active_connections = 42,
66        response_time_ms = 120,
67        "📊 系统状态检查"
68    );
69
70    // 记录警告信息
71    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    // 记录错误信息
83    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    // 记录业务逻辑
93    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    // 记录API调用
104    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}