sz-rust-middleware-facade 0.6.3

中间件层 facade(P3)— auth/sanctum/jwt_blacklist 等 14 个 Tower 中间件 + log 模块
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
//! 日志系统 — 对齐 PHP `think-logger`
//!
//! ## 设计
//!
//! - 基于 `sz-orm-logger` 的 `StructuredLogger` 提供日志收集
//! - 同时通过 `tracing` 宏输出(与 SZ-ORM-Tracing 协同,未来接入 OpenTelemetry)
//! - 全局单例 `LogFacade`,通过 [`LogFacade::init()`] 初始化、[`LogFacade::instance()`] 获取
//! - 支持多通道(file/console),对齐 PHP `config/log.php` 的 `channels` 配置
//!
//! ## PHP 对齐
//!
//! ```php
//! // PHP think-logger
//! Log::info('hello');
//! Log::error('error occurred', ['exception' => $e]);
//! Log::channel('file')->info('file log');
//! ```
//!
//! ```rust,ignore
//! // SZ-Rust 等价
//! use sz_rust_core::log::LogFacade;
//! LogFacade::instance().unwrap().info("hello");
//! LogFacade::instance().unwrap().error("error occurred");
//! ```

use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::OnceLock;
use sz_rust_infra_facade::config::{LogChannel, LogSection};

// 重导出 sz-orm-logger 核心类型,方便上层直接使用
pub use sz_rust_orm_facade::logger::{LogEntry, LogLevel, Logger, LoggerFactory, StructuredLogger};

/// 全局日志 facade 单例
static LOG_FACADE: OnceLock<LogFacade> = OnceLock::new();

/// 日志 facade — 持有默认 `StructuredLogger` 和命名通道
///
/// 对齐 PHP `think\facade\Log`,提供全局日志访问点。
pub struct LogFacade {
    /// 默认通道名(对应 PHP `config/log.php` 的 `default`)
    default_channel: String,
    /// 默认 logger 实例
    logger: StructuredLogger,
    /// 命名通道集合(对应 PHP `channels`)
    channels: RwLock<HashMap<String, StructuredLogger>>,
}

impl LogFacade {
    /// 构造 LogFacade 实例(不注册到全局单例)
    pub fn new(section: &LogSection) -> Self {
        let default_channel = section.default.clone();
        let default_log_level = section
            .channels
            .get(&default_channel)
            .map(|c| parse_level(&c.level))
            .unwrap_or(LogLevel::Info);
        let logger = StructuredLogger::with_level(default_log_level);

        let mut channels = HashMap::new();
        for (name, channel_cfg) in &section.channels {
            channels.insert(name.clone(), channel_to_logger(channel_cfg));
        }

        LogFacade {
            default_channel,
            logger,
            channels: RwLock::new(channels),
        }
    }

    /// 初始化全局日志 facade
    ///
    /// 重复调用返回已有实例(不覆盖)。
    pub fn init(section: &LogSection) -> &'static LogFacade {
        LOG_FACADE.get_or_init(|| LogFacade::new(section))
    }

    /// 获取全局日志 facade 实例
    ///
    /// 必须先调用 [`LogFacade::init()`] 初始化,否则返回 `None`。
    pub fn instance() -> Option<&'static LogFacade> {
        LOG_FACADE.get()
    }

    /// 获取默认通道名
    pub fn default_channel(&self) -> &str {
        &self.default_channel
    }

    /// 获取默认 logger 引用
    pub fn logger(&self) -> &StructuredLogger {
        &self.logger
    }

    /// 获取指定通道的 logger 引用
    ///
    /// 对齐 PHP `Log::channel('file')->info(...)`。
    pub fn channel(&self, name: &str) -> Option<ChannelRef<'_>> {
        if self.channels.read().contains_key(name) {
            Some(ChannelRef {
                facade: self,
                name: name.to_string(),
            })
        } else {
            None
        }
    }

    /// 获取所有通道名
    pub fn channel_names(&self) -> Vec<String> {
        self.channels.read().keys().cloned().collect()
    }

    /// 记录日志(同时输出到 StructuredLogger 和 tracing)
    pub fn log(&self, level: LogLevel, msg: &str) {
        self.logger.log(level, msg);
        match level {
            LogLevel::Debug => tracing::debug!("{}", msg),
            LogLevel::Info => tracing::info!("{}", msg),
            LogLevel::Warn => tracing::warn!("{}", msg),
            LogLevel::Error => tracing::error!("{}", msg),
        }
    }

    /// 记录 DEBUG 级别日志
    pub fn debug(&self, msg: &str) {
        self.log(LogLevel::Debug, msg);
    }

    /// 记录 INFO 级别日志
    pub fn info(&self, msg: &str) {
        self.log(LogLevel::Info, msg);
    }

    /// 记录 WARN 级别日志
    pub fn warn(&self, msg: &str) {
        self.log(LogLevel::Warn, msg);
    }

    /// 记录 ERROR 级别日志
    pub fn error(&self, msg: &str) {
        self.log(LogLevel::Error, msg);
    }
}

impl std::fmt::Debug for LogFacade {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LogFacade")
            .field("default_channel", &self.default_channel)
            .field("channels", &self.channels.read().keys().collect::<Vec<_>>())
            .finish()
    }
}

