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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
//! 定义 QuantumLog (灵迹) 日志框架的所有配置结构体。

use serde::Deserialize;
use std::collections::HashMap;
use std::path::PathBuf;

// --- 辅助函数,用于提供配置项的默认值 ---
fn default_global_level() -> String {
    "INFO".to_string()
}
fn default_true() -> bool {
    true
}
fn default_false() -> bool {
    false
}
fn default_true_option() -> Option<bool> {
    Some(true)
}
fn default_timestamp_format() -> String {
    "%Y-%m-%d %H:%M:%S%.3f".to_string()
}
fn default_log_template() -> String {
    "{timestamp} [{level}] {target} - {message}".to_string()
}
fn default_json_fields_key() -> String {
    "fields".to_string()
}
fn default_file_sink_filename_base() -> String {
    "quantum".to_string()
}
fn default_file_buffer_size() -> usize {
    8192
}
fn default_writer_cache_ttl_seconds() -> u64 {
    300
} // 5 minutes
fn default_writer_cache_capacity() -> u64 {
    1024
}
fn default_db_table_name() -> String {
    "quantum_logs".to_string()
}
fn default_db_batch_size() -> usize {
    100
}
fn default_db_pool_size() -> u32 {
    5
}
fn default_db_connection_timeout_ms() -> u64 {
    5000
}
fn default_output_format() -> OutputFormat {
    OutputFormat::Text
}

/// 定义当日志通道已满时的背压处理策略。
#[derive(Deserialize, Debug, Clone, PartialEq, Eq, Default)]
pub enum BackpressureStrategy {
    /// 阻塞直到通道有空间。
    Block,
    /// 如果通道已满,立即丢弃最新的日志。
    #[default]
    Drop,
}

/// QuantumLog (灵迹) 日志框架的顶层配置结构体。
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct QuantumLoggerConfig {
    #[serde(default = "default_global_level")]
    pub global_level: String,
    pub pre_init_buffer_size: Option<usize>,
    #[serde(default = "default_false")]
    pub pre_init_stdout_enabled: bool,
    #[serde(default)]
    pub backpressure_strategy: BackpressureStrategy,
    pub stdout: Option<StdoutConfig>,
    pub file: Option<FileSinkConfig>,
    pub database: Option<DatabaseSinkConfig>,
    #[serde(default)]
    pub context_fields: ContextFieldsConfig,
    #[serde(default)]
    pub format: LogFormatConfig,
}

impl Default for QuantumLoggerConfig {
    fn default() -> Self {
        Self {
            global_level: default_global_level(),
            pre_init_buffer_size: None,
            pre_init_stdout_enabled: default_false(),
            backpressure_strategy: BackpressureStrategy::default(),
            stdout: None,
            file: None,
            database: None,
            context_fields: ContextFieldsConfig::default(),
            format: LogFormatConfig::default(),
        }
    }
}

/// 控制哪些背景信息字段需要被采集和包含在日志输出中。
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ContextFieldsConfig {
    #[serde(default = "default_true")]
    pub timestamp: bool,
    #[serde(default = "default_true")]
    pub level: bool,
    #[serde(default = "default_true")]
    pub target: bool,
    #[serde(default = "default_false")]
    pub file_line: bool,
    #[serde(default = "default_false")]
    pub pid: bool,
    #[serde(default = "default_false")]
    pub tid: bool,
    #[serde(default = "default_false")]
    pub mpi_rank: bool,
    #[serde(default = "default_false")]
    pub username: bool,
    #[serde(default = "default_false")]
    pub hostname: bool,
    #[serde(default = "default_false")]
    pub span_info: bool,
}

impl Default for ContextFieldsConfig {
    fn default() -> Self {
        Self {
            timestamp: default_true(),
            level: default_true(),
            target: default_true(),
            file_line: default_false(),
            pid: default_false(),
            tid: default_false(),
            mpi_rank: default_false(),
            username: default_false(),
            hostname: default_false(),
            span_info: default_false(),
        }
    }
}

/// 日志级别枚举
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

