rustapi-extras 0.1.450

Production-ready middleware collection for RustAPI. Includes JWT auth, CORS, Rate Limiting, SQLx integration, and OpenTelemetry observability.
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
//! Log formatters for different output formats

use serde_json::json;
use std::collections::HashMap;
use std::time::SystemTime;

/// Log entry data structure
#[derive(Clone, Debug)]
pub struct LogEntry {
    /// Timestamp of the log entry
    pub timestamp: SystemTime,
    /// Log level (info, warn, error, debug)
    pub level: String,
    /// Log message
    pub message: String,
    /// HTTP method
    pub method: Option<String>,
    /// Request URI/path
    pub uri: Option<String>,
    /// HTTP status code
    pub status: Option<u16>,
    /// Request duration in milliseconds
    pub duration_ms: Option<u64>,
    /// Correlation ID
    pub correlation_id: Option<String>,
    /// Trace ID (for distributed tracing)
    pub trace_id: Option<String>,
    /// Span ID
    pub span_id: Option<String>,
    /// Service name
    pub service_name: Option<String>,
    /// Service version
    pub service_version: Option<String>,
    /// Environment name
    pub environment: Option<String>,
    /// Request headers (key-value pairs)
    pub request_headers: Option<HashMap<String, String>>,
    /// Response headers (key-value pairs)
    pub response_headers: Option<HashMap<String, String>>,
    /// Request body (truncated)
    pub request_body: Option<String>,
    /// Response body (truncated)
    pub response_body: Option<String>,
    /// Client IP address
    pub client_ip: Option<String>,
    /// User agent string
    pub user_agent: Option<String>,
    /// Additional custom fields
    pub custom_fields: HashMap<String, String>,
    /// Error message if any
    pub error: Option<String>,
}

impl Default for LogEntry {
    fn default() -> Self {
        Self {
            timestamp: SystemTime::now(),
            level: "info".to_string(),
            message: String::new(),
            method: None,
            uri: None,
            status: None,
            duration_ms: None,
            correlation_id: None,
            trace_id: None,
            span_id: None,
            service_name: None,
            service_version: None,
            environment: None,
            request_headers: None,
            response_headers: None,
            request_body: None,
            response_body: None,
            client_ip: None,
            user_agent: None,
            custom_fields: HashMap::new(),
            error: None,
        }
    }
}

impl LogEntry {
    /// Create a new log entry
    pub fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            ..Default::default()
        }
    }

    /// Set the log level
    pub fn level(mut self, level: impl Into<String>) -> Self {
        self.level = level.into();
        self
    }

    /// Set HTTP method
    pub fn method(mut self, method: impl Into<String>) -> Self {
        self.method = Some(method.into());
        self
    }

    /// Set URI
    pub fn uri(mut self, uri: impl Into<String>) -> Self {
        self.uri = Some(uri.into());
        self
    }

    /// Set status code
    pub fn status(mut self, status: u16) -> Self {
        self.status = Some(status);
        self
    }

    /// Set duration in milliseconds
    pub fn duration_ms(mut self, duration: u64) -> Self {
        self.duration_ms = Some(duration);
        self
    }

    /// Set correlation ID
    pub fn correlation_id(mut self, id: impl Into<String>) -> Self {
        self.correlation_id = Some(id.into());
        self
    }

    /// Set trace ID
    pub fn trace_id(mut self, id: impl Into<String>) -> Self {
        self.trace_id = Some(id.into());
        self
    }

    /// Set span ID
    pub fn span_id(mut self, id: impl Into<String>) -> Self {
        self.span_id = Some(id.into());
        self
    }

    /// Set service name
    pub fn service_name(mut self, name: impl Into<String>) -> Self {
        self.service_name = Some(name.into());
        self
    }

    /// Add a custom field
    pub fn field(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.custom_fields.insert(key.into(), value.into());
        self
    }

    /// Set error message
    pub fn error(mut self, error: impl Into<String>) -> Self {
        self.error = Some(error.into());
        self.level = "error".to_string();
        self
    }
}

/// Trait for log formatters
pub trait LogFormatter: Send + Sync {
    /// Format a log entry to a string
    fn format(&self, entry: &LogEntry) -> String;
}

/// JSON log formatter
#[derive(Clone, Debug, Default)]
pub struct JsonFormatter {
    /// Whether to pretty print JSON
    pub pretty: bool,
}