/// 命名通道引用
///
/// 通过 [`LogFacade::channel()`] 获取,提供与默认 logger 相同的日志方法。
pub struct ChannelRef<'a> {
    facade: &'a LogFacade,
    name: String,
}

impl<'a> ChannelRef<'a> {
    /// 通道名
    pub fn name(&self) -> &str {
        &self.name
    }

    /// 记录日志到指定通道
    pub fn log(&self, level: LogLevel, msg: &str) {
        let guard = self.facade.channels.read();
        if let Some(logger) = guard.get(&self.name) {
            logger.log(level, msg);
        }
        match level {
            LogLevel::Debug => tracing::debug!("[{}] {}", self.name, msg),
            LogLevel::Info => tracing::info!("[{}] {}", self.name, msg),
            LogLevel::Warn => tracing::warn!("[{}] {}", self.name, msg),
            LogLevel::Error => tracing::error!("[{}] {}", self.name, msg),
        }
    }

    /// 记录 debug 级别日志
    pub fn debug(&self, msg: &str) {
        self.log(LogLevel::Debug, msg);
    }

    /// 记录 info 级别日志
    pub fn info(&self, msg: &str) {
        self.log(LogLevel::Info, msg);
    }

    /// 记录 warn 级别日志
    pub fn warn(&self, msg: &str) {
        self.log(LogLevel::Warn, msg);
    }

    /// 记录 error 级别日志
    pub fn error(&self, msg: &str) {
        self.log(LogLevel::Error, msg);
    }
}

/// 从字符串解析日志级别
///
/// 支持大小写不敏感:`"DEBUG"` / `"debug"` / `"Debug"` 均解析为 `LogLevel::Debug`。
/// 未知字符串默认为 `LogLevel::Info`。
pub fn parse_level(s: &str) -> LogLevel {
    match s.to_lowercase().as_str() {
        "debug" => LogLevel::Debug,
        "info" => LogLevel::Info,
        "warn" | "warning" => LogLevel::Warn,
        "error" => LogLevel::Error,
        _ => LogLevel::Info,
    }
}

/// 从 `LogChannel` 配置构造 `StructuredLogger`
fn channel_to_logger(channel: &LogChannel) -> StructuredLogger {
    StructuredLogger::with_level(parse_level(&channel.level))
}