impl std::str::FromStr for LogLevel {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_uppercase().as_str() {
            "TRACE" => Ok(LogLevel::Trace),
            "DEBUG" => Ok(LogLevel::Debug),
            "INFO" => Ok(LogLevel::Info),
            "WARN" => Ok(LogLevel::Warn),
            "ERROR" => Ok(LogLevel::Error),
            _ => Err(format!("Invalid log level: {}", s)),
        }
    }
}

/// 日志格式类型
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum LogFormatType {
    Text,
    Json,
    Csv,
}

/// 输出格式
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum OutputFormat {
    Text,
    Json,
    Csv,
}

/// 网络协议
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum NetworkProtocol {
    Tcp,
    Udp,
    Http,
}

/// 轮转策略(别名)
pub type RotationPolicy = RotationStrategy;

/// 日志格式化相关的配置。
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct LogFormatConfig {
    #[serde(default = "default_timestamp_format")]
    pub timestamp_format: String,
    #[serde(default = "default_log_template")]
    pub template: String,
    pub csv_columns: Option<Vec<String>>,
    #[serde(default = "default_false")]
    pub json_flatten_fields: bool,
    #[serde(default = "default_json_fields_key")]
    pub json_fields_key: String,
    #[serde(default = "default_format_type")]
    pub format_type: LogFormatType,
}

fn default_format_type() -> LogFormatType {
    LogFormatType::Text
}

impl Default for LogFormatConfig {
    fn default() -> Self {
        Self {
            timestamp_format: default_timestamp_format(),
            template: default_log_template(),
            csv_columns: None,
            json_flatten_fields: default_false(),
            json_fields_key: default_json_fields_key(),
            format_type: default_format_type(),
        }
    }
}

/// 标准输出 (stdout) 的配置。
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct StdoutConfig {
    #[serde(default = "default_false")]
    pub enabled: bool,
    pub level: Option<String>,
    #[serde(default = "default_true_option")]
    pub color_enabled: Option<bool>,
    pub level_colors: Option<HashMap<String, String>>,
    #[serde(default = "default_output_format")]
    pub format: OutputFormat,
    #[serde(default = "default_true")]
    pub colored: bool,
}

impl Default for StdoutConfig {
    fn default() -> Self {
        Self {
            enabled: default_false(),
            level: None,
            color_enabled: default_true_option(),
            level_colors: None,
            format: default_output_format(),
            colored: default_true(),
        }
    }
}

/// 文件输出类型。
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum FileOutputType {
    Text,
    Csv,
    Json,
}

/// 文件分离策略。
#[derive(Deserialize, Debug, Clone, PartialEq, Eq, Default)]
pub enum FileSeparationStrategy {
    #[default]
    None,
    ByPid,
    ByTid,
    ByMpiRank,
    Level,
    Module,
    Time,
}

/// 日志文件轮转策略。
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum RotationStrategy {
    None,
    Hourly,
    Daily,
    Size,
}

/// 日志文件轮转配置。
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct RotationConfig {
    pub strategy: RotationStrategy,
    pub max_size_mb: Option<u64>,
    pub max_files: Option<usize>,
    #[serde(default = "default_false")]
    pub compress_rotated_files: bool,
}

/// 文件输出管道的配置。
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct FileSinkConfig {
    #[serde(default = "default_false")]
    pub enabled: bool,
    pub level: Option<String>,
    pub output_type: FileOutputType,
    pub directory: PathBuf,
    #[serde(default = "default_file_sink_filename_base")]
    pub filename_base: String,
    pub extension: Option<String>,
    #[serde(default)]
    pub separation_strategy: FileSeparationStrategy,
    #[serde(default = "default_file_buffer_size")]
    pub write_buffer_size: usize,
    pub rotation: Option<RotationConfig>,
    #[serde(default = "default_writer_cache_ttl_seconds")]
    pub writer_cache_ttl_seconds: u64,
    #[serde(default = "default_writer_cache_capacity")]
    pub writer_cache_capacity: u64,
}

/// 支持的数据库类型。
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum DatabaseType {
    Sqlite,
    Mysql,
    Postgresql,
}

/// 文件配置(别名)
pub type FileConfig = FileSinkConfig;

/// 滚动文件配置(别名)
pub type RollingFileConfig = FileSinkConfig;