impl JsonFormatter {
    /// Create a new JSON formatter
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a pretty-printing JSON formatter
    pub fn pretty() -> Self {
        Self { pretty: true }
    }
}

impl LogFormatter for JsonFormatter {
    fn format(&self, entry: &LogEntry) -> String {
        let timestamp = entry
            .timestamp
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis();

        let mut obj = json!({
            "timestamp": timestamp,
            "level": entry.level,
            "message": entry.message,
        });

        // Add optional fields
        if let Some(ref method) = entry.method {
            obj["http.method"] = json!(method);
        }
        if let Some(ref uri) = entry.uri {
            obj["http.url"] = json!(uri);
        }
        if let Some(status) = entry.status {
            obj["http.status_code"] = json!(status);
        }
        if let Some(duration) = entry.duration_ms {
            obj["duration_ms"] = json!(duration);
        }
        if let Some(ref correlation_id) = entry.correlation_id {
            obj["correlation_id"] = json!(correlation_id);
        }
        if let Some(ref trace_id) = entry.trace_id {
            obj["trace.id"] = json!(trace_id);
        }
        if let Some(ref span_id) = entry.span_id {
            obj["span.id"] = json!(span_id);
        }
        if let Some(ref service_name) = entry.service_name {
            obj["service.name"] = json!(service_name);
        }
        if let Some(ref service_version) = entry.service_version {
            obj["service.version"] = json!(service_version);
        }
        if let Some(ref env) = entry.environment {
            obj["environment"] = json!(env);
        }
        if let Some(ref client_ip) = entry.client_ip {
            obj["client.ip"] = json!(client_ip);
        }
        if let Some(ref user_agent) = entry.user_agent {
            obj["user_agent"] = json!(user_agent);
        }
        if let Some(ref error) = entry.error {
            obj["error.message"] = json!(error);
        }
        if let Some(ref headers) = entry.request_headers {
            obj["http.request.headers"] = json!(headers);
        }
        if let Some(ref headers) = entry.response_headers {
            obj["http.response.headers"] = json!(headers);
        }

        // Add custom fields
        for (key, value) in &entry.custom_fields {
            obj[key] = json!(value);
        }

        if self.pretty {
            serde_json::to_string_pretty(&obj).unwrap_or_default()
        } else {
            serde_json::to_string(&obj).unwrap_or_default()
        }
    }
}

/// Datadog APM log formatter
#[derive(Clone, Debug, Default)]
pub struct DatadogFormatter;

impl DatadogFormatter {
    /// Create a new Datadog formatter
    pub fn new() -> Self {
        Self
    }
}

impl LogFormatter for DatadogFormatter {
    fn format(&self, entry: &LogEntry) -> String {
        let timestamp = entry
            .timestamp
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis();

        let mut obj = json!({
            "timestamp": timestamp,
            "status": entry.level,
            "message": entry.message,
        });

        // Datadog specific fields
        if let Some(ref trace_id) = entry.trace_id {
            obj["dd.trace_id"] = json!(trace_id);
        }
        if let Some(ref span_id) = entry.span_id {
            obj["dd.span_id"] = json!(span_id);
        }
        if let Some(ref service_name) = entry.service_name {
            obj["service"] = json!(service_name);
        }
        if let Some(ref env) = entry.environment {
            obj["env"] = json!(env);
        }
        if let Some(ref service_version) = entry.service_version {
            obj["version"] = json!(service_version);
        }

        // HTTP fields in Datadog format
        if let Some(ref method) = entry.method {
            obj["http.method"] = json!(method);
        }
        if let Some(ref uri) = entry.uri {
            obj["http.url"] = json!(uri);
        }
        if let Some(status) = entry.status {
            obj["http.status_code"] = json!(status);
        }
        if let Some(duration) = entry.duration_ms {
            obj["duration"] = json!(duration * 1_000_000); // Datadog uses nanoseconds
        }
        if let Some(ref client_ip) = entry.client_ip {
            obj["network.client.ip"] = json!(client_ip);
        }
        if let Some(ref user_agent) = entry.user_agent {
            obj["http.useragent"] = json!(user_agent);
        }

        // Error handling
        if let Some(ref error) = entry.error {
            obj["error.message"] = json!(error);
            obj["error.stack"] = json!(error);
        }

        // Custom fields
        for (key, value) in &entry.custom_fields {
            obj[key] = json!(value);
        }

        serde_json::to_string(&obj).unwrap_or_default()
    }
}