// ============================================================================
// 单元测试
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use sz_rust_infra_facade::config::{LogChannel, LogSection};

    /// 构造测试用的 LogSection(含 file + console 两个通道)
    fn make_log_section() -> LogSection {
        let mut channels = HashMap::new();
        channels.insert(
            "file".to_string(),
            LogChannel {
                r#type: "file".to_string(),
                path: "runtime/logs".to_string(),
                level: "info".to_string(),
                max_files: 30,
                format: "%{time} [%{level}] %{message}".to_string(),
            },
        );
        channels.insert(
            "console".to_string(),
            LogChannel {
                r#type: "console".to_string(),
                path: String::new(),
                level: "debug".to_string(),
                max_files: 0,
                format: "%{time} [%{level}] %{message}".to_string(),
            },
        );
        LogSection {
            default: "file".to_string(),
            channels,
        }
    }

    /// 测试 parse_level 各种输入
    #[test]
    fn test_parse_level() {
        assert_eq!(parse_level("debug"), LogLevel::Debug);
        assert_eq!(parse_level("DEBUG"), LogLevel::Debug);
        assert_eq!(parse_level("Debug"), LogLevel::Debug);
        assert_eq!(parse_level("info"), LogLevel::Info);
        assert_eq!(parse_level("INFO"), LogLevel::Info);
        assert_eq!(parse_level("warn"), LogLevel::Warn);
        assert_eq!(parse_level("warning"), LogLevel::Warn);
        assert_eq!(parse_level("WARN"), LogLevel::Warn);
        assert_eq!(parse_level("error"), LogLevel::Error);
        assert_eq!(parse_level("ERROR"), LogLevel::Error);
        // 未知字符串默认 Info
        assert_eq!(parse_level("unknown"), LogLevel::Info);
        assert_eq!(parse_level(""), LogLevel::Info);
    }

    /// 测试 LogFacade 构造和默认通道
    #[test]
    fn test_log_facade_new() {
        let section = make_log_section();
        let facade = LogFacade::new(&section);

        assert_eq!(facade.default_channel(), "file");
        let names = facade.channel_names();
        assert_eq!(names.len(), 2);
        assert!(names.contains(&"file".to_string()));
        assert!(names.contains(&"console".to_string()));
    }

    /// 测试默认 logger 级别取自 default 通道
    #[test]
    fn test_default_logger_level() {
        let section = make_log_section();
        let facade = LogFacade::new(&section);

        // file 通道 level=info,所以默认 logger 级别为 Info
        assert_eq!(facade.logger().level(), LogLevel::Info);

        // Debug 级别应被过滤
        facade.debug("debug msg - should be filtered");
        let entries = facade.logger().entries();
        assert!(entries.iter().all(|e| e.level != LogLevel::Debug));
    }

    /// 测试日志记录到默认 logger
    #[test]
    fn test_log_to_default_logger() {
        let section = make_log_section();
        let facade = LogFacade::new(&section);

        facade.info("test info message");
        facade.warn("test warn message");
        facade.error("test error message");

        let entries = facade.logger().entries();
        assert!(entries.iter().any(|e| e.message == "test info message"));
        assert!(entries.iter().any(|e| e.message == "test warn message"));
        assert!(entries.iter().any(|e| e.message == "test error message"));
    }

    /// 测试通过 ChannelRef 访问命名通道
    #[test]
    fn test_channel_access() {
        let section = make_log_section();
        let facade = LogFacade::new(&section);

        // file 通道存在
        let file_channel = facade.channel("file");
        assert!(file_channel.is_some());
        let file_channel = file_channel.unwrap();
        assert_eq!(file_channel.name(), "file");

        // console 通道存在
        let console_channel = facade.channel("console");
        assert!(console_channel.is_some());

        // 不存在的通道返回 None
        assert!(facade.channel("nonexistent").is_none());
    }

    /// 测试 console 通道(level=debug)能记录所有级别
    #[test]
    fn test_console_channel_debug_level() {
        let section = make_log_section();
        let facade = LogFacade::new(&section);

        let console = facade.channel("console").unwrap();
        console.debug("debug msg");
        console.info("info msg");
        console.warn("warn msg");
        console.error("error msg");

        // console 通道 level=debug,所有级别都应记录
        let guard = facade.channels.read();
        let console_logger = guard.get("console").unwrap();
        let entries = console_logger.entries();
        assert_eq!(entries.len(), 4);
    }

    /// 测试 LogFacade init 全局单例
    #[test]
    fn test_log_facade_init_singleton() {
        let section = make_log_section();
        let facade = LogFacade::init(&section);

        // instance() 应返回同一实例
        let facade2 = LogFacade::instance();
        assert!(facade2.is_some());
        assert!(std::ptr::eq(facade, facade2.unwrap()));

        // 再次 init 应返回同一实例(不覆盖)
        let section2 = make_log_section();
        let facade3 = LogFacade::init(&section2);
        assert!(std::ptr::eq(facade, facade3));
    }

    /// 测试从实际配置文件加载日志配置
    #[test]
    fn test_load_from_config_file() {
        // 查找 config 目录
        let config_dir = std::env::current_dir().ok().and_then(|d| {
            let mut current = d.clone();
            for _ in 0..5 {
                if current.join("config").exists() {
                    return Some(current.join("config"));
                }
                if let Some(parent) = current.parent() {
                    current = parent.to_path_buf();
                } else {
                    break;
                }
            }
            None
        });

        let Some(config_dir) = config_dir else {
            eprintln!("跳过:未找到 config 目录");
            return;
        };

        let log_path = config_dir.join("log.yml");
        if !log_path.exists() {
            eprintln!("跳过:未找到 log.yml");
            return;
        }

        let content = std::fs::read_to_string(&log_path).unwrap();
        let section: LogSection = serde_yaml::from_str(&content).unwrap();

        // 验证默认通道为 file
        assert_eq!(section.default, "file");

        // 验证有 file 和 console 两个通道
        assert!(section.channels.contains_key("file"));
        assert!(section.channels.contains_key("console"));

        // 验证 file 通道配置
        let file_channel = section.channels.get("file").unwrap();
        assert_eq!(file_channel.r#type, "file");
        assert_eq!(file_channel.level, "info");
        assert_eq!(file_channel.max_files, 30);

        // 验证 console 通道配置
        let console_channel = section.channels.get("console").unwrap();
        assert_eq!(console_channel.r#type, "console");
        assert_eq!(console_channel.level, "debug");
    }

    /// 测试 LogFacade::new 处理空 channels(默认通道不存在时用 Info 级别)
    #[test]
    fn test_log_facade_with_empty_channels() {
        let section = LogSection::default();
        let facade = LogFacade::new(&section);

        // 默认通道为空,logger 级别应为 Info(fallback)
        assert_eq!(facade.logger().level(), LogLevel::Info);
        assert_eq!(facade.default_channel(), "");
    }

    /// 测试 LogFacade::Debug 输出
    #[test]
    fn test_log_facade_debug_format() {
        let section = make_log_section();
        let facade = LogFacade::new(&section);

        let debug_str = format!("{:?}", facade);
        assert!(debug_str.contains("LogFacade"));
        assert!(debug_str.contains("file"));
    }
}
// Log 中间件 — 请求/响应日志(对齐 PHP `think-logger`)
//
// sz-rust 自研中间件,PHP 端无全局 Log 中间件(PHP `app/middleware.php` 仅含
// `SessionInit` + `AllowCrossDomain`)。本模块在 [`crate::order::DEFAULT_ORDER`]
// 中位于第 3 位(`Trace` → `Cors` → **`Log`** → `RateLimit` → `Auth`)。
//
// ## 行为
//
// 1. **入口**:生成 `RequestId`(如果 extensions 中没有,则新生成),注入 extensions
// 2. **记录起始时间**:`std::time::Instant::now()`
// 3. **调用 `next.run(req)`**:传递请求给下游
// 4. **出口**:根据响应状态码记录日志
//    - 2xx/3xx → `tracing::info!`
//    - 4xx → `tracing::warn!`(对齐 PHP `apart_level=['error','sql']` 的级别分离思想)
//    - 5xx → `tracing::error!`
//
// ## 日志字段
//
// | 字段 | 来源 | 说明 |
// |------|------|------|
// | `request_id` | `generate_request_id()` | 全局唯一计数器 + 时间戳,16 字符 hex |
// | `method` | `Request::method()` | HTTP 方法 |
// | `uri` | `Request::uri().path()` | 请求路径(不含查询字符串) |
// | `status` | `Response::status().as_u16()` | HTTP 状态码 |
// | `duration_ms` | `Instant::elapsed()` | 请求耗时(毫秒) |
//
// ## PHP 对齐
//
// PHP 端无 Log 中间件,业务代码通过 `Log::info()` 等主动调用。
// sz-rust 的 Log 中间件是自研增强,提供:
// - 请求生命周期自动日志(无需业务代码手动调用)
// - 请求 ID 追踪(贯穿整个请求链路)
// - 响应状态码分级日志(4xx Warn / 5xx Error)
//
// 日志级别对齐 think-logger 的 4 级(debug/info/warn/error),
// `apart_level` 思想对齐 PHP `config/log.php` 的 `['error','sql']` 独立文件配置。
//
// ## 用法
//
// ```ignore
// use sz_rust_core::middleware::log::log_middleware;
// use axum::Router;
//
// let app: Router = Router::new()
//     .route("/", axum::routing::get(|| async { "ok" }))
//     .layer(axum::middleware::from_fn(log_middleware));
// ```

use axum::extract::Request;
use axum::middleware::Next;
use axum::response::Response;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;

/// 请求 ID(注入到 request extensions,供下游 handler 和日志使用)
///
/// 生成方式:全局 `AtomicU64` 计数器 + 当前时间戳,保证进程内唯一。
/// 格式:16 字符 hex(`{timestamp_secs:08x}{counter:08x}`)。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RequestId {
    /// 时间戳部分(UNIX 秒)
    timestamp_secs: u64,
    /// 计数器部分(进程内递增)
    counter: u64,
}