/// 网络配置
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct NetworkConfig {
    #[serde(default = "default_false")]
    pub enabled: bool,
    pub level: Option<String>,
    pub protocol: NetworkProtocol,
    pub host: String,
    pub port: u16,
    pub format: OutputFormat,
    #[serde(default = "default_file_buffer_size")]
    pub buffer_size: usize,
    pub timeout_ms: Option<u64>,
}

/// 级别文件配置
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct LevelFileConfig {
    #[serde(default = "default_false")]
    pub enabled: bool,
    pub directory: PathBuf,
    #[serde(default = "default_file_sink_filename_base")]
    pub filename_base: String,
    pub extension: Option<String>,
    pub levels: Option<Vec<LogLevel>>,
    pub format: OutputFormat,
    #[serde(default = "default_file_buffer_size")]
    pub buffer_size: usize,
    pub rotation: Option<RotationConfig>,
}

/// QuantumLog配置(别名)
pub type QuantumLogConfig = QuantumLoggerConfig;

/// 数据库输出管道的配置。
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct DatabaseSinkConfig {
    #[serde(default = "default_false")]
    pub enabled: bool,
    pub level: Option<String>,
    pub db_type: DatabaseType,
    pub connection_string: String,
    pub schema_name: Option<String>,
    #[serde(default = "default_db_table_name")]
    pub table_name: String,
    #[serde(default = "default_db_batch_size")]
    pub batch_size: usize,
    #[serde(default = "default_db_pool_size")]
    pub connection_pool_size: u32,
    #[serde(default = "default_db_connection_timeout_ms")]
    pub connection_timeout_ms: u64,
    #[serde(default = "default_true")]
    pub auto_create_table: bool,
}

/// 用于从 TOML 文件加载 `QuantumLoggerConfig` 的辅助函数。
pub fn load_config_from_file(path: &std::path::Path) -> crate::error::Result<QuantumLoggerConfig> {
    use crate::error::QuantumLogError;
    use std::fs;

    if !path.exists() {
        return Err(QuantumLogError::ConfigFileMissing(
            path.to_string_lossy().into_owned(),
        ));
    }

    let config_str = fs::read_to_string(path)?;
    let config: QuantumLoggerConfig = toml::from_str(&config_str)
        .map_err(|e| QuantumLogError::ConfigError(format!("TOML解析失败: {}", e)))?;

    Ok(config)
}

/// 用于从 TOML 字符串加载 `QuantumLoggerConfig` 的辅助函数。
pub fn load_config_from_str(config_str: &str) -> crate::error::Result<QuantumLoggerConfig> {
    use crate::error::QuantumLogError;

    let config: QuantumLoggerConfig = toml::from_str(config_str)
        .map_err(|e| QuantumLogError::ConfigError(format!("TOML解析失败: {}", e)))?;

    Ok(config)
}

