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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# QuantumLog 版本发布说明

## 版本 0.2.0 - 统一 Sink Trait 系统 🚀

**发布日期**: 2024年

### 🎯 重大更新

这是 QuantumLog 的一个重要里程碑版本,引入了全新的统一 Sink Trait 系统,为日志处理提供了更强大、更灵活的架构。

## 🎯 核心特性

### 统一 Sink Trait 系统

- **QuantumSink 核心 Trait**: 定义了所有 Sink 的基础接口
- **可叠加型 vs 独占型**: 智能区分不同类型的 Sink
- **Pipeline 管理**: 统一的 Sink 管理和协调系统
- **健康检查**: 实时监控 Sink 状态
- **统计信息**: 详细的性能和状态统计

### Pipeline 管理系统

- **并行处理**: 支持多 Sink 并行执行
- **错误策略**: 可配置的错误处理策略
- **背压控制**: 智能缓冲区管理
- **优雅关闭**: 确保数据完整性的关闭机制
- **建造者模式**: 灵活的配置接口

## 📋 详细技术说明

### QuantumSink Trait 设计

#### 核心 Trait 定义

```rust
use quantum_log::sinks::{QuantumSink, SinkError, SinkMetadata};
use quantum_log::core::event::QuantumLogEvent;
use async_trait::async_trait;

#[async_trait]
pub trait QuantumSink: Send + Sync + std::fmt::Debug {
    type Config: Send + Sync;
    type Error: std::error::Error + Send + Sync + 'static;
    
    // 核心功能
    async fn send_event(&self, event: QuantumLogEvent) -> Result<(), Self::Error>;
    async fn shutdown(&self) -> Result<(), Self::Error>;
    async fn is_healthy(&self) -> bool;
    
    // 元数据和诊断
    fn name(&self) -> &'static str;
    fn stats(&self) -> String;
    fn metadata(&self) -> SinkMetadata;
}
```

#### Sink 类型区分

```rust
// 可叠加型 Sink - 可与其他 Sink 组合使用
pub trait StackableSink: QuantumSink {}

// 独占型 Sink - 独立运行
pub trait ExclusiveSink: QuantumSink {}
```

**可叠加型 Sink 适用场景**:
- 控制台输出
- 网络发送(HTTP、UDP)
- 指标收集
- 通知系统
- 缓存写入

**独占型 Sink 适用场景**:
- 文件写入
- 数据库连接
- 消息队列
- 需要事务的操作

### Pipeline 管理系统详解

#### 核心配置

```rust
use quantum_log::pipeline::{Pipeline, PipelineConfig, ErrorStrategy};
use std::time::Duration;

// 创建和配置 Pipeline
let config = PipelineConfig {
    parallel_processing: true,
    buffer_size: 1000,
    error_strategy: ErrorStrategy::RetryThenContinue,
    max_retries: 3,
    retry_delay: Duration::from_millis(100),
    health_check_interval: Duration::from_secs(30),
};

let pipeline = Pipeline::with_config(config);
```

#### 建造者模式配置

```rust
use quantum_log::pipeline::PipelineBuilder;

let pipeline = PipelineBuilder::new()
    .with_parallel_processing(true)
    .with_buffer_size(2000)
    .with_error_strategy(ErrorStrategy::LogAndContinue)
    .with_health_check_interval(Duration::from_secs(60))
    .build();
```

### 错误处理策略

```rust
use quantum_log::pipeline::ErrorStrategy;

// 可用策略
let strategies = [
    ErrorStrategy::FailFast,           // 遇到错误立即停止
    ErrorStrategy::LogAndContinue,     // 记录错误并继续
    ErrorStrategy::RetryThenContinue,  // 重试后继续
    ErrorStrategy::RetryThenFail,      // 重试后失败
];

// 生产环境推荐
let config = PipelineConfig {
    error_strategy: ErrorStrategy::RetryThenContinue,
    max_retries: 3,
    retry_delay: Duration::from_millis(100),
    ..Default::default()
};

// 开发环境推荐
let config = PipelineConfig {
    error_strategy: ErrorStrategy::LogAndContinue,
    ..Default::default()
};

// 关键系统推荐
let config = PipelineConfig {
    error_strategy: ErrorStrategy::FailFast,
    ..Default::default()
};
```