impl RequestId {
    /// 返回 16 字符 hex 字符串
    ///
    /// 格式:`{timestamp_secs:08x}{counter:08x}`(对齐 W3C traceparent 的 16 字符 span_id 长度)。
    pub fn to_hex(&self) -> String {
        format!("{:08x}{:08x}", self.timestamp_secs, self.counter)
    }

    /// 返回时间戳部分
    pub fn timestamp_secs(&self) -> u64 {
        self.timestamp_secs
    }

    /// 返回计数器部分
    pub fn counter(&self) -> u64 {
        self.counter
    }
}

impl std::fmt::Display for RequestId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.to_hex())
    }
}

/// 全局 request_id 计数器(进程内递增)
static REQUEST_ID_COUNTER: AtomicU64 = AtomicU64::new(0);

/// 生成新的 `RequestId`
///
/// 使用全局 `AtomicU64` 计数器 + 当前 UNIX 时间戳,保证进程内唯一。
/// 多线程安全(`fetch_add` 是原子操作)。
pub fn generate_request_id() -> RequestId {
    let counter = REQUEST_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
    let timestamp_secs = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);
    RequestId {
        timestamp_secs,
        counter,
    }
}

/// Log 中间件配置
#[derive(Debug, Clone, Default)]
pub struct LogConfig {
    /// 排除路径(不记录日志,对齐 PHP 端白名单思想)
    ///
    /// 支持精确匹配(如 `/health`)和通配符匹配(如 `/health/*`)。
    pub exclude_paths: Vec<String>,
}

impl LogConfig {
    /// 创建带排除路径的配置
    pub fn with_exclude_paths(mut self, paths: Vec<String>) -> Self {
        self.exclude_paths = paths;
        self
    }

    /// 判断路径是否被排除
    ///
    /// 支持精确匹配和 `*` 通配符匹配(复用 [`crate::auth::is_route_allowed`] 的逻辑)。
    pub fn is_excluded(&self, path: &str) -> bool {
        crate::auth::is_route_allowed(path, &self.exclude_paths)
    }
}

/// 根据响应状态码返回日志级别
///
/// 对齐 PHP `config/log.php` 的 `apart_level=['error','sql']` 思想:
/// - 2xx/3xx → `Info`(成功请求)
/// - 4xx → `Warn`(客户端错误)
/// - 5xx → `Error`(服务端错误)
///
/// 其他状态码(如 1xx)默认为 `Info`。
pub fn log_level_for_status(status: u16) -> LogLevel {
    match status {
        400..=499 => LogLevel::Warn,
        500..=599 => LogLevel::Error,
        _ => LogLevel::Info,
    }
}

/// 格式化请求日志消息
///
/// 输出格式:`request_id=<hex> method=<METHOD> uri=<path> status=<code> duration_ms=<ms>`
///
/// 此函数主要用于测试可验证的纯函数,中间件实际输出通过 `tracing` 宏的结构化字段实现。
pub fn format_request_log(
    method: &str,
    uri: &str,
    status: u16,
    duration_ms: u64,
    request_id: &RequestId,
) -> String {
    format!(
        "request_id={} method={} uri={} status={} duration_ms={}",
        request_id.to_hex(),
        method,
        uri,
        status,
        duration_ms
    )
}

