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
impl LogConfig
Sourcepub fn new() -> Self
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}Sourcepub fn time_format(self, format: impl Into<String>) -> Self
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}Sourcepub fn ansi(self, enable: bool) -> Self
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}Sourcepub fn target(self, show: bool) -> Self
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}Sourcepub fn thread_ids(self, show: bool) -> Self
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}Sourcepub fn compact(self, enable: bool) -> Self
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}Sourcepub fn console_level(self, level: LevelFilter) -> Self
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}Sourcepub fn file_level(self, level: LevelFilter) -> Self
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§
Auto Trait Implementations§
impl Freeze for LogConfig
impl RefUnwindSafe for LogConfig
impl Send for LogConfig
impl Sync for LogConfig
impl Unpin for LogConfig
impl UnwindSafe for LogConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more