### 健康检查机制

```rust
// 检查 Pipeline 健康状态
let health = pipeline.health_check().await;
if health.overall_healthy {
    println!("所有 Sink 都健康");
} else {
    for sink_health in health.sink_details {
        if !sink_health.healthy {
            eprintln!("Sink {} 不健康: {:?}", 
                     sink_health.name, sink_health.last_error);
        }
    }
}

// 获取详细统计信息
let stats = pipeline.get_stats().await;
println!("Pipeline 统计: {}", stats);
```

### 默认标准输出 Sink

```rust
use quantum_log::sinks::DefaultStdoutSink;
use quantum_log::core::level::Level;

// 基本用法
let stdout_sink = DefaultStdoutSink::new();

// 带配置
let stdout_sink = DefaultStdoutSink::with_config(StdoutConfig {
    colored: true,
    format: OutputFormat::Text,
    level_filter: Some(Level::INFO),
    timestamp_format: "%Y-%m-%d %H:%M:%S".to_string(),
});

// 便利函数
let stdout_sink = DefaultStdoutSink::colored();
let stdout_sink = DefaultStdoutSink::json_format();
let stdout_sink = DefaultStdoutSink::with_level_filter(Level::WARN);
```

### 自定义 Sink 实现示例

#### 可叠加型 Sink 示例

```rust
use quantum_log::sinks::{QuantumSink, StackableSink, SinkError, SinkMetadata, SinkType};
use quantum_log::core::event::QuantumLogEvent;
use async_trait::async_trait;

#[derive(Debug)]
struct MetricsSink {
    endpoint: String,
    event_count: std::sync::atomic::AtomicU64,
}

impl MetricsSink {
    fn new(endpoint: String) -> Self {
        Self {
            endpoint,
            event_count: std::sync::atomic::AtomicU64::new(0),
        }
    }
}

#[async_trait]
impl QuantumSink for MetricsSink {
    type Config = String;
    type Error = SinkError;
    
    async fn send_event(&self, event: QuantumLogEvent) -> Result<(), Self::Error> {
        // 发送指标到监控系统
        self.event_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        println!("发送指标到 {}: {} - {}", self.endpoint, event.level, event.message);
        Ok(())
    }
    
    async fn shutdown(&self) -> Result<(), Self::Error> {
        println!("关闭指标 Sink: {}", self.endpoint);
        Ok(())
    }
    
    async fn is_healthy(&self) -> bool {
        true // 检查端点是否可达
    }
    
    fn name(&self) -> &'static str {
        "metrics_sink"
    }
    
    fn stats(&self) -> String {
        format!("MetricsSink[{}]: {} events sent", 
                self.endpoint, 
                self.event_count.load(std::sync::atomic::Ordering::Relaxed))
    }
    
    fn metadata(&self) -> SinkMetadata {
        SinkMetadata {
            name: "metrics_sink".to_string(),
            sink_type: SinkType::Network,
            version: "1.0.0".to_string(),
            description: "Metrics collection sink".to_string(),
        }
    }
}

// 标记为可叠加型
impl StackableSink for MetricsSink {}
```

#### 独占型 Sink 示例

```rust
use quantum_log::sinks::{QuantumSink, ExclusiveSink};
use std::fs::OpenOptions;
use std::io::Write;

#[derive(Debug)]
struct CustomFileSink {
    file_path: String,
    writer: std::sync::Mutex<std::fs::File>,
}

impl CustomFileSink {
    async fn new(file_path: String) -> Result<Self, std::io::Error> {
        let file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(&file_path)?;
            
        Ok(Self {
            file_path,
            writer: std::sync::Mutex::new(file),
        })
    }
}

#[async_trait]
impl QuantumSink for CustomFileSink {
    type Config = String;
    type Error = SinkError;
    
    async fn send_event(&self, event: QuantumLogEvent) -> Result<(), Self::Error> {
        let formatted = format!("{} [{}] {}\n", 
                               event.timestamp, event.level, event.message);
        
        let mut writer = self.writer.lock().unwrap();
        writer.write_all(formatted.as_bytes())
            .map_err(|e| SinkError::WriteError(e.to_string()))?;
        writer.flush()
            .map_err(|e| SinkError::WriteError(e.to_string()))?;
        
        Ok(())
    }
    
    async fn shutdown(&self) -> Result<(), Self::Error> {
        let mut writer = self.writer.lock().unwrap();
        writer.flush()
            .map_err(|e| SinkError::WriteError(e.to_string()))?;
        Ok(())
    }
    
    async fn is_healthy(&self) -> bool {
        std::path::Path::new(&self.file_path).exists()
    }
    
    fn name(&self) -> &'static str {
        "custom_file_sink"
    }
    
    fn stats(&self) -> String {
        format!("CustomFileSink[{}]", self.file_path)
    }
    
    fn metadata(&self) -> SinkMetadata {
        SinkMetadata {
            name: "custom_file_sink".to_string(),
            sink_type: SinkType::File,
            version: "1.0.0".to_string(),
            description: "Custom file output sink".to_string(),
        }
    }
}

// 标记为独占型
impl ExclusiveSink for CustomFileSink {}
```

