quantum_log 0.3.0

High-performance asynchronous logging framework based on tracing ecosystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! QuantumLog 完整示例集合
//! 这个文件包含了所有主要使用场景的完整、可运行的示例代码

use quantum_log::config::*;
use quantum_log::{get_buffer_stats, get_diagnostics, QuantumLogConfig};
use serde_json::json;
use std::time::{Duration, Instant};
use tokio::time::sleep;
use tracing::{debug, error, info, span, warn, Level};

/// 示例1: 基本使用
async fn example_basic_usage() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 示例1: 基本使用 ===");

    // 使用标准 tracing 宏
    info!("应用程序启动");
    warn!("这是一个警告");
    error!("这是一个错误");
    debug!("调试信息");

    println!("基本使用示例完成");
    Ok(())
}

/// 示例2: 使用设计文档推荐的 API
async fn example_design_document_api() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 示例2: 设计文档 API ===");

    info!("使用 QuantumLog 记录日志");
    warn!("警告消息");
    error!("错误消息");

    println!("设计文档 API 示例完成");
    Ok(())
}

/// 示例3: 自定义配置
async fn example_custom_config() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 示例3: 自定义配置 ===");

    debug!("调试消息现在会被记录");
    info!("应用程序已配置");
    error!("这是一个错误");

    println!("自定义配置示例完成");
    Ok(())
}

/// 示例4: 使用构建器模式
async fn example_builder_pattern() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 示例4: 构建器模式 ===");

    // 创建 span 进行结构化日志记录
    let span = span!(Level::INFO, "user_operation", user_id = 12345);
    let _enter = span.enter();

    info!("用户操作开始");
    info!(action = "login", result = "success", "用户登录成功");

    println!("构建器模式示例完成");
    Ok(())
}

/// 示例5: 结构化日志记录
async fn example_structured_logging() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 示例5: 结构化日志记录 ===");

    // 基本结构化日志记录
    info!(user_id = 12345, action = "login", "用户登录");
    warn!(error_code = 404, path = "/api/users", "API 路径未找到");

    // 复杂数据结构
    let user_data = json!({
        "id": 12345,
        "name": "张三",
        "email": "zhangsan@example.com",
        "roles": ["user", "admin"]
    });
    info!(user = %user_data, "用户数据已更新");

    // 使用 span 进行上下文跟踪
    let request_span = span!(
        Level::INFO,
        "http_request",
        method = "POST",
        path = "/api/users",
        request_id = "req-123"
    );

    let _enter = request_span.enter();
    info!("处理 HTTP 请求");
    info!(status = 200, duration_ms = 45, "请求完成");

    // 嵌套 span
    let db_span = span!(Level::DEBUG, "database_query", table = "users");
    let _db_enter = db_span.enter();
    info!(query = "SELECT * FROM users WHERE id = ?", "执行数据库查询");

    println!("结构化日志记录示例完成");
    Ok(())
}

/// 示例6: 错误处理和诊断
async fn example_error_handling_diagnostics() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 示例6: 错误处理和诊断 ===");

    // 记录一些消息
    for i in 0..100 {
        info!(iteration = i, "处理迭代 {}", i);
        if i % 10 == 0 {
            error!(iteration = i, "模拟错误");
        }
    }

    // 获取缓冲区统计信息
    if let Some(stats) = get_buffer_stats() {
        info!(
            current_size = stats.current_size,
            dropped_count = stats.dropped_count,
            is_initialized = stats.is_initialized,
            "缓冲区统计信息"
        );
    }

    // 获取诊断信息
    let diagnostics = get_diagnostics();
    info!(
        events_processed = diagnostics.events_processed,
        events_dropped = diagnostics.events_dropped_backpressure + diagnostics.events_dropped_error,
        sink_errors = diagnostics.sink_errors,
        uptime_seconds = diagnostics.uptime.map(|d| d.as_secs()).unwrap_or(0),
        success_rate = format!(
            "{:.2}%",
            if diagnostics.events_processed > 0 {
                (diagnostics.events_processed as f64
                    / (diagnostics.events_processed
                        + diagnostics.events_dropped_backpressure
                        + diagnostics.events_dropped_error
                        + diagnostics.total_events_dropped) as f64)
                    * 100.0
            } else {
                0.0
            }
        ),
        "诊断信息"
    );

    println!("错误处理和诊断示例完成");
    Ok(())
}