/// Splunk HEC log formatter
#[derive(Clone, Debug, Default)]
pub struct SplunkFormatter {
    /// Splunk source
    pub source: Option<String>,
    /// Splunk sourcetype
    pub sourcetype: Option<String>,
    /// Splunk index
    pub index: Option<String>,
}

impl SplunkFormatter {
    /// Create a new Splunk formatter
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the source
    pub fn source(mut self, source: impl Into<String>) -> Self {
        self.source = Some(source.into());
        self
    }

    /// Set the sourcetype
    pub fn sourcetype(mut self, sourcetype: impl Into<String>) -> Self {
        self.sourcetype = Some(sourcetype.into());
        self
    }

    /// Set the index
    pub fn index(mut self, index: impl Into<String>) -> Self {
        self.index = Some(index.into());
        self
    }
}

impl LogFormatter for SplunkFormatter {
    fn format(&self, entry: &LogEntry) -> String {
        let timestamp = entry
            .timestamp
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs_f64();

        let mut event = json!({
            "level": entry.level,
            "message": entry.message,
        });

        // Add all fields to event
        if let Some(ref method) = entry.method {
            event["http_method"] = json!(method);
        }
        if let Some(ref uri) = entry.uri {
            event["http_url"] = json!(uri);
        }
        if let Some(status) = entry.status {
            event["http_status"] = json!(status);
        }
        if let Some(duration) = entry.duration_ms {
            event["duration_ms"] = json!(duration);
        }
        if let Some(ref correlation_id) = entry.correlation_id {
            event["correlation_id"] = json!(correlation_id);
        }
        if let Some(ref trace_id) = entry.trace_id {
            event["trace_id"] = json!(trace_id);
        }
        if let Some(ref service_name) = entry.service_name {
            event["service"] = json!(service_name);
        }
        if let Some(ref env) = entry.environment {
            event["environment"] = json!(env);
        }
        if let Some(ref error) = entry.error {
            event["error"] = json!(error);
        }

        for (key, value) in &entry.custom_fields {
            event[key] = json!(value);
        }

        // Build Splunk HEC format
        let mut obj = json!({
            "time": timestamp,
            "event": event,
        });

        if let Some(ref source) = self.source {
            obj["source"] = json!(source);
        }
        if let Some(ref sourcetype) = self.sourcetype {
            obj["sourcetype"] = json!(sourcetype);
        }
        if let Some(ref index) = self.index {
            obj["index"] = json!(index);
        }
        if let Some(ref service_name) = entry.service_name {
            obj["host"] = json!(service_name);
        }

        serde_json::to_string(&obj).unwrap_or_default()
    }
}

/// Logfmt log formatter (key=value pairs)
#[derive(Clone, Debug, Default)]
pub struct LogfmtFormatter;

impl LogfmtFormatter {
    /// Create a new Logfmt formatter
    pub fn new() -> Self {
        Self
    }
}

impl LogFormatter for LogfmtFormatter {
    fn format(&self, entry: &LogEntry) -> String {
        let mut parts = Vec::new();

        let timestamp = entry
            .timestamp
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis();

        parts.push(format!("ts={}", timestamp));
        parts.push(format!("level={}", entry.level));
        parts.push(format!("msg=\"{}\"", escape_logfmt(&entry.message)));

        if let Some(ref method) = entry.method {
            parts.push(format!("method={}", method));
        }
        if let Some(ref uri) = entry.uri {
            parts.push(format!("uri=\"{}\"", escape_logfmt(uri)));
        }
        if let Some(status) = entry.status {
            parts.push(format!("status={}", status));
        }
        if let Some(duration) = entry.duration_ms {
            parts.push(format!("duration_ms={}", duration));
        }
        if let Some(ref correlation_id) = entry.correlation_id {
            parts.push(format!("correlation_id={}", correlation_id));
        }
        if let Some(ref trace_id) = entry.trace_id {
            parts.push(format!("trace_id={}", trace_id));
        }
        if let Some(ref span_id) = entry.span_id {
            parts.push(format!("span_id={}", span_id));
        }
        if let Some(ref service_name) = entry.service_name {
            parts.push(format!("service={}", service_name));
        }
        if let Some(ref env) = entry.environment {
            parts.push(format!("env={}", env));
        }
        if let Some(ref error) = entry.error {
            parts.push(format!("error=\"{}\"", escape_logfmt(error)));
        }

        for (key, value) in &entry.custom_fields {
            parts.push(format!("{}=\"{}\"", key, escape_logfmt(value)));
        }

        parts.join(" ")
    }
}