/// Log 中间件 — 请求/响应日志
///
/// ## 校验流程
///
/// 1. **提取请求信息**:method, uri(在 `req` 被消费之前)
/// 2. **生成 RequestId**:如果 extensions 中没有,则新生成
/// 3. **记录起始时间**:`Instant::now()`
/// 4. **注入 RequestId**:插入 request extensions
/// 5. **调用 `next.run(req)`**:传递请求给下游
/// 6. **计算耗时**:`start.elapsed()`
/// 7. **记录日志**:根据状态码选择级别,输出结构化日志
///
/// ## 排除路径
///
/// 如果请求路径在 [`LogConfig::exclude_paths`] 中,则不记录日志(但仍注入 RequestId)。
///
/// ## 用法
///
/// ```ignore
/// use sz_rust_core::middleware::log::{log_middleware, LogConfig};
/// use axum::Router;
///
/// let config = LogConfig::default();
/// let app: Router = Router::new()
///     .route("/", axum::routing::get(|| async { "ok" }))
///     .layer(axum::middleware::from_fn_with_state(config, log_middleware_with_config));
/// ```
pub async fn log_middleware(req: Request, next: Next) -> Response {
    log_middleware_inner(req, next, &LogConfig::default()).await
}

/// 带配置的 Log 中间件
pub async fn log_middleware_with_config(
    axum::extract::State(config): axum::extract::State<LogConfig>,
    req: Request,
    next: Next,
) -> Response {
    log_middleware_inner(req, next, &config).await
}

async fn log_middleware_inner(req: Request, next: Next, config: &LogConfig) -> Response {
    // 1. 提取请求信息(在 req 被消费之前)
    let method = req.method().clone();
    let uri = req.uri().path().to_string();

    // 2. 生成 RequestId(如果 extensions 中没有,则新生成)
    let request_id = req
        .extensions()
        .get::<RequestId>()
        .copied()
        .unwrap_or_else(generate_request_id);

    // 3. 记录起始时间
    let start = Instant::now();

    // 4. 注入 RequestId 到 extensions
    let mut req = req;
    req.extensions_mut().insert(request_id);

    // 5. 调用 next
    let response = next.run(req).await;

    // 6. 计算耗时
    let duration_ms = start.elapsed().as_millis() as u64;

    // 7. 记录日志(排除路径不记录)
    if !config.is_excluded(&uri) {
        let status = response.status().as_u16();
        let level = log_level_for_status(status);
        let request_id_hex = request_id.to_hex();

        match level {
            LogLevel::Debug => tracing::debug!(
                request_id = %request_id_hex,
                method = %method,
                uri = %uri,
                status = status,
                duration_ms = duration_ms,
                "request completed"
            ),
            LogLevel::Info => tracing::info!(
                request_id = %request_id_hex,
                method = %method,
                uri = %uri,
                status = status,
                duration_ms = duration_ms,
                "request completed"
            ),
            LogLevel::Warn => tracing::warn!(
                request_id = %request_id_hex,
                method = %method,
                uri = %uri,
                status = status,
                duration_ms = duration_ms,
                "request completed"
            ),
            LogLevel::Error => tracing::error!(
                request_id = %request_id_hex,
                method = %method,
                uri = %uri,
                status = status,
                duration_ms = duration_ms,
                "request completed"
            ),
        }
    }

    response
}

#[cfg(test)]
mod middleware_tests {
    use super::*;
    use axum::body::Body;
    use axum::http::StatusCode;
    use axum::Router;
    use http_body_util::BodyExt;
    use tower::ServiceExt;

    // ====================================================================
    // 辅助函数
    // ====================================================================

    async fn read_body(resp: Response) -> String {
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        String::from_utf8(bytes.to_vec()).unwrap()
    }

    fn make_request(method: &str, uri: &str) -> Request {
        Request::builder()
            .method(method)
            .uri(uri)
            .body(Body::empty())
            .unwrap()
    }

    /// 构建测试用 Router
    fn build_app() -> Router {
        Router::new()
            .route(
                "/ok",
                axum::routing::get(|| async { axum::http::StatusCode::OK }),
            )
            .route(
                "/notfound",
                axum::routing::get(|| async { axum::http::StatusCode::NOT_FOUND }),
            )
            .route(
                "/error",
                axum::routing::get(|| async { axum::http::StatusCode::INTERNAL_SERVER_ERROR }),
            )
            .route("/body", axum::routing::get(|| async { "hello" }))
            .layer(axum::middleware::from_fn(log_middleware))
    }

    // ====================================================================
    // RequestId 单元测试
    // ====================================================================

    #[test]
    fn test_request_id_to_hex_is_16_chars() {
        let id = RequestId {
            timestamp_secs: 0x12345678,
            counter: 0x9ABCDEF0,
        };
        let hex = id.to_hex();
        assert_eq!(hex.len(), 16);
        assert_eq!(hex, "123456789abcdef0");
    }

    #[test]
    fn test_request_id_to_hex_zero() {
        let id = RequestId {
            timestamp_secs: 0,
            counter: 0,
        };
        assert_eq!(id.to_hex(), "0000000000000000");
    }