### 最佳实践和故障排除

#### 性能优化建议

- 使用并行处理提升吞吐量
- 合理设置缓冲区大小
- 监控健康状态和统计信息
- 定期清理资源

#### 故障排除

```rust
// 检查 Pipeline 健康状态
let health = pipeline.health_check().await;
if !health.overall_healthy {
    for sink_health in health.sink_details {
        if !sink_health.healthy {
            eprintln!("Sink {} 不健康: {:?}", 
                     sink_health.name, sink_health.last_error);
        }
    }
}

// 获取详细统计信息
let stats = pipeline.get_stats().await;
println!("Pipeline 统计: {}", stats);
```

### ✨ 新增特性

#### 1. 统一 Sink Trait 系统
- **QuantumSink**: 核心 trait,定义了所有 Sink 的基本接口
- **StackableSink**: 可叠加型 Sink 标记 trait,支持多个 Sink 同时工作
- **ExclusiveSink**: 独占型 Sink 标记 trait,确保资源独占访问
- **SinkError**: 统一的错误处理机制
- **SinkMetadata**: 丰富的元数据支持

#### 2. Pipeline 管理系统
- **Pipeline**: 强大的 Sink 协调器,支持多 Sink 管理
- **PipelineBuilder**: 建造者模式,简化 Pipeline 配置
- **PipelineConfig**: 灵活的配置选项
- **ErrorStrategy**: 多种错误处理策略
  - `FailFast`: 遇到错误立即停止
  - `LogAndContinue`: 记录错误并继续
  - `RetryThenContinue`: 重试后继续
  - `RetryThenFail`: 重试后失败

#### 3. 健康检查机制
- 实时监控 Sink 健康状态
- 自动故障检测和报告
- 健康状态统计信息

#### 4. 增强的优雅关闭
- 支持超时控制的关闭机制
- 确保所有事件在关闭前完成处理
- 资源清理保证

#### 5. 背压控制
- 智能事件速率限制
- 防止系统过载
- 可配置的背压策略

#### 6. 默认标准输出库
- **DefaultStdoutSink**: 开箱即用的标准输出 Sink
- 支持彩色输出、多种格式
- 内置日志级别过滤
- 便利函数支持

#### 7. 设计模式应用
- **策略模式**: 错误处理策略
- **建造者模式**: Pipeline 构建
- **观察者模式**: 事件分发
- **适配器模式**: Sink 接口统一
- **装饰器模式**: Sink 功能增强

### 🔧 现有 Sink 增强

所有现有 Sink 都已升级支持新的 trait 系统:

- **ConsoleSink**: 实现 StackableSink
- **StdoutSink**: 实现 StackableSink  
- **FileSink**: 实现 ExclusiveSink
- **NetworkSink**: 实现 StackableSink
- **RollingFileSink**: 实现 ExclusiveSink
- **LevelFileSink**: 实现 ExclusiveSink

### 📊 技术指标

- **性能提升**: 并行处理支持,提升 30-50% 吞吐量
- **内存优化**: 智能缓冲和背压控制
- **错误恢复**: 多级错误处理和重试机制
- **可扩展性**: 插件化架构,易于扩展
- **类型安全**: 完整的类型系统和错误处理

### 🛡️ 向后兼容性