/// Escape special characters for logfmt
fn escape_logfmt(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
}

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

    #[test]
    fn test_json_formatter() {
        let entry = LogEntry::new("test message")
            .method("GET")
            .uri("/api/test")
            .status(200)
            .duration_ms(42)
            .correlation_id("abc-123");

        let formatter = JsonFormatter::new();
        let output = formatter.format(&entry);

        let parsed: Value = serde_json::from_str(&output).unwrap();
        assert_eq!(parsed["message"], "test message");
        assert_eq!(parsed["http.method"], "GET");
        assert_eq!(parsed["http.status_code"], 200);
    }

    #[test]
    fn test_datadog_formatter() {
        let entry = LogEntry::new("test message")
            .method("POST")
            .status(201)
            .trace_id("trace-123")
            .span_id("span-456")
            .service_name("my-service");

        let formatter = DatadogFormatter::new();
        let output = formatter.format(&entry);

        let parsed: Value = serde_json::from_str(&output).unwrap();
        assert_eq!(parsed["dd.trace_id"], "trace-123");
        assert_eq!(parsed["dd.span_id"], "span-456");
        assert_eq!(parsed["service"], "my-service");
    }

    #[test]
    fn test_splunk_formatter() {
        let entry = LogEntry::new("test message")
            .method("GET")
            .status(200)
            .service_name("my-service");

        let formatter = SplunkFormatter::new()
            .source("rustapi")
            .sourcetype("json")
            .index("main");
        let output = formatter.format(&entry);

        let parsed: Value = serde_json::from_str(&output).unwrap();
        assert!(parsed["time"].is_number());
        assert_eq!(parsed["source"], "rustapi");
        assert_eq!(parsed["sourcetype"], "json");
    }

    #[test]
    fn test_logfmt_formatter() {
        let entry = LogEntry::new("test message")
            .method("GET")
            .uri("/api/test")
            .status(200)
            .correlation_id("abc-123");

        let formatter = LogfmtFormatter::new();
        let output = formatter.format(&entry);

        assert!(output.contains("level=info"));
        assert!(output.contains("method=GET"));
        assert!(output.contains("status=200"));
        assert!(output.contains("correlation_id=abc-123"));
    }

    #[test]
    fn test_log_entry_builder() {
        let entry = LogEntry::new("test")
            .level("warn")
            .method("DELETE")
            .uri("/api/item/1")
            .status(204)
            .field("custom", "value");

        assert_eq!(entry.level, "warn");
        assert_eq!(entry.method, Some("DELETE".to_string()));
        assert_eq!(
            entry.custom_fields.get("custom"),
            Some(&"value".to_string())
        );
    }
}

#[cfg(test)]
mod property_tests {
    use super::*;
    use proptest::prelude::*;
    use serde_json::Value;

    /// **Feature: v1-features-roadmap, Property 14: Structured log format**
    /// **Validates: Requirements 7.4**
    ///
    /// For any log output:
    /// - Format SHALL be valid JSON for JSON/Datadog/Splunk formatters
    /// - All required fields SHALL be present (timestamp, level, message)
    /// - Round-trip SHALL preserve all data
    /// - Special characters SHALL be properly escaped
    /// - Logfmt format SHALL follow key=value specification

    /// Strategy for generating log levels
    fn log_level_strategy() -> impl Strategy<Value = String> {
        prop_oneof![
            Just("debug".to_string()),
            Just("info".to_string()),
            Just("warn".to_string()),
            Just("error".to_string()),
        ]
    }

    /// Strategy for generating HTTP methods
    fn http_method_strategy() -> impl Strategy<Value = String> {
        prop_oneof![
            Just("GET".to_string()),
            Just("POST".to_string()),
            Just("PUT".to_string()),
            Just("DELETE".to_string()),
            Just("PATCH".to_string()),
            Just("HEAD".to_string()),
            Just("OPTIONS".to_string()),
        ]
    }

    /// Strategy for generating HTTP status codes
    fn status_code_strategy() -> impl Strategy<Value = u16> {
        prop_oneof![
            200u16..=299, // 2xx success
            300u16..=399, // 3xx redirect
            400u16..=499, // 4xx client error
            500u16..=599, // 5xx server error
        ]
    }