    #[test]
    fn test_request_id_to_hex_max() {
        let id = RequestId {
            timestamp_secs: u64::MAX,
            counter: u64::MAX,
        };
        // u64::MAX = 0xFFFFFFFFFFFFFFFF,但 format!("{:08x}", u64::MAX) 会输出 16 字符
        let hex = id.to_hex();
        assert_eq!(hex.len(), 32); // 每部分 16 字符,总共 32 字符
    }

    #[test]
    fn test_request_id_display_matches_to_hex() {
        let id = RequestId {
            timestamp_secs: 0x12345678,
            counter: 0x9ABCDEF0,
        };
        assert_eq!(format!("{}", id), id.to_hex());
    }

    #[test]
    fn test_request_id_accessors() {
        let id = RequestId {
            timestamp_secs: 100,
            counter: 200,
        };
        assert_eq!(id.timestamp_secs(), 100);
        assert_eq!(id.counter(), 200);
    }

    #[test]
    fn test_request_id_equality() {
        let id1 = RequestId {
            timestamp_secs: 1,
            counter: 2,
        };
        let id2 = RequestId {
            timestamp_secs: 1,
            counter: 2,
        };
        let id3 = RequestId {
            timestamp_secs: 1,
            counter: 3,
        };
        assert_eq!(id1, id2);
        assert_ne!(id1, id3);
    }

    // ====================================================================
    // generate_request_id 单元测试
    // ====================================================================

    #[test]
    fn test_generate_request_id_returns_unique() {
        let id1 = generate_request_id();
        let id2 = generate_request_id();
        // 计数器递增,保证唯一
        assert_ne!(id1.counter(), id2.counter());
        assert_eq!(id2.counter(), id1.counter() + 1);
    }

    #[test]
    fn test_generate_request_id_hex_is_16_chars() {
        let id = generate_request_id();
        let hex = id.to_hex();
        // 注意:如果 timestamp_secs 或 counter 超过 u32::MAX,hex 会超过 16 字符
        // 但在正常情况下(timestamp < 2106 年,counter < 40 亿次),hex 是 16 字符
        assert!(hex.len() >= 16);
    }

    // ====================================================================
    // log_level_for_status 单元测试
    // ====================================================================

    #[test]
    fn test_log_level_for_2xx_returns_info() {
        assert_eq!(log_level_for_status(200), LogLevel::Info);
        assert_eq!(log_level_for_status(201), LogLevel::Info);
        assert_eq!(log_level_for_status(204), LogLevel::Info);
    }

    #[test]
    fn test_log_level_for_3xx_returns_info() {
        assert_eq!(log_level_for_status(301), LogLevel::Info);
        assert_eq!(log_level_for_status(302), LogLevel::Info);
        assert_eq!(log_level_for_status(304), LogLevel::Info);
    }

    #[test]
    fn test_log_level_for_4xx_returns_warn() {
        assert_eq!(log_level_for_status(400), LogLevel::Warn);
        assert_eq!(log_level_for_status(401), LogLevel::Warn);
        assert_eq!(log_level_for_status(403), LogLevel::Warn);
        assert_eq!(log_level_for_status(404), LogLevel::Warn);
        assert_eq!(log_level_for_status(422), LogLevel::Warn);
        assert_eq!(log_level_for_status(499), LogLevel::Warn);
    }

    #[test]
    fn test_log_level_for_5xx_returns_error() {
        assert_eq!(log_level_for_status(500), LogLevel::Error);
        assert_eq!(log_level_for_status(501), LogLevel::Error);
        assert_eq!(log_level_for_status(502), LogLevel::Error);
        assert_eq!(log_level_for_status(503), LogLevel::Error);
        assert_eq!(log_level_for_status(599), LogLevel::Error);
    }

    #[test]
    fn test_log_level_for_1xx_returns_info() {
        // 1xx 信息响应默认为 Info
        assert_eq!(log_level_for_status(100), LogLevel::Info);
        assert_eq!(log_level_for_status(101), LogLevel::Info);
    }

    #[test]
    fn test_log_level_for_boundary() {
        // 边界测试:399 → Info,400 → Warn,499 → Warn,500 → Error,599 → Error,600 → Info
        assert_eq!(log_level_for_status(399), LogLevel::Info);
        assert_eq!(log_level_for_status(400), LogLevel::Warn);
        assert_eq!(log_level_for_status(499), LogLevel::Warn);
        assert_eq!(log_level_for_status(500), LogLevel::Error);
        assert_eq!(log_level_for_status(599), LogLevel::Error);
        assert_eq!(log_level_for_status(600), LogLevel::Info);
    }

    // ====================================================================
    // format_request_log 单元测试
    // ====================================================================

    #[test]
    fn test_format_request_log_basic() {
        let request_id = RequestId {
            timestamp_secs: 0x12345678,
            counter: 0x9ABCDEF0,
        };
        let msg = format_request_log("GET", "/api/users", 200, 15, &request_id);
        assert_eq!(
            msg,
            "request_id=123456789abcdef0 method=GET uri=/api/users status=200 duration_ms=15"
        );
    }

    #[test]
    fn test_format_request_log_post_method() {
        let request_id = RequestId {
            timestamp_secs: 0,
            counter: 1,
        };
        let msg = format_request_log("POST", "/api/orders", 201, 42, &request_id);
        assert_eq!(
            msg,
            "request_id=0000000000000001 method=POST uri=/api/orders status=201 duration_ms=42"
        );
    }

