LogConfig

Struct LogConfig 

Source
pub struct LogConfig {
    pub time_format: String,
    pub enable_ansi: bool,
    pub show_target: bool,
    pub show_thread_ids: bool,
    pub compact_format: bool,
    pub console_level: LevelFilter,
    pub file_level: LevelFilter,
}
Expand description

日志配置结构

Fields§

§time_format: String

时间格式字符串

§enable_ansi: bool

是否启用ANSI颜色(控制台)

§show_target: bool

是否显示目标模块

§show_thread_ids: bool

是否显示线程ID

§compact_format: bool

是否使用紧凑格式

§console_level: LevelFilter

控制台日志级别

§file_level: LevelFilter

文件日志级别

Implementations§

Source§

impl LogConfig

Source

pub fn new() -> Self

创建新的日志配置

Examples found in repository?
examples/beautiful_logs_custom.rs (line 18)
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}
Source

pub fn time_format(self, format: impl Into<String>) -> Self

设置时间格式

Examples found in repository?
examples/beautiful_logs_custom.rs (line 19)
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}
Source

pub fn ansi(self, enable: bool) -> Self

启用/禁用ANSI颜色

Examples found in repository?
examples/beautiful_logs_custom.rs (line 20)
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}
Source

pub fn target(self, show: bool) -> Self

显示/隐藏目标模块

Examples found in repository?
examples/beautiful_logs_custom.rs (line 21)
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}
Source

pub fn thread_ids(self, show: bool) -> Self

显示/隐藏线程ID

Examples found in repository?
examples/beautiful_logs_custom.rs (line 22)
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}
Source

pub fn compact(self, enable: bool) -> Self

设置紧凑格式

Examples found in repository?
examples/beautiful_logs_custom.rs (line 23)
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}
Source

pub fn console_level(self, level: LevelFilter) -> Self

设置控制台日志级别

Examples found in repository?
examples/beautiful_logs_custom.rs (line 24)
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}
Source

pub fn file_level(self, level: LevelFilter) -> Self

设置文件日志级别

Examples found in repository?
examples/beautiful_logs_custom.rs (line 25)
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}

Trait Implementations§

Source§

impl Clone for LogConfig

Source§

fn clone(&self) -> LogConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LogConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for LogConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more