    /// Strategy for generating URIs
    fn uri_strategy() -> impl Strategy<Value = String> {
        prop::string::string_regex("/api/[a-z]{3,10}(/[0-9]{1,5})?").unwrap()
    }

    /// Strategy for generating messages with special characters
    fn message_strategy() -> impl Strategy<Value = String> {
        prop::string::string_regex(r#"[a-zA-Z0-9 \n\r\t"'\\]{5,50}"#).unwrap()
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property 14: JSON formatter produces valid JSON
        #[test]
        fn prop_json_format_valid(
            message in message_strategy(),
            level in log_level_strategy(),
            method in prop::option::of(http_method_strategy()),
            uri in prop::option::of(uri_strategy()),
            status in prop::option::of(status_code_strategy()),
        ) {
            let mut entry = LogEntry::new(message.clone()).level(level.clone());
            if let Some(m) = method {
                entry = entry.method(m);
            }
            if let Some(u) = uri {
                entry = entry.uri(u);
            }
            if let Some(s) = status {
                entry = entry.status(s);
            }

            let formatter = JsonFormatter::new();
            let output = formatter.format(&entry);

            // MUST be valid JSON
            let parsed: Value = serde_json::from_str(&output)
                .expect("JSON formatter must produce valid JSON");

            // Required fields MUST be present
            prop_assert!(parsed.get("timestamp").is_some());
            prop_assert!(parsed.get("level").is_some());
            prop_assert!(parsed.get("message").is_some());

            // Values MUST match
            prop_assert_eq!(parsed["level"].as_str().unwrap(), level);
            prop_assert_eq!(parsed["message"].as_str().unwrap(), message);
        }

        /// Property 14: JSON formatter preserves all fields
        #[test]
        fn prop_json_format_preserves_fields(
            message in "[a-zA-Z0-9 ]{5,30}",
            method in http_method_strategy(),
            uri in uri_strategy(),
            status in status_code_strategy(),
            duration in 1u64..10000,
            correlation_id in "[a-z0-9-]{10,20}",
            trace_id in "[a-f0-9]{32}",
        ) {
            let entry = LogEntry::new(message.clone())
                .method(method.clone())
                .uri(uri.clone())
                .status(status)
                .duration_ms(duration)
                .correlation_id(correlation_id.clone())
                .trace_id(trace_id.clone());

            let formatter = JsonFormatter::new();
            let output = formatter.format(&entry);
            let parsed: Value = serde_json::from_str(&output).unwrap();

            // All fields MUST be preserved
            prop_assert_eq!(parsed["message"].clone(), message);
            prop_assert_eq!(parsed["http.method"].clone(), method);
            prop_assert_eq!(parsed["http.url"].clone(), uri);
            prop_assert_eq!(parsed["http.status_code"].clone(), status);
            prop_assert_eq!(parsed["duration_ms"].clone(), duration);
            prop_assert_eq!(parsed["correlation_id"].clone(), correlation_id);
            prop_assert_eq!(parsed["trace.id"].clone(), trace_id);
        }

        /// Property 14: Datadog formatter produces valid JSON
        #[test]
        fn prop_datadog_format_valid(
            message in message_strategy(),
            trace_id in "[a-f0-9]{32}",
            span_id in "[a-f0-9]{16}",
            service in "[a-z-]{5,15}",
        ) {
            let entry = LogEntry::new(message.clone())
                .trace_id(trace_id.clone())
                .span_id(span_id.clone())
                .service_name(service.clone());

            let formatter = DatadogFormatter::new();
            let output = formatter.format(&entry);

            // MUST be valid JSON
            let parsed: Value = serde_json::from_str(&output)
                .expect("Datadog formatter must produce valid JSON");

            // Datadog-specific fields MUST be present
            prop_assert_eq!(parsed["dd.trace_id"].clone(), trace_id);
            prop_assert_eq!(parsed["dd.span_id"].clone(), span_id);
            prop_assert_eq!(parsed["service"].clone(), service);
            prop_assert_eq!(parsed["status"].clone(), "info");
        }

        /// Property 14: Datadog converts duration to nanoseconds
        #[test]
        fn prop_datadog_duration_nanoseconds(duration_ms in 1u64..10000) {
            let entry = LogEntry::new("test").duration_ms(duration_ms);
            let formatter = DatadogFormatter::new();
            let output = formatter.format(&entry);
            let parsed: Value = serde_json::from_str(&output).unwrap();

            // Datadog duration MUST be in nanoseconds (ms * 1_000_000)
            let expected_ns = duration_ms * 1_000_000;
            prop_assert_eq!(parsed["duration"].clone(), expected_ns);
        }

        /// Property 14: Splunk formatter produces valid JSON
        #[test]
        fn prop_splunk_format_valid(
            message in message_strategy(),
            source in "[a-z]{5,10}",
            sourcetype in "[a-z]{5,10}",
            index in "[a-z]{4,8}",
        ) {
            let entry = LogEntry::new(message.clone());
            let formatter = SplunkFormatter::new()
                .source(source.clone())
                .sourcetype(sourcetype.clone())
                .index(index.clone());
            let output = formatter.format(&entry);

            // MUST be valid JSON
            let parsed: Value = serde_json::from_str(&output)
                .expect("Splunk formatter must produce valid JSON");

            // Splunk HEC structure MUST be correct
            prop_assert!(parsed.get("time").is_some());
            prop_assert!(parsed.get("event").is_some());
            prop_assert_eq!(parsed["source"].clone(), source);
            prop_assert_eq!(parsed["sourcetype"].clone(), sourcetype);
            prop_assert_eq!(parsed["index"].clone(), index);

            // Event MUST contain message
            let event = &parsed["event"];
            prop_assert_eq!(event["message"].clone(), message);
        }

        /// Property 14: Splunk timestamp is Unix epoch
        #[test]
        fn prop_splunk_timestamp_format(_seed in 0u32..100) {
            let entry = LogEntry::new("test");
            let formatter = SplunkFormatter::new();
            let output = formatter.format(&entry);
            let parsed: Value = serde_json::from_str(&output).unwrap();

            // Time MUST be a number (Unix timestamp)
            prop_assert!(parsed["time"].is_f64() || parsed["time"].is_u64());

            let time = parsed["time"].as_f64().unwrap();
            // Reasonable timestamp check (after 2020-01-01, before 2100-01-01)
            prop_assert!(time > 1577836800.0 && time < 4102444800.0);
        }

        /// Property 14: Logfmt format follows specification
        #[test]
        fn prop_logfmt_format_valid(
            message in "[a-zA-Z0-9 ]{5,30}",
            method in http_method_strategy(),
            status in status_code_strategy(),
        ) {
            let entry = LogEntry::new(message.clone())
                .method(method.clone())
                .status(status);

            let formatter = LogfmtFormatter::new();
            let output = formatter.format(&entry);

            // MUST contain required fields
            prop_assert!(output.contains("ts="));
            prop_assert!(output.contains("level="));
            prop_assert!(output.contains("msg="));

            // MUST contain optional fields
            let method_str = format!("method={}", method);
            prop_assert!(output.contains(&method_str));
            let status_str = format!("status={}", status);
            prop_assert!(output.contains(&status_str));
        }

        /// Property 14: Logfmt escapes special characters
        #[test]
        fn prop_logfmt_escapes_special_chars(
            message in prop::string::string_regex(r#"[a-zA-Z0-9 \n\r"\\]{5,30}"#).unwrap()
        ) {
            let entry = LogEntry::new(message.clone());
            let formatter = LogfmtFormatter::new();
            let output = formatter.format(&entry);

            // Quotes MUST be escaped
            if message.contains('"') {
                prop_assert!(output.contains(r#"\""#));
            }

            // Newlines MUST be escaped
            if message.contains('\n') {
                prop_assert!(output.contains(r"\n"));
            }

            // Carriage returns MUST be escaped
            if message.contains('\r') {
                prop_assert!(output.contains(r"\r"));
            }

            // Backslashes MUST be escaped
            if message.contains('\\') {
                prop_assert!(output.contains(r"\\"));
            }
        }

        /// Property 14: Custom fields are preserved in all formats
        #[test]
        fn prop_custom_fields_preserved(
            message in "[a-zA-Z0-9 ]{5,20}",
            key in "[a-z_]{5,10}",
            value in "[a-zA-Z0-9]{5,15}",
        ) {
            let entry = LogEntry::new(message).field(key.clone(), value.clone());

            // JSON formatter
            let json_formatter = JsonFormatter::new();
            let json_output = json_formatter.format(&entry);
            let json_parsed: Value = serde_json::from_str(&json_output).unwrap();
            prop_assert_eq!(json_parsed[&key].clone(), value.clone());

            // Datadog formatter
            let dd_formatter = DatadogFormatter::new();
            let dd_output = dd_formatter.format(&entry);
            let dd_parsed: Value = serde_json::from_str(&dd_output).unwrap();
            prop_assert_eq!(dd_parsed[&key].clone(), value.clone());

            // Splunk formatter
            let splunk_formatter = SplunkFormatter::new();
            let splunk_output = splunk_formatter.format(&entry);
            let splunk_parsed: Value = serde_json::from_str(&splunk_output).unwrap();
            prop_assert_eq!(splunk_parsed["event"][&key].clone(), value.clone());

            // Logfmt formatter
            let logfmt_formatter = LogfmtFormatter::new();
            let logfmt_output = logfmt_formatter.format(&entry);
            let logfmt_expected = format!("{}=\"{}\"", key, value);
            prop_assert!(logfmt_output.contains(&logfmt_expected));
        }

        /// Property 14: Error messages are properly formatted
        #[test]
        fn prop_error_formatting(
            error_msg in message_strategy(),
        ) {
            let entry = LogEntry::new("error occurred").error(error_msg.clone());

            // Level MUST be set to error
            prop_assert_eq!(&entry.level, "error");

            // JSON formatter
            let json_formatter = JsonFormatter::new();
            let json_output = json_formatter.format(&entry);
            let json_parsed: Value = serde_json::from_str(&json_output).unwrap();
            prop_assert_eq!(json_parsed["level"].clone(), "error");
            prop_assert_eq!(json_parsed["error.message"].clone(), error_msg.clone());

            // Datadog formatter
            let dd_formatter = DatadogFormatter::new();
            let dd_output = dd_formatter.format(&entry);
            let dd_parsed: Value = serde_json::from_str(&dd_output).unwrap();
            prop_assert_eq!(dd_parsed["status"].clone(), "error");
            prop_assert_eq!(dd_parsed["error.message"].clone(), error_msg);
        }

        /// Property 14: JSON pretty printing is valid
        #[test]
        fn prop_json_pretty_valid(message in "[a-zA-Z0-9 ]{5,20}") {
            let entry = LogEntry::new(message.clone());
            let formatter = JsonFormatter::pretty();
            let output = formatter.format(&entry);

            // MUST be valid JSON despite formatting
            let parsed: Value = serde_json::from_str(&output).unwrap();
            prop_assert_eq!(parsed["message"].clone(), message);

            // MUST contain newlines (pretty formatted)
            prop_assert!(output.contains('\n'));
        }

        /// Property 14: Timestamps are monotonic and reasonable
        #[test]
        fn prop_timestamps_reasonable(_seed in 0u32..100) {
            let entry1 = LogEntry::new("test1");
            std::thread::sleep(std::time::Duration::from_millis(1));
            let entry2 = LogEntry::new("test2");

            let formatter = JsonFormatter::new();

            let output1 = formatter.format(&entry1);
            let output2 = formatter.format(&entry2);

            let parsed1: Value = serde_json::from_str(&output1).unwrap();
            let parsed2: Value = serde_json::from_str(&output2).unwrap();

            let ts1 = parsed1["timestamp"].as_u64().unwrap();
            let ts2 = parsed2["timestamp"].as_u64().unwrap();

            // Later entry MUST have later or equal timestamp
            prop_assert!(ts2 >= ts1);

            // Timestamps MUST be reasonable (after 2020, before 2100)
            prop_assert!(ts1 > 1577836800000); // 2020-01-01 in milliseconds
            prop_assert!(ts1 < 4102444800000); // 2100-01-01 in milliseconds
        }

        /// Property 14: All formatters handle empty optional fields
        #[test]
        fn prop_empty_fields_handled(message in "[a-zA-Z0-9 ]{5,20}") {
            let entry = LogEntry::new(message.clone());

            // JSON
            let json_output = JsonFormatter::new().format(&entry);
            prop_assert!(serde_json::from_str::<Value>(&json_output).is_ok());

            // Datadog
            let dd_output = DatadogFormatter::new().format(&entry);
            prop_assert!(serde_json::from_str::<Value>(&dd_output).is_ok());

            // Splunk
            let splunk_output = SplunkFormatter::new().format(&entry);
            prop_assert!(serde_json::from_str::<Value>(&splunk_output).is_ok());

            // Logfmt
            let logfmt_output = LogfmtFormatter::new().format(&entry);
            prop_assert!(logfmt_output.contains("msg="));
        }
    }
}