    #[test]
    fn test_format_request_log_error_status() {
        let request_id = RequestId {
            timestamp_secs: 0,
            counter: 0,
        };
        let msg = format_request_log("GET", "/missing", 404, 5, &request_id);
        assert_eq!(
            msg,
            "request_id=0000000000000000 method=GET uri=/missing status=404 duration_ms=5"
        );
    }

    #[test]
    fn test_format_request_log_with_query_string_in_uri() {
        // uri 应该是原始 path(含查询字符串),由调用方决定是否截取
        let request_id = RequestId {
            timestamp_secs: 0,
            counter: 0,
        };
        let msg = format_request_log("GET", "/api?foo=bar", 200, 1, &request_id);
        assert!(msg.contains("uri=/api?foo=bar"));
    }

    // ====================================================================
    // LogConfig 单元测试
    // ====================================================================

    #[test]
    fn test_log_config_default_empty_exclude_paths() {
        let config = LogConfig::default();
        assert!(config.exclude_paths.is_empty());
    }

    #[test]
    fn test_log_config_with_exclude_paths() {
        let config = LogConfig::default().with_exclude_paths(vec!["/health".to_string()]);
        assert_eq!(config.exclude_paths, vec!["/health".to_string()]);
    }

    #[test]
    fn test_log_config_is_excluded_exact_match() {
        let config = LogConfig::default().with_exclude_paths(vec!["/health".to_string()]);
        assert!(config.is_excluded("/health"));
        assert!(!config.is_excluded("/health/detail"));
        assert!(!config.is_excluded("/api"));
    }

    #[test]
    fn test_log_config_is_excluded_wildcard_match() {
        let config = LogConfig::default().with_exclude_paths(vec!["/health/*".to_string()]);
        assert!(config.is_excluded("/health/check"));
        assert!(config.is_excluded("/health/deep/nested"));
        assert!(!config.is_excluded("/health"));
        assert!(!config.is_excluded("/api"));
    }

    #[test]
    fn test_log_config_is_excluded_empty_list() {
        let config = LogConfig::default();
        assert!(!config.is_excluded("/any"));
    }

    #[test]
    fn test_log_config_is_excluded_multiple_entries() {
        let config = LogConfig::default()
            .with_exclude_paths(vec!["/health".to_string(), "/metrics/*".to_string()]);
        assert!(config.is_excluded("/health"));
        assert!(config.is_excluded("/metrics/prometheus"));
        assert!(!config.is_excluded("/api"));
    }

    // ====================================================================
    // log_middleware 集成测试
    // ====================================================================

    #[tokio::test]
    async fn test_log_middleware_returns_response_unchanged() {
        // 验证中间件不修改响应体
        let app = build_app();
        let resp = app.oneshot(make_request("GET", "/body")).await.unwrap();
        let body = read_body(resp).await;
        assert_eq!(body, "hello");
    }