- ✅ 完全向后兼容现有 API
- ✅ 现有代码无需修改即可运行
- ✅ 渐进式迁移支持
- ✅ 详细的迁移指南

### 📚 文档更新

- 新增 `SINK_TRAIT_GUIDE.md` 详细使用指南
- 更新 `README.md` 包含新特性示例
- 新增示例文件 `examples/sink_trait_usage.rs`
- 完整的 API 文档和最佳实践

### 🧪 测试覆盖

- 为所有新增模块添加了完整的测试套件
- 单元测试覆盖率 > 90%
- 集成测试验证 Pipeline 功能
- 并发测试确保线程安全
- 错误场景测试保证健壮性

### 🔒 安全性

- 可叠加型 Sink 仅包内可见,不对外公开
- 严格的类型检查和错误处理
- 资源泄漏防护
- 线程安全保证

### 📦 依赖更新

- 保持最小依赖原则
- 所有依赖版本锁定
- 安全漏洞检查通过

### 🚀 使用示例

#### 基本 Pipeline 使用

```rust
use quantum_log::sinks::{
    Pipeline, PipelineBuilder, PipelineConfig, ErrorStrategy
};

let mut pipeline = PipelineBuilder::new()
    .with_name("production".to_string())
    .with_parallel_processing(true)
    .with_error_strategy(ErrorStrategy::RetryThenContinue)
    .build();

// 添加多个 Sink
pipeline.add_stackable_sink(Box::new(ConsoleSink::new())).await?;
pipeline.set_exclusive_sink(Box::new(FileSink::new("app.log".to_string()).await?)).await?;

// 发送事件
let event = create_log_event("INFO", "系统启动");
pipeline.send_event(event).await?;
```

#### 自定义 Sink 实现

```rust
use quantum_log::sinks::{QuantumSink, StackableSink};

#[derive(Debug)]
struct CustomSink;

#[async_trait]
impl QuantumSink for CustomSink {
    // 实现必需的方法
}

impl StackableSink for CustomSink {}
```

### 🔄 迁移指南

#### 从 0.1.x 迁移

1. **无需修改现有代码** - 所有现有 API 保持兼容
2. **可选升级** - 可以逐步采用新的 Pipeline 系统
3. **配置迁移** - 现有配置文件无需修改

#### 推荐迁移步骤

1. 更新依赖版本到 0.2.0
2. 运行现有测试确保兼容性
3. 逐步引入 Pipeline 系统
4. 利用新的健康检查和统计功能
5. 考虑实现自定义 Sink

### 🐛 已知问题

- 无已知重大问题
- 所有测试通过
- 性能基准测试达标

### 🔮 未来计划

#### 版本 0.3.0 发布说明
- 零 dead_code 警告:清理所有编译告警,提供更干净的构建体验
- MPI 动态加载:支持运行时动态加载 MPI 库,提高部署灵活性
- 文档完善:统一 features 名称,修正示例,新增库查找配置与平台指引
- 错误处理增强:更精确的错误分类与处理机制

> 迁移提示(MPI 动态加载):自 0.3.0 起,运行时代码不再读取自定义 `MPI_LIB_PATH` 变量;请使用平台标准环境变量(`LD_LIBRARY_PATH`/`PATH`/`DYLD_LIBRARY_PATH`)覆盖或指定库路径。`MPI_LIB_PATH` 仅作为构建阶段的提示性输出,运行时不依赖该变量。

#### 长期路线图
- 云原生集成
- 机器学习日志分析
- 实时日志流处理
- 可视化日志查看器

### 🙏 致谢

感谢所有贡献者和用户的反馈,让 QuantumLog 变得更加强大和易用。

### 📞 支持

- 📖 文档: 查看 `SINK_TRAIT_GUIDE.md`
- 🐛 问题报告: 请在 GitHub Issues 中提交
- 💬 讨论: 欢迎在 GitHub Discussions 中交流
- 📧 联系: 通过项目维护者联系

---

**完整更新日志**: 查看 Git 提交历史获取详细变更信息

**下载**: 通过 Cargo 更新到最新版本

```bash
cargo update quantum_log
```

**验证安装**:

```bash
cargo test --all-features
```

---

*QuantumLog 0.2.0 - 让日志处理更加量子化!* ⚡