/// 示例7: 高级配置
async fn example_advanced_config() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 示例7: 高级配置 ===");

    QuantumLogConfig {
        global_level: "DEBUG".to_string(),
        pre_init_buffer_size: Some(2000),
        pre_init_stdout_enabled: true,
        backpressure_strategy: BackpressureStrategy::Drop,

        // 配置标准输出
        stdout: Some(StdoutConfig {
            enabled: true,
            level: Some("INFO".to_string()),
            color_enabled: Some(true),
            level_colors: None,
            format: OutputFormat::Json,
            colored: true,
        }),

        // 配置文件输出
        file: Some(quantum_log::config::FileSinkConfig {
            enabled: true,
            level: Some("DEBUG".to_string()),
            output_type: FileOutputType::Json,
            directory: std::path::PathBuf::from("./logs"),
            filename_base: "quantum".to_string(),
            extension: Some("log".to_string()),
            separation_strategy: FileSeparationStrategy::None,
            write_buffer_size: 16384,
            rotation: Some(RotationConfig {
                strategy: RotationStrategy::Size,
                max_size_mb: Some(50),
                max_files: Some(5),
                compress_rotated_files: false,
            }),
            writer_cache_ttl_seconds: 600,
            writer_cache_capacity: 2048,
        }),

        // 配置上下文字段
        context_fields: ContextFieldsConfig {
            timestamp: true,
            level: true,
            target: true,
            file_line: true,
            pid: true,
            tid: true,
            mpi_rank: false,
            username: true,
            hostname: true,
            span_info: true,
        },

        // 配置格式
        format: LogFormatConfig {
            timestamp_format: "%Y-%m-%d %H:%M:%S%.3f".to_string(),
            template: "{timestamp} [{level}] {target} - {message}".to_string(),
            csv_columns: None,
            json_flatten_fields: false,
            json_fields_key: "fields".to_string(),
            format_type: LogFormatType::Json,
        },

        // 数据库配置(需要 database 特性)
        #[cfg(feature = "database")]
        database: Some(DatabaseSinkConfig {
            enabled: false, // 在示例中禁用以避免依赖数据库
            level: Some("WARN".to_string()),
            db_type: DatabaseType::Postgresql,
            connection_string: "postgresql://user:pass@localhost/logs".to_string(),
            schema_name: None,
            table_name: "quantum_logs".to_string(),
            batch_size: 200,
            connection_pool_size: 10,
            connection_timeout_ms: 10000,
            auto_create_table: true,
        }),

        #[cfg(not(feature = "database"))]
        database: None,
    };

    info!("高级配置已初始化");
    warn!(component = "auth", "认证模块警告");
    error!(error_code = "E001", module = "database", "数据库连接失败");

    println!("高级配置示例完成");
    Ok(())
}

/// 示例8: 性能测试和基准测试
async fn example_performance_test() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 示例8: 性能测试 ===");

    let start_time = Instant::now();
    let num_messages = 1000; // 减少消息数量以便快速演示

    info!("开始性能测试,将记录 {} 条消息", num_messages);

    // 高频日志记录测试
    for i in 0..num_messages {
        match i % 4 {
            0 => info!(id = i, "信息日志 {}", i),
            1 => warn!(id = i, "警告日志 {}", i),
            2 => error!(id = i, "错误日志 {}", i),
            _ => info!(
                id = i,
                data = format!("complex_data_{}", i),
                "结构化日志 {}",
                i
            ),
        }

        // 每 100 条消息暂停一下以模拟真实应用
        if i % 100 == 0 && i > 0 {
            sleep(Duration::from_millis(1)).await;
        }
    }

    let elapsed = start_time.elapsed();
    let messages_per_second = num_messages as f64 / elapsed.as_secs_f64();

    info!(
        total_messages = num_messages,
        elapsed_ms = elapsed.as_millis(),
        messages_per_second = format!("{:.2}", messages_per_second),
        "性能测试完成"
    );

    // 获取最终诊断信息
    let diagnostics = get_diagnostics();
    info!(
        events_processed = diagnostics.events_processed,
        events_dropped = diagnostics.events_dropped_backpressure + diagnostics.events_dropped_error,
        success_rate = format!(
            "{:.4}%",
            if diagnostics.events_processed > 0 {
                ((diagnostics.events_processed - diagnostics.total_events_dropped) as f64
                    / diagnostics.events_processed as f64)
                    * 100.0
            } else {
                100.0
            }
        ),
        "最终统计信息"
    );
    println!("性能测试示例完成");
    Ok(())
}