    #[tokio::test]
    async fn test_log_middleware_returns_correct_status() {
        let app = build_app();
        let resp = app.oneshot(make_request("GET", "/ok")).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn test_log_middleware_injects_request_id() {
        // 验证 request_id 被注入 extensions
        let app = Router::new()
            .route(
                "/",
                axum::routing::get(|req: Request| async move {
                    let request_id = req.extensions().get::<RequestId>().unwrap();
                    format!("request_id:{}", request_id.to_hex())
                }),
            )
            .layer(axum::middleware::from_fn(log_middleware));

        let resp = app.oneshot(make_request("GET", "/")).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let body = read_body(resp).await;
        assert!(body.starts_with("request_id:"));
        // 验证 hex 长度至少 16 字符
        let hex = body.strip_prefix("request_id:").unwrap();
        assert!(hex.len() >= 16);
    }

    #[tokio::test]
    async fn test_log_middleware_generates_unique_request_ids() {
        // 验证多个请求生成不同的 request_id
        let app = Router::new()
            .route(
                "/",
                axum::routing::get(|req: Request| async move {
                    let request_id = req.extensions().get::<RequestId>().unwrap();
                    request_id.to_hex()
                }),
            )
            .layer(axum::middleware::from_fn(log_middleware));

        let resp1 = app.clone().oneshot(make_request("GET", "/")).await.unwrap();
        let hex1 = read_body(resp1).await;

        let resp2 = app.oneshot(make_request("GET", "/")).await.unwrap();
        let hex2 = read_body(resp2).await;

        assert_ne!(hex1, hex2);
    }

    #[tokio::test]
    async fn test_log_middleware_preserves_existing_request_id() {
        // 验证已存在的 request_id 不被覆盖
        let existing_id = RequestId {
            timestamp_secs: 0xDEADBEEF,
            counter: 0x12345678,
        };
        let app = Router::new()
            .route(
                "/",
                axum::routing::get(|req: Request| async move {
                    let request_id = req.extensions().get::<RequestId>().unwrap();
                    request_id.to_hex()
                }),
            )
            .layer(axum::middleware::from_fn(log_middleware))
            .layer(
                tower::ServiceBuilder::new().layer(tower::layer::layer_fn(move |service| {
                    tower::util::MapRequest::new(service, move |mut req: Request| {
                        req.extensions_mut().insert(existing_id);
                        req
                    })
                })),
            );

        let resp = app.oneshot(make_request("GET", "/")).await.unwrap();
        let body = read_body(resp).await;
        assert_eq!(body, "deadbeef12345678");
    }

    #[tokio::test]
    async fn test_log_middleware_records_2xx_status() {
        // 验证 2xx 响应正常处理(日志级别由 log_level_for_status 决定)
        let app = build_app();
        let resp = app.oneshot(make_request("GET", "/ok")).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn test_log_middleware_records_4xx_status() {
        let app = build_app();
        let resp = app.oneshot(make_request("GET", "/notfound")).await.unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn test_log_middleware_records_5xx_status() {
        let app = build_app();
        let resp = app.oneshot(make_request("GET", "/error")).await.unwrap();
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[tokio::test]
    async fn test_log_middleware_with_config_excludes_path() {
        // 验证排除路径不记录日志(但仍注入 request_id)
        let config = LogConfig::default().with_exclude_paths(vec!["/health".to_string()]);
        let app = Router::new()
            .route("/health", axum::routing::get(|| async { "healthy" }))
            .layer(axum::middleware::from_fn_with_state(
                config,
                log_middleware_with_config,
            ));

        let resp = app.oneshot(make_request("GET", "/health")).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let body = read_body(resp).await;
        assert_eq!(body, "healthy");
    }

    #[tokio::test]
    async fn test_log_middleware_with_config_wildcard_exclude() {
        // 验证通配符排除路径
        let config = LogConfig::default().with_exclude_paths(vec!["/metrics/*".to_string()]);
        let app = Router::new()
            .route(
                "/metrics/prometheus",
                axum::routing::get(|| async { "metrics" }),
            )
            .layer(axum::middleware::from_fn_with_state(
                config,
                log_middleware_with_config,
            ));

        let resp = app
            .oneshot(make_request("GET", "/metrics/prometheus"))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn test_log_middleware_preserves_method_and_uri() {
        // 验证 method 和 uri 被正确提取(通过日志消息格式验证)
        // 由于 tracing 宏输出在测试中难以捕获,这里验证中间件不破坏请求
        let app = build_app();
        let resp = app.oneshot(make_request("GET", "/ok")).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn test_log_middleware_duration_is_non_negative() {
        // 验证 duration_ms 是非负的(通过响应正常返回间接验证)
        let app = build_app();
        let start = std::time::Instant::now();
        let resp = app.oneshot(make_request("GET", "/ok")).await.unwrap();
        let elapsed = start.elapsed();
        assert!(resp.status().is_success());
        // 中间件内部记录的 duration_ms 应该 <= 测试外部的 elapsed
        assert!(elapsed.as_millis() < 5000); // 5 秒上限(防止死循环)
    }

    #[tokio::test]
    async fn test_log_middleware_handles_post_request() {
        let app = Router::new()
            .route(
                "/submit",
                axum::routing::post(|| async { axum::http::StatusCode::CREATED }),
            )
            .layer(axum::middleware::from_fn(log_middleware));

        let req = Request::builder()
            .method("POST")
            .uri("/submit")
            .body(Body::empty())
            .unwrap();
        let resp = app.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::CREATED);
    }

    #[tokio::test]
    async fn test_log_middleware_chains_with_other_middleware() {
        // 验证 Log 中间件与其他中间件链式调用
        async fn add_header_middleware(req: Request, next: Next) -> Response {
            let mut resp = next.run(req).await;
            resp.headers_mut()
                .insert("X-Custom", "value".parse().unwrap());
            resp
        }

        let app = Router::new()
            .route("/", axum::routing::get(|| async { "ok" }))
            .layer(axum::middleware::from_fn(add_header_middleware))
            .layer(axum::middleware::from_fn(log_middleware));

        let resp = app.oneshot(make_request("GET", "/")).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(resp.headers().get("X-Custom").unwrap(), "value");
    }

    // ====================================================================
    // PHP 行为对齐验证
    // ====================================================================

    #[test]
    fn test_php_apart_level_alignment() {
        // 对齐 PHP `config/log.php` 的 `apart_level=['error','sql']` 思想:
        // 4xx → Warn(客户端错误,类似 PHP warning)
        // 5xx → Error(服务端错误,对齐 PHP error 独立文件)
        assert_eq!(log_level_for_status(200), LogLevel::Info);
        assert_eq!(log_level_for_status(404), LogLevel::Warn);
        assert_eq!(log_level_for_status(500), LogLevel::Error);
    }

    #[test]
    fn test_php_think_logger_level_alignment() {
        // 对齐 PHP think-logger 的 4 级日志(debug/info/warn/error)
        // sz-rust 的 LogLevel 也是 4 级,一一对应
        let levels = [
            LogLevel::Debug,
            LogLevel::Info,
            LogLevel::Warn,
            LogLevel::Error,
        ];
        assert_eq!(levels.len(), 4);
    }

    #[test]
    fn test_request_id_format_aligns_with_w3c_span_id_length() {
        // 对齐 W3C traceparent 的 span_id 长度(16 字符 hex)
        // 便于未来 Trace 中间件实现时与 trace_id 格式兼容
        let id = RequestId {
            timestamp_secs: 0x12345678,
            counter: 0x9ABCDEF0,
        };
        assert_eq!(id.to_hex().len(), 16);
    }
}