/// 验证配置的有效性。
pub fn validate_config(config: &QuantumLoggerConfig) -> crate::error::Result<()> {
    use crate::error::QuantumLogError;

    // 验证全局日志级别
    match config.global_level.to_uppercase().as_str() {
        "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR" => {}
        _ => {
            return Err(QuantumLogError::InvalidLogLevel(
                config.global_level.clone(),
            ))
        }
    }

    // 验证各个sink的日志级别
    if let Some(ref stdout_config) = config.stdout {
        if let Some(ref level) = stdout_config.level {
            match level.to_uppercase().as_str() {
                "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR" => {}
                _ => return Err(QuantumLogError::InvalidLogLevel(level.clone())),
            }
        }
    }

    if let Some(ref file_config) = config.file {
        if let Some(ref level) = file_config.level {
            match level.to_uppercase().as_str() {
                "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR" => {}
                _ => return Err(QuantumLogError::InvalidLogLevel(level.clone())),
            }
        }

        // 验证文件路径
        if !file_config.directory.is_absolute() {
            return Err(QuantumLogError::InvalidPath(format!(
                "文件输出目录必须是绝对路径: {:?}",
                file_config.directory
            )));
        }
    }

    if let Some(ref db_config) = config.database {
        if let Some(ref level) = db_config.level {
            match level.to_uppercase().as_str() {
                "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR" => {}
                _ => return Err(QuantumLogError::InvalidLogLevel(level.clone())),
            }
        }

        // 验证数据库连接字符串不为空
        if db_config.connection_string.trim().is_empty() {
            return Err(QuantumLogError::ConfigError(
                "数据库连接字符串不能为空".to_string(),
            ));
        }
    }

    Ok(())
}

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

    #[test]
    fn test_default_config() {
        let config = QuantumLoggerConfig::default();
        assert_eq!(config.global_level, "INFO");
        assert_eq!(config.pre_init_stdout_enabled, false);
        assert_eq!(config.backpressure_strategy, BackpressureStrategy::Drop);
        assert!(config.stdout.is_none());
        assert!(config.file.is_none());
        assert!(config.database.is_none());
    }

    #[test]
    fn test_context_fields_config_defaults() {
        let config = ContextFieldsConfig::default();
        assert_eq!(config.timestamp, true);
        assert_eq!(config.level, true);
        assert_eq!(config.target, true);
        assert_eq!(config.file_line, false);
        assert_eq!(config.pid, false);
        assert_eq!(config.tid, false);
        assert_eq!(config.mpi_rank, false);
        assert_eq!(config.username, false);
        assert_eq!(config.hostname, false);
        assert_eq!(config.span_info, false);
    }

    #[test]
    fn test_log_format_config_defaults() {
        let config = LogFormatConfig::default();
        assert_eq!(config.timestamp_format, "%Y-%m-%d %H:%M:%S%.3f");
        assert_eq!(
            config.template,
            "{timestamp} [{level}] {target} - {message}"
        );
        assert!(config.csv_columns.is_none());
        assert_eq!(config.json_flatten_fields, false);
        assert_eq!(config.json_fields_key, "fields");
    }

    #[test]
    fn test_stdout_config_defaults() {
        let config = StdoutConfig::default();
        assert_eq!(config.enabled, false);
        assert!(config.level.is_none());
        assert_eq!(config.color_enabled, Some(true));
        assert!(config.level_colors.is_none());
    }

    #[test]
    fn test_backpressure_strategy_default() {
        let strategy = BackpressureStrategy::default();
        assert_eq!(strategy, BackpressureStrategy::Drop);
    }

    #[test]
    fn test_file_separation_strategy_default() {
        let strategy = FileSeparationStrategy::default();
        assert_eq!(strategy, FileSeparationStrategy::None);
    }

    #[test]
    fn test_load_config_from_str_basic() {
        let toml_str = r#"
            global_level = "DEBUG"
            pre_init_stdout_enabled = true
            
            [stdout]
            enabled = true
            level = "INFO"
        "#;

        let config = load_config_from_str(toml_str).unwrap();
        assert_eq!(config.global_level, "DEBUG");
        assert_eq!(config.pre_init_stdout_enabled, true);
        assert!(config.stdout.is_some());

        let stdout_config = config.stdout.unwrap();
        assert_eq!(stdout_config.enabled, true);
        assert_eq!(stdout_config.level, Some("INFO".to_string()));
    }

    #[test]
    fn test_load_config_from_str_with_context_fields() {
        let toml_str = r#"
            global_level = "TRACE"
            
            [context_fields]
            timestamp = true
            level = true
            target = false
            pid = true
            tid = true
            mpi_rank = true
        "#;

        let config = load_config_from_str(toml_str).unwrap();
        assert_eq!(config.global_level, "TRACE");

        let context = &config.context_fields;
        assert_eq!(context.timestamp, true);
        assert_eq!(context.level, true);
        assert_eq!(context.target, false);
        assert_eq!(context.pid, true);
        assert_eq!(context.tid, true);
        assert_eq!(context.mpi_rank, true);
    }

    #[test]
    fn test_load_config_from_str_with_file_config() {
        let toml_str = r#"
            global_level = "INFO"
            
            [file]
            enabled = true
            output_type = "Json"
            directory = "/tmp/logs"
            filename_base = "test"
            extension = "log"
            separation_strategy = "ByPid"
            write_buffer_size = 4096
            
            [file.rotation]
            strategy = "Daily"
            max_files = 7
            compress_rotated_files = true
        "#;

        let config = load_config_from_str(toml_str).unwrap();
        assert!(config.file.is_some());

        let file_config = config.file.unwrap();
        assert_eq!(file_config.enabled, true);
        assert_eq!(file_config.output_type, FileOutputType::Json);
        assert_eq!(file_config.directory, PathBuf::from("/tmp/logs"));
        assert_eq!(file_config.filename_base, "test");
        assert_eq!(file_config.extension, Some("log".to_string()));
        assert_eq!(
            file_config.separation_strategy,
            FileSeparationStrategy::ByPid
        );
        assert_eq!(file_config.write_buffer_size, 4096);

        assert!(file_config.rotation.is_some());
        let rotation = file_config.rotation.unwrap();
        assert_eq!(rotation.strategy, RotationStrategy::Daily);
        assert_eq!(rotation.max_files, Some(7));
        assert_eq!(rotation.compress_rotated_files, true);
    }

    #[test]
    fn test_load_config_from_str_with_database_config() {
        let toml_str = r#"
            global_level = "WARN"
            
            [database]
            enabled = true
            level = "ERROR"
            db_type = "Sqlite"
            connection_string = "sqlite:///tmp/quantum.db"
            table_name = "logs"
            batch_size = 50
            connection_pool_size = 3
            auto_create_table = false
        "#;

        let config = load_config_from_str(toml_str).unwrap();
        assert!(config.database.is_some());

        let db_config = config.database.unwrap();
        assert_eq!(db_config.enabled, true);
        assert_eq!(db_config.level, Some("ERROR".to_string()));
        assert_eq!(db_config.db_type, DatabaseType::Sqlite);
        assert_eq!(db_config.connection_string, "sqlite:///tmp/quantum.db");
        assert_eq!(db_config.table_name, "logs");
        assert_eq!(db_config.batch_size, 50);
        assert_eq!(db_config.connection_pool_size, 3);
        assert_eq!(db_config.auto_create_table, false);
    }

    #[test]
    fn test_validate_config_valid() {
        let mut config = QuantumLoggerConfig::default();
        config.global_level = "INFO".to_string();

        let result = validate_config(&config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_config_invalid_global_level() {
        let mut config = QuantumLoggerConfig::default();
        config.global_level = "INVALID".to_string();

        let result = validate_config(&config);
        assert!(result.is_err());

        if let Err(crate::error::QuantumLogError::InvalidLogLevel(level)) = result {
            assert_eq!(level, "INVALID");
        } else {
            panic!("Expected InvalidLogLevel error");
        }
    }

    #[test]
    fn test_validate_config_invalid_stdout_level() {
        let mut config = QuantumLoggerConfig::default();
        config.stdout = Some(StdoutConfig {
            enabled: true,
            level: Some("INVALID".to_string()),
            color_enabled: Some(true),
            level_colors: None,
            format: crate::config::OutputFormat::Text,
            colored: true,
        });

        let result = validate_config(&config);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_config_invalid_file_path() {
        let mut config = QuantumLoggerConfig::default();
        config.file = Some(FileSinkConfig {
            enabled: true,
            level: None,
            output_type: FileOutputType::Text,
            directory: PathBuf::from("relative/path"), // 相对路径,应该失败
            filename_base: "test".to_string(),
            extension: None,
            separation_strategy: FileSeparationStrategy::None,
            write_buffer_size: 8192,
            rotation: None,
            writer_cache_ttl_seconds: 300,
            writer_cache_capacity: 1024,
        });

        let result = validate_config(&config);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_config_empty_database_connection() {
        let mut config = QuantumLoggerConfig::default();
        config.database = Some(DatabaseSinkConfig {
            enabled: true,
            level: None,
            db_type: DatabaseType::Sqlite,
            connection_string: "".to_string(), // 空连接字符串,应该失败
            schema_name: None,
            table_name: "logs".to_string(),
            batch_size: 100,
            connection_pool_size: 5,
            connection_timeout_ms: 5000,
            auto_create_table: true,
        });

        let result = validate_config(&config);
        assert!(result.is_err());
    }

    #[test]
    fn test_load_config_from_str_invalid_toml() {
        let invalid_toml = r#"
            global_level = "INFO
            # 缺少引号结束
        "#;

        let result = load_config_from_str(invalid_toml);
        assert!(result.is_err());

        if let Err(crate::error::QuantumLogError::ConfigError(msg)) = result {
            assert!(msg.contains("TOML解析失败"));
        } else {
            panic!("Expected ConfigError");
        }
    }
}