/// 示例9: MPI 环境使用(需要 mpi_support 特性)
#[cfg(feature = "mpi_support")]
async fn example_mpi_usage() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 示例9: MPI 环境使用 ===");

    use quantum_log::mpi::*;

    // 检查 MPI 是否可用
    if is_mpi_available() {
        let rank = get_mpi_rank().unwrap_or(0);

        let config = QuantumLogConfig {
            global_level: "INFO".to_string(),
            context_fields: ContextFieldsConfig {
                mpi_rank: true,
                ..Default::default()
            },
            file: Some(FileSinkConfig {
                enabled: true,
                level: Some("DEBUG".to_string()),
                output_type: FileOutputType::Json,
                directory: std::path::PathBuf::from(format!("./logs/rank_{}", rank)),
                filename_base: format!("quantum_rank_{}", rank),
                extension: Some("log".to_string()),
                separation_strategy: FileSeparationStrategy::None,
                write_buffer_size: 8192,
                rotation: None,
                writer_cache_ttl_seconds: 300,
                writer_cache_capacity: 1024,
            }),
            ..Default::default()
        };

        // 使用配置初始化日志系统
        quantum_log::init_with_config(config).await?;

        info!(rank = rank, "MPI 进程启动");

        // 模拟 MPI 工作负载
        for i in 0..10 {
            info!(rank = rank, iteration = i, "处理数据块 {}", i);
            sleep(Duration::from_millis(100)).await;
        }

        warn!(rank = rank, "MPI 进程即将结束");
    } else {
        println!("MPI 不可用,使用标准模式");
        info!("标准模式启动");
    }

    println!("MPI 使用示例完成");
    Ok(())
}

#[cfg(not(feature = "mpi_support"))]
#[allow(dead_code)]
async fn example_mpi_usage() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 示例9: MPI 环境使用 ===");
    println!("MPI 特性未启用,使用标准模式");

    quantum_log::init().await?;
    info!("标准模式启动");
    quantum_log::shutdown().await?;

    println!("MPI 使用示例完成(标准模式)");
    Ok(())
}

/// 示例10: 错误处理详细演示
async fn example_detailed_error_handling() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 示例10: 详细错误处理 ===");

    // 记录各种类型的错误
    error!(error_type = "validation", "数据验证失败");
    error!(error_type = "network", code = 500, "网络连接错误");
    error!(error_type = "database", "数据库操作失败");

    // 演示错误恢复
    info!("尝试错误恢复");
    warn!("系统正在恢复中");
    info!("错误恢复完成");

    println!("详细错误处理示例完成");
    Ok(())
}

/// 主函数 - 运行所有示例
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("QuantumLog 完整示例集合");
    println!("=========================\n");

    // 统一初始化日志系统
    quantum_log::init().await?;

    // 运行所有示例
    let examples = vec![
        "基本使用",
        "设计文档 API",
        "自定义配置",
        "构建器模式",
        "结构化日志记录",
        "错误处理和诊断",
        "高级配置",
        "性能测试",
        "详细错误处理",
    ];

    #[cfg(feature = "mpi_support")]
    let mpi_examples = vec!["MPI 环境使用"];

    // 合并所有示例
    let mut all_examples = examples;
    #[cfg(feature = "mpi_support")]
    all_examples.extend(mpi_examples);

    for name in all_examples {
        println!("\n运行示例: {}", name);
        println!("{}", "=".repeat(50));

        let result = match name {
            "基本使用" => example_basic_usage().await,
            "设计文档 API" => example_design_document_api().await,
            "自定义配置" => example_custom_config().await,
            "构建器模式" => example_builder_pattern().await,
            "结构化日志记录" => example_structured_logging().await,
            "错误处理和诊断" => example_error_handling_diagnostics().await,
            "高级配置" => example_advanced_config().await,
            "性能测试" => example_performance_test().await,
            "详细错误处理" => example_detailed_error_handling().await,
            #[cfg(feature = "mpi_support")]
            "MPI 环境使用" => example_mpi_usage().await,
            _ => {
                eprintln!("❌ 未知示例: {}", name);
                continue;
            }
        };

        match result {
            Ok(_) => println!("{} 示例成功完成", name),
            Err(e) => {
                eprintln!("{} 示例失败: {}", name, e);
                // 继续运行其他示例
            }
        }

        // 在示例之间稍作停顿
        sleep(Duration::from_millis(500)).await;
    }

    // 统一关闭日志系统
    quantum_log::shutdown().await?;

    println!("\n🎉 所有示例运行完成!");
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_basic_usage() {
        assert!(example_basic_usage().await.is_ok());
    }

    #[tokio::test]
    async fn test_design_document_api() {
        assert!(example_design_document_api().await.is_ok());
    }

    #[tokio::test]
    async fn test_custom_config() {
        assert!(example_custom_config().await.is_ok());
    }

    #[tokio::test]
    async fn test_structured_logging() {
        assert!(example_structured_logging().await.is_ok());
    }

    #[tokio::test]
    async fn test_error_handling_diagnostics() {
        assert!(example_error_handling_diagnostics().await.is_ok());
    }

    #[tokio::test]
    async fn test_performance_test() {
        assert!(example_performance_test().await.is_ok());
    }

    #[tokio::test]
    async fn test_detailed_error_handling() {
        assert!(example_detailed_error_handling().await.is_ok());
    }
}