nautilus-common 0.56.0

Common functionality and machinery for the Nautilus trading engine
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
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::{
    fmt::Display,
    sync::{Mutex, OnceLock, atomic::Ordering, mpsc::SendError},
};

use ahash::AHashMap;
use indexmap::IndexMap;
use log::{
    Level, LevelFilter, Log, STATIC_MAX_LEVEL,
    kv::{ToValue, Value},
    set_boxed_logger, set_max_level,
};
use nautilus_core::{
    UUID4, UnixNanos,
    datetime::unix_nanos_to_iso8601,
    time::{get_atomic_clock_realtime, get_atomic_clock_static},
};
use nautilus_model::identifiers::TraderId;
use serde::{Deserialize, Serialize, Serializer};
use ustr::Ustr;

pub use super::config::LoggerConfig;
use super::{LOGGING_BYPASSED, LOGGING_GUARDS_ACTIVE, LOGGING_INITIALIZED, LOGGING_REALTIME};
#[cfg(not(all(feature = "simulation", madsim)))]
use crate::logging::writer::{FileWriter, LogWriter, StderrWriter, StdoutWriter};
use crate::{
    enums::{LogColor, LogLevel},
    logging::writer::FileWriterConfig,
};

#[cfg(not(all(feature = "simulation", madsim)))]
const LOGGING: &str = "logging";
const KV_COLOR: &str = "color";
const KV_COMPONENT: &str = "component";

/// Global log sender which allows multiple log guards per process.
static LOGGER_TX: OnceLock<std::sync::mpsc::Sender<LogEvent>> = OnceLock::new();

/// Global handle to the logging thread - only one thread exists per process.
static LOGGER_HANDLE: Mutex<Option<std::thread::JoinHandle<()>>> = Mutex::new(None);

/// A high-performance logger utilizing a MPSC channel under the hood.
///
/// A logger is initialized with a [`LoggerConfig`] to set up different logging levels for
/// stdout, file, and components. The logger spawns a thread that listens for [`LogEvent`]s
/// sent via an MPSC channel.
#[derive(Debug)]
pub struct Logger {
    /// Configuration for logging levels and behavior.
    pub config: LoggerConfig,
    /// Transmitter for sending log events to the 'logging' thread.
    tx: std::sync::mpsc::Sender<LogEvent>,
}

/// Represents a type of log event.
#[derive(Debug)]
pub enum LogEvent {
    /// A log line event.
    Log(LogLine),
    /// A command to flush all logger buffers.
    Flush,
    /// A command to close the logger.
    Close,
}

/// Represents a log event which includes a message.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LogLine {
    /// The timestamp for the event.
    pub timestamp: UnixNanos,
    /// The log level for the event.
    pub level: Level,
    /// The color for the log message content.
    pub color: LogColor,
    /// The Nautilus system component the log event originated from.
    pub component: Ustr,
    /// The log message content.
    pub message: String,
}

impl Display for LogLine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[{}] {}: {}", self.level, self.component, self.message)
    }
}

/// A wrapper around a log line that provides formatted and cached representations.
///
/// This struct contains a log line and provides various formatted versions
/// of it, such as plain string, colored string, and JSON. It also caches the
/// results for repeated calls, optimizing performance when the same message
/// needs to be logged multiple times in different formats.
#[derive(Clone, Debug)]
pub struct LogLineWrapper {
    /// The underlying log line that contains the log data.
    line: LogLine,
    /// Cached plain string representation of the log line.
    cache: Option<String>,
    /// Cached colored string representation of the log line.
    colored: Option<String>,
    /// The ID of the trader associated with this log event.
    trader_id: Ustr,
}

impl LogLineWrapper {
    /// Creates a new [`LogLineWrapper`] instance.
    #[must_use]
    pub const fn new(line: LogLine, trader_id: Ustr) -> Self {
        Self {
            line,
            cache: None,
            colored: None,
            trader_id,
        }
    }

    /// Returns the plain log message string, caching the result.
    ///
    /// This method constructs the log line format and caches it for repeated calls. Useful when the
    /// same log message needs to be printed multiple times.
    pub fn get_string(&mut self) -> &str {
        self.cache.get_or_insert_with(|| {
            format!(
                "{} [{}] {}.{}: {}\n",
                unix_nanos_to_iso8601(self.line.timestamp),
                self.line.level,
                self.trader_id,
                &self.line.component,
                &self.line.message,
            )
        })
    }

    /// Returns the colored log message string, caching the result.
    ///
    /// This method constructs the colored log line format and caches the result
    /// for repeated calls, providing the message with ANSI color codes if the
    /// logger is configured to use colors.
    pub fn get_colored(&mut self) -> &str {
        self.colored.get_or_insert_with(|| {
            format!(
                "\x1b[1m{}\x1b[0m {}[{}] {}.{}: {}\x1b[0m\n",
                unix_nanos_to_iso8601(self.line.timestamp),
                &self.line.color.as_ansi(),
                self.line.level,
                self.trader_id,
                &self.line.component,
                &self.line.message,
            )
        })
    }

    /// Returns the log message as a JSON string.
    ///
    /// This method serializes the log line and its associated metadata
    /// (timestamp, trader ID, etc.) into a JSON string format. This is useful
    /// for structured logging or when logs need to be stored in a JSON format.
    /// # Panics
    ///
    /// Panics if serialization of the log event to JSON fails.
    #[must_use]
    pub fn get_json(&self) -> String {
        let json_string =
            serde_json::to_string(&self).expect("Error serializing log event to string");
        format!("{json_string}\n")
    }
}

impl Serialize for LogLineWrapper {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut json_obj = IndexMap::new();
        let timestamp = unix_nanos_to_iso8601(self.line.timestamp);
        json_obj.insert("timestamp".to_string(), timestamp);
        json_obj.insert("trader_id".to_string(), self.trader_id.to_string());
        json_obj.insert("level".to_string(), self.line.level.to_string());
        json_obj.insert("color".to_string(), self.line.color.to_string());
        json_obj.insert("component".to_string(), self.line.component.to_string());
        json_obj.insert("message".to_string(), self.line.message.clone());

        json_obj.serialize(serializer)
    }
}

impl Log for Logger {
    fn enabled(&self, metadata: &log::Metadata) -> bool {
        !LOGGING_BYPASSED.load(Ordering::Relaxed)
            && (metadata.level() == Level::Error
                || metadata.level() <= self.config.stdout_level
                || metadata.level() <= self.config.fileout_level)
    }

    fn log(&self, record: &log::Record) {
        if self.enabled(record.metadata()) {
            let timestamp = if LOGGING_REALTIME.load(Ordering::Relaxed) {
                get_atomic_clock_realtime().get_time_ns()
            } else {
                get_atomic_clock_static().get_time_ns()
            };
            let level = record.level();
            let key_values = record.key_values();
            let color: LogColor = key_values
                .get(KV_COLOR.into())
                .and_then(|v| v.to_u64().map(|v| (v as u8).into()))
                .unwrap_or(level.into());
            let component = key_values.get(KV_COMPONENT.into()).map_or_else(
                || Ustr::from(record.metadata().target()),
                |v| Ustr::from(&v.to_string()),
            );

            let line = LogLine {
                timestamp,
                level,
                color,
                component,
                message: format!("{}", record.args()),
            };

            if let Err(SendError(LogEvent::Log(line))) = self.tx.send(LogEvent::Log(line)) {
                eprintln!("Error sending log event (receiver closed): {line}");
            }
        }
    }

    fn flush(&self) {
        // Don't attempt to flush if we're already bypassed/shutdown
        if LOGGING_BYPASSED.load(Ordering::Relaxed) {
            return;
        }

        if let Err(e) = self.tx.send(LogEvent::Flush) {
            eprintln!("Error sending flush log event: {e}");
        }
    }
}

impl Logger {
    /// Initializes the logger based on the `NAUTILUS_LOG` environment variable.
    ///
    /// # Errors
    ///
    /// Returns an error if reading the environment variable or parsing the configuration fails.
    pub fn init_with_env(
        trader_id: TraderId,
        instance_id: UUID4,
        file_config: FileWriterConfig,
    ) -> anyhow::Result<LogGuard> {
        let config = LoggerConfig::from_env()?;
        Self::init_with_config(trader_id, instance_id, config, file_config)
    }

    /// Initializes the logger with the given configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the logger fails to register or initialize the background thread.
    pub fn init_with_config(
        trader_id: TraderId,
        instance_id: UUID4,
        config: LoggerConfig,
        file_config: FileWriterConfig,
    ) -> anyhow::Result<LogGuard> {
        // Fast path: already initialized
        if super::LOGGING_INITIALIZED.load(Ordering::SeqCst) {
            return LogGuard::new()
                .ok_or_else(|| anyhow::anyhow!("Logging already initialized but sender missing"));
        }

        let (tx, rx) = std::sync::mpsc::channel::<LogEvent>();

        let logger_tx = tx.clone();
        let logger = Self {
            tx: logger_tx,
            config: config.clone(),
        };

        set_boxed_logger(Box::new(logger))?;

        // Store the sender globally so additional guards can be created
        if LOGGER_TX.set(tx).is_err() {
            debug_assert!(
                false,
                "LOGGER_TX already set - re-initialization not supported"
            );
        }

        if config.bypass_logging {
            super::logging_set_bypass();
        }

        let is_colored = config.is_colored;

        let print_config = config.print_config;
        if print_config {
            println!("STATIC_MAX_LEVEL={STATIC_MAX_LEVEL}");
            println!("Logger initialized with {config:?} {file_config:?}");
        }

        #[cfg(not(all(feature = "simulation", madsim)))]
        {
            let handle = std::thread::Builder::new()
                .name(LOGGING.to_string())
                .spawn(move || {
                    Self::handle_messages(
                        trader_id.to_string(),
                        instance_id.to_string(),
                        config,
                        file_config,
                        rx,
                    );
                })?;

            // Store the handle globally
            if let Ok(mut handle_guard) = LOGGER_HANDLE.lock() {
                debug_assert!(
                    handle_guard.is_none(),
                    "LOGGER_HANDLE already set - re-initialization not supported"
                );
                *handle_guard = Some(handle);
            }
        }

        #[cfg(all(feature = "simulation", madsim))]
        {
            // Under simulation, the background writer thread would escape the
            // madsim scheduler. Drop the receiver so the channel closes cleanly
            // and force the bypass flag so subsequent log calls no-op without
            // SendError noise.
            let _ = (trader_id, instance_id, config, file_config, rx);
            super::logging_set_bypass();
        }

        let max_level = log::LevelFilter::Trace;
        set_max_level(max_level);

        if print_config {
            println!("Logger set as `log` implementation with max level {max_level}");
        }

        super::LOGGING_INITIALIZED.store(true, Ordering::SeqCst);
        super::LOGGING_COLORED.store(is_colored, Ordering::SeqCst);

        LogGuard::new()
            .ok_or_else(|| anyhow::anyhow!("Failed to create LogGuard from global sender"))
    }

    #[cfg(not(all(feature = "simulation", madsim)))]
    #[expect(clippy::needless_pass_by_value)]
    fn handle_messages(
        trader_id: String,
        instance_id: String,
        config: LoggerConfig,
        file_config: FileWriterConfig,
        rx: std::sync::mpsc::Receiver<LogEvent>,
    ) {
        let LoggerConfig {
            stdout_level,
            fileout_level,
            component_level,
            module_level,
            log_components_only,
            is_colored,
            print_config: _,
            use_tracing: _,
            bypass_logging: _,
            file_config: _,
            clear_log_file: _,
        } = config;

        // Pre-sort module filters by descending path length for O(n) longest-prefix lookup
        let mut module_filters_sorted: Vec<(Ustr, LevelFilter)> =
            module_level.into_iter().collect();
        module_filters_sorted.sort_by_key(|b| std::cmp::Reverse(b.0.len()));

        let trader_id_cache = Ustr::from(&trader_id);

        // Set up std I/O buffers
        let mut stdout_writer = StdoutWriter::new(stdout_level, is_colored);
        let mut stderr_writer = StderrWriter::new(is_colored);

        // Conditionally create file writer based on fileout_level
        let mut file_writer_opt = if fileout_level == LevelFilter::Off {
            None
        } else {
            FileWriter::new(trader_id, instance_id, file_config, fileout_level)
        };

        let process_event = |event: LogEvent,
                             stdout_writer: &mut StdoutWriter,
                             stderr_writer: &mut StderrWriter,
                             file_writer_opt: &mut Option<FileWriter>| {
            match event {
                LogEvent::Log(line) => {
                    if should_filter_log(
                        &line.component,
                        line.level,
                        &module_filters_sorted,
                        &component_level,
                        log_components_only,
                    ) {
                        return;
                    }

                    let mut wrapper = LogLineWrapper::new(line, trader_id_cache);

                    if stderr_writer.enabled(&wrapper.line) {
                        if is_colored {
                            stderr_writer.write(wrapper.get_colored());
                        } else {
                            stderr_writer.write(wrapper.get_string());
                        }
                    }

                    if stdout_writer.enabled(&wrapper.line) {
                        if is_colored {
                            stdout_writer.write(wrapper.get_colored());
                        } else {
                            stdout_writer.write(wrapper.get_string());
                        }
                    }

                    if let Some(file_writer) = file_writer_opt
                        && file_writer.enabled(&wrapper.line)
                    {
                        if file_writer.json_format {
                            file_writer.write(&wrapper.get_json());
                        } else {
                            file_writer.write(wrapper.get_string());
                        }
                    }
                }
                LogEvent::Flush => {
                    stdout_writer.flush();
                    stderr_writer.flush();

                    if let Some(file_writer) = file_writer_opt {
                        file_writer.flush();
                    }
                }
                LogEvent::Close => {
                    // Close handled in the main loop; ignore here.
                }
            }
        };

        // Continue to receive and handle log events until channel is hung up
        while let Ok(event) = rx.recv() {
            match event {
                LogEvent::Log(_) | LogEvent::Flush => process_event(
                    event,
                    &mut stdout_writer,
                    &mut stderr_writer,
                    &mut file_writer_opt,
                ),
                LogEvent::Close => {
                    // First flush what's been written so far
                    stdout_writer.flush();
                    stderr_writer.flush();

                    if let Some(ref mut file_writer) = file_writer_opt {
                        file_writer.flush();
                    }

                    // Drain any remaining events that may have raced with shutdown
                    // This ensures logs enqueued just before/around shutdown aren't lost.
                    while let Ok(evt) = rx.try_recv() {
                        match evt {
                            LogEvent::Close => (), // ignore extra Close events
                            _ => process_event(
                                evt,
                                &mut stdout_writer,
                                &mut stderr_writer,
                                &mut file_writer_opt,
                            ),
                        }
                    }

                    // Final flush after draining
                    stdout_writer.flush();
                    stderr_writer.flush();

                    if let Some(ref mut file_writer) = file_writer_opt {
                        file_writer.flush();
                    }

                    break;
                }
            }
        }
    }
}

/// Determines if a log line should be filtered out based on module and component filters.
///
/// Returns `true` if the line should be skipped (filtered out), `false` if it should be logged.
///
/// The `module_filters_sorted` slice must be pre-sorted by descending path length so the
/// first `starts_with` match is the longest prefix.
#[must_use]
pub fn should_filter_log(
    component: &Ustr,
    line_level: log::Level,
    module_filters_sorted: &[(Ustr, LevelFilter)],
    component_level: &AHashMap<Ustr, LevelFilter>,
    log_components_only: bool,
) -> bool {
    if module_filters_sorted.is_empty() && component_level.is_empty() {
        return log_components_only;
    }

    // Module filter: first match in sorted list is longest prefix
    let module_filter = module_filters_sorted
        .iter()
        .find(|(path, _)| component.starts_with(path.as_str()))
        .map(|(_, level)| *level);

    let component_filter = component_level.get(component).copied();

    if log_components_only && module_filter.is_none() && component_filter.is_none() {
        return true;
    }

    // Module filter takes precedence over component filter
    if let Some(filter_level) = module_filter.or(component_filter)
        && line_level > filter_level
    {
        return true;
    }

    false
}

/// Gracefully shuts down the logging subsystem.
///
/// Performs the same shutdown sequence as dropping the last `LogGuard`, but can be called
/// explicitly for deterministic shutdown timing (e.g., testing or Windows Python applications).
///
/// # Safety
///
/// Safe to call multiple times. Thread join is skipped if called from the logging thread.
pub(crate) fn shutdown_graceful() {
    // Prevent further logging
    LOGGING_BYPASSED.store(true, Ordering::SeqCst);
    log::set_max_level(log::LevelFilter::Off);

    // Signal Close if the sender exists
    if let Some(tx) = LOGGER_TX.get() {
        let _ = tx.send(LogEvent::Close);
    }

    if let Ok(mut handle_guard) = LOGGER_HANDLE.lock()
        && let Some(handle) = handle_guard.take()
        && handle.thread().id() != std::thread::current().id()
    {
        let _ = handle.join();
    }

    LOGGING_INITIALIZED.store(false, Ordering::SeqCst);
}

pub fn log<T: AsRef<str>>(level: LogLevel, color: LogColor, component: Ustr, message: T) {
    let color = Value::from(color as u8);

    match level {
        LogLevel::Off => {}
        LogLevel::Trace => {
            log::trace!(component = component.to_value(), color = color; "{}", message.as_ref());
        }
        LogLevel::Debug => {
            log::debug!(component = component.to_value(), color = color; "{}", message.as_ref());
        }
        LogLevel::Info => {
            log::info!(component = component.to_value(), color = color; "{}", message.as_ref());
        }
        LogLevel::Warning => {
            log::warn!(component = component.to_value(), color = color; "{}", message.as_ref());
        }
        LogLevel::Error => {
            log::error!(component = component.to_value(), color = color; "{}", message.as_ref());
        }
    }
}

/// A guard that manages the lifecycle of the logging subsystem.
///
/// `LogGuard` ensures the logging thread remains active while instances exist and properly
/// terminates when all guards are dropped. The system uses reference counting to track active
/// guards - when the last `LogGuard` is dropped, the logging thread is joined to ensure all
/// pending log messages are written before the process terminates.
///
/// # Reference Counting
///
/// The logging system maintains a global atomic counter of active `LogGuard` instances. This
/// ensures that:
/// - The logging thread remains active as long as at least one `LogGuard` exists.
/// - All log messages are properly flushed when intermediate guards are dropped.
/// - The logging thread is cleanly terminated and joined when the last guard is dropped.
///
/// # Shutdown Behavior
///
/// When the last guard is dropped, the logging thread is signaled to close, drains pending
/// messages, and is joined to ensure all logs are written before process termination.
///
/// **Python on Windows:** Non-deterministic GC order during interpreter shutdown can
/// occasionally prevent proper thread join, resulting in truncated logs.
///
/// # Limits
///
/// The system supports a maximum of 255 concurrent `LogGuard` instances.
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.common")
)]
#[cfg_attr(
    feature = "python",
    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.common")
)]
#[derive(Debug)]
pub struct LogGuard {
    tx: std::sync::mpsc::Sender<LogEvent>,
}

impl LogGuard {
    /// Creates a new [`LogGuard`] instance from the global logger.
    ///
    /// Returns `None` if logging has not been initialized.
    ///
    /// # Panics
    ///
    /// Panics if the number of active LogGuards would exceed 255.
    #[must_use]
    pub fn new() -> Option<Self> {
        LOGGER_TX.get().map(|tx| {
            LOGGING_GUARDS_ACTIVE
                .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |count| {
                    if count == u8::MAX {
                        None // Reject the update if we're at the limit
                    } else {
                        Some(count + 1)
                    }
                })
                .expect("Maximum number of active LogGuards (255) exceeded");

            Self { tx: tx.clone() }
        })
    }
}

impl Drop for LogGuard {
    /// Handles cleanup when a `LogGuard` is dropped.
    ///
    /// Sends `Flush` if other guards remain active, otherwise sends `Close`, joins the
    /// logging thread, and resets the subsystem state.
    fn drop(&mut self) {
        let previous_count = LOGGING_GUARDS_ACTIVE
            .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |count| {
                assert!(count != 0, "LogGuard reference count underflow");
                Some(count - 1)
            })
            .expect("Failed to decrement LogGuard count");

        // Check if this was the last LogGuard - re-check after decrement to avoid race
        if previous_count == 1 && LOGGING_GUARDS_ACTIVE.load(Ordering::SeqCst) == 0 {
            // This is truly the last LogGuard, so we should close the logger and join the thread
            // to ensure all log messages are written before the process terminates.
            // Prevent any new log events from being accepted while shutting down.
            LOGGING_BYPASSED.store(true, Ordering::SeqCst);

            // Disable all log levels to reduce overhead on late calls
            log::set_max_level(log::LevelFilter::Off);

            // Ensure Close is delivered before joining (critical for shutdown)
            let _ = self.tx.send(LogEvent::Close);

            // Join the logging thread to ensure all pending logs are written
            if let Ok(mut handle_guard) = LOGGER_HANDLE.lock()
                && let Some(handle) = handle_guard.take()
            {
                // Avoid self-join deadlock
                if handle.thread().id() != std::thread::current().id() {
                    let _ = handle.join();
                }
            }

            // Reset LOGGING_INITIALIZED since the logging thread has terminated
            LOGGING_INITIALIZED.store(false, Ordering::SeqCst);
        } else {
            // Other LogGuards are still active, just flush our logs
            let _ = self.tx.send(LogEvent::Flush);
        }
    }
}

#[cfg(test)]
mod tests {
    use ahash::AHashMap;
    use log::LevelFilter;
    use nautilus_core::UUID4;
    use nautilus_model::identifiers::TraderId;
    use rstest::*;
    use serde_json::Value;
    use tempfile::tempdir;
    use ustr::Ustr;

    use super::*;
    use crate::enums::LogColor;

    #[rstest]
    fn log_message_serialization() {
        let log_message = LogLine {
            timestamp: UnixNanos::default(),
            level: log::Level::Info,
            color: LogColor::Normal,
            component: Ustr::from("Portfolio"),
            message: "This is a log message".to_string(),
        };

        let serialized_json = serde_json::to_string(&log_message).unwrap();
        let deserialized_value: Value = serde_json::from_str(&serialized_json).unwrap();

        assert_eq!(deserialized_value["level"], "INFO");
        assert_eq!(deserialized_value["component"], "Portfolio");
        assert_eq!(deserialized_value["message"], "This is a log message");
    }

    #[rstest]
    fn log_config_parsing() {
        let config =
            LoggerConfig::from_spec("stdout=Info;is_colored;fileout=Debug;RiskEngine=Error")
                .unwrap();
        assert_eq!(
            config,
            LoggerConfig {
                stdout_level: LevelFilter::Info,
                fileout_level: LevelFilter::Debug,
                component_level: AHashMap::from_iter(vec![(
                    Ustr::from("RiskEngine"),
                    LevelFilter::Error
                )]),
                module_level: AHashMap::new(),
                log_components_only: false,
                is_colored: true,
                print_config: false,
                use_tracing: false,
                ..Default::default()
            }
        );
    }

    #[rstest]
    fn log_config_parsing2() {
        let config = LoggerConfig::from_spec("stdout=Warn;print_config;fileout=Error;").unwrap();
        assert_eq!(
            config,
            LoggerConfig {
                stdout_level: LevelFilter::Warn,
                fileout_level: LevelFilter::Error,
                component_level: AHashMap::new(),
                module_level: AHashMap::new(),
                log_components_only: false,
                is_colored: true,
                print_config: true,
                use_tracing: false,
                ..Default::default()
            }
        );
    }

    #[rstest]
    fn log_config_parsing_with_log_components_only() {
        let config =
            LoggerConfig::from_spec("stdout=Info;log_components_only;RiskEngine=Debug").unwrap();
        assert_eq!(
            config,
            LoggerConfig {
                stdout_level: LevelFilter::Info,
                fileout_level: LevelFilter::Off,
                component_level: AHashMap::from_iter(vec![(
                    Ustr::from("RiskEngine"),
                    LevelFilter::Debug
                )]),
                module_level: AHashMap::new(),
                log_components_only: true,
                is_colored: true,
                print_config: false,
                use_tracing: false,
                ..Default::default()
            }
        );
    }

    #[rstest]
    fn test_log_line_wrapper_plain_string() {
        let line = LogLine {
            timestamp: 1_650_000_000_000_000_000.into(),
            level: log::Level::Info,
            color: LogColor::Normal,
            component: Ustr::from("TestComponent"),
            message: "Test message".to_string(),
        };

        let mut wrapper = LogLineWrapper::new(line, Ustr::from("TRADER-001"));
        let result = wrapper.get_string();

        assert!(result.contains("TRADER-001"));
        assert!(result.contains("TestComponent"));
        assert!(result.contains("Test message"));
        assert!(result.contains("[INFO]"));
        assert!(result.ends_with('\n'));
        // Should NOT contain ANSI codes
        assert!(!result.contains("\x1b["));
    }

    #[rstest]
    fn test_log_line_wrapper_colored_string() {
        let line = LogLine {
            timestamp: 1_650_000_000_000_000_000.into(),
            level: log::Level::Info,
            color: LogColor::Green,
            component: Ustr::from("TestComponent"),
            message: "Test message".to_string(),
        };

        let mut wrapper = LogLineWrapper::new(line, Ustr::from("TRADER-001"));
        let result = wrapper.get_colored();

        assert!(result.contains("TRADER-001"));
        assert!(result.contains("TestComponent"));
        assert!(result.contains("Test message"));
        // Should contain ANSI codes
        assert!(result.contains("\x1b["));
        assert!(result.ends_with('\n'));
    }

    #[rstest]
    fn test_log_line_wrapper_json_output() {
        let line = LogLine {
            timestamp: 1_650_000_000_000_000_000.into(),
            level: log::Level::Warn,
            color: LogColor::Yellow,
            component: Ustr::from("RiskEngine"),
            message: "Warning message".to_string(),
        };

        let wrapper = LogLineWrapper::new(line, Ustr::from("TRADER-002"));
        let json = wrapper.get_json();

        let parsed: Value = serde_json::from_str(json.trim()).unwrap();
        assert_eq!(parsed["trader_id"], "TRADER-002");
        assert_eq!(parsed["component"], "RiskEngine");
        assert_eq!(parsed["message"], "Warning message");
        assert_eq!(parsed["level"], "WARN");
        assert_eq!(parsed["color"], "YELLOW");
    }

    #[rstest]
    fn test_log_line_wrapper_caches_string() {
        let line = LogLine {
            timestamp: 1_650_000_000_000_000_000.into(),
            level: log::Level::Info,
            color: LogColor::Normal,
            component: Ustr::from("Test"),
            message: "Cached".to_string(),
        };

        let mut wrapper = LogLineWrapper::new(line, Ustr::from("TRADER"));
        let first = wrapper.get_string().to_string();
        let second = wrapper.get_string().to_string();

        assert_eq!(first, second);
    }

    #[rstest]
    fn test_log_line_display() {
        let line = LogLine {
            timestamp: 0.into(),
            level: log::Level::Error,
            color: LogColor::Red,
            component: Ustr::from("Component"),
            message: "Error occurred".to_string(),
        };

        let display = format!("{line}");
        assert_eq!(display, "[ERROR] Component: Error occurred");
    }

    /// Helper to convert module level map to sorted vec (descending by path length)
    fn sorted_module_filters(map: AHashMap<Ustr, LevelFilter>) -> Vec<(Ustr, LevelFilter)> {
        let mut v: Vec<_> = map.into_iter().collect();
        v.sort_by_key(|b| std::cmp::Reverse(b.0.len()));
        v
    }

    #[rstest]
    fn test_filter_no_filters_passes_all() {
        let module_filters = vec![];
        let component_level = AHashMap::new();

        assert!(!should_filter_log(
            &Ustr::from("anything"),
            Level::Trace,
            &module_filters,
            &component_level,
            false
        ));
    }

    #[rstest]
    fn test_filter_component_exact_match() {
        let module_filters = vec![];
        let component_level = AHashMap::from_iter([(Ustr::from("RiskEngine"), LevelFilter::Error)]);

        assert!(should_filter_log(
            &Ustr::from("RiskEngine"),
            Level::Info,
            &module_filters,
            &component_level,
            false
        ));
        assert!(!should_filter_log(
            &Ustr::from("RiskEngine"),
            Level::Error,
            &module_filters,
            &component_level,
            false
        ));
        assert!(!should_filter_log(
            &Ustr::from("Portfolio"),
            Level::Info,
            &module_filters,
            &component_level,
            false
        ));
    }

    #[rstest]
    fn test_filter_module_prefix_match() {
        let module_filters = vec![(Ustr::from("nautilus_okx::websocket"), LevelFilter::Debug)];
        let component_level = AHashMap::new();

        assert!(!should_filter_log(
            &Ustr::from("nautilus_okx::websocket"),
            Level::Debug,
            &module_filters,
            &component_level,
            false
        ));
        assert!(!should_filter_log(
            &Ustr::from("nautilus_okx::websocket::handler"),
            Level::Debug,
            &module_filters,
            &component_level,
            false
        ));
        assert!(should_filter_log(
            &Ustr::from("nautilus_okx::websocket::handler"),
            Level::Trace,
            &module_filters,
            &component_level,
            false
        ));
        assert!(!should_filter_log(
            &Ustr::from("nautilus_binance::data"),
            Level::Trace,
            &module_filters,
            &component_level,
            false
        ));
    }

    #[rstest]
    fn test_filter_longest_prefix_wins() {
        let module_filters = sorted_module_filters(AHashMap::from_iter([
            (Ustr::from("nautilus_okx"), LevelFilter::Error),
            (Ustr::from("nautilus_okx::websocket"), LevelFilter::Debug),
        ]));
        let component_level = AHashMap::new();

        assert!(!should_filter_log(
            &Ustr::from("nautilus_okx::websocket::handler"),
            Level::Debug,
            &module_filters,
            &component_level,
            false
        ));
        assert!(should_filter_log(
            &Ustr::from("nautilus_okx::data"),
            Level::Debug,
            &module_filters,
            &component_level,
            false
        ));
        assert!(!should_filter_log(
            &Ustr::from("nautilus_okx::data"),
            Level::Error,
            &module_filters,
            &component_level,
            false
        ));
    }

    #[rstest]
    fn test_filter_module_precedence_over_component() {
        let module_filters = vec![(Ustr::from("nautilus_okx::websocket"), LevelFilter::Debug)];
        let component_level =
            AHashMap::from_iter([(Ustr::from("nautilus_okx::websocket"), LevelFilter::Error)]);

        assert!(!should_filter_log(
            &Ustr::from("nautilus_okx::websocket"),
            Level::Debug,
            &module_filters,
            &component_level,
            false
        ));
    }

    #[rstest]
    fn test_filter_log_components_only_blocks_unknown() {
        let module_filters = vec![];
        let component_level = AHashMap::from_iter([(Ustr::from("RiskEngine"), LevelFilter::Debug)]);

        assert!(should_filter_log(
            &Ustr::from("Portfolio"),
            Level::Info,
            &module_filters,
            &component_level,
            true
        ));
        assert!(!should_filter_log(
            &Ustr::from("RiskEngine"),
            Level::Info,
            &module_filters,
            &component_level,
            true
        ));
    }

    #[rstest]
    fn test_filter_log_components_only_with_module() {
        let module_filters = vec![(Ustr::from("nautilus_okx"), LevelFilter::Debug)];
        let component_level = AHashMap::new();

        assert!(!should_filter_log(
            &Ustr::from("nautilus_okx::websocket"),
            Level::Debug,
            &module_filters,
            &component_level,
            true
        ));
        assert!(should_filter_log(
            &Ustr::from("nautilus_binance::data"),
            Level::Debug,
            &module_filters,
            &component_level,
            true
        ));
    }

    #[rstest]
    fn test_filter_level_comparison() {
        let module_filters = vec![];
        let component_level = AHashMap::from_iter([(Ustr::from("Test"), LevelFilter::Warn)]);

        assert!(!should_filter_log(
            &Ustr::from("Test"),
            Level::Error,
            &module_filters,
            &component_level,
            false
        ));
        assert!(!should_filter_log(
            &Ustr::from("Test"),
            Level::Warn,
            &module_filters,
            &component_level,
            false
        ));
        assert!(should_filter_log(
            &Ustr::from("Test"),
            Level::Info,
            &module_filters,
            &component_level,
            false
        ));
        assert!(should_filter_log(
            &Ustr::from("Test"),
            Level::Debug,
            &module_filters,
            &component_level,
            false
        ));
        assert!(should_filter_log(
            &Ustr::from("Test"),
            Level::Trace,
            &module_filters,
            &component_level,
            false
        ));
    }

    // These tests use global logging state (one logger per process).
    // They run correctly with cargo-nextest which isolates each test in its own process.
    //
    // Gated out under `cfg(madsim)`: every test here drives the file-logging writer
    // thread, which is itself gated out under simulation (see `Logger::init_with_config`),
    // so log events are dropped and these tests would either hang on `wait_until` or
    // assert against an empty log file. Logging is outside the determinism contract.
    #[cfg(not(all(feature = "simulation", madsim)))]
    mod serial_tests {
        use std::{sync::atomic::Ordering, time::Duration};

        use super::*;
        use crate::{
            logging::{
                LOGGING_BYPASSED, logging_clock_set_static_mode, logging_clock_set_static_time,
                logging_is_initialized, logging_set_bypass,
            },
            testing::wait_until,
        };

        #[rstest]
        fn test_logging_to_file() {
            let config = LoggerConfig {
                fileout_level: LevelFilter::Debug,
                ..Default::default()
            };

            let temp_dir = tempdir().expect("Failed to create temporary directory");
            let file_config = FileWriterConfig {
                directory: Some(temp_dir.path().to_str().unwrap().to_string()),
                ..Default::default()
            };

            let log_guard = Logger::init_with_config(
                TraderId::from("TRADER-001"),
                UUID4::new(),
                config,
                file_config,
            );

            logging_clock_set_static_mode();
            logging_clock_set_static_time(1_650_000_000_000_000);

            log::info!(
                component = "RiskEngine";
                "This is a test"
            );

            let mut log_contents = String::new();

            wait_until(
                || {
                    std::fs::read_dir(&temp_dir)
                        .expect("Failed to read directory")
                        .filter_map(Result::ok)
                        .any(|entry| entry.path().is_file())
                },
                Duration::from_secs(3),
            );

            drop(log_guard); // Ensure log buffers are flushed

            wait_until(
                || {
                    let log_file_path = std::fs::read_dir(&temp_dir)
                        .expect("Failed to read directory")
                        .filter_map(Result::ok)
                        .find(|entry| entry.path().is_file())
                        .expect("No files found in directory")
                        .path();
                    log_contents = std::fs::read_to_string(log_file_path)
                        .expect("Error while reading log file");
                    !log_contents.is_empty()
                },
                Duration::from_secs(3),
            );

            assert_eq!(
                log_contents,
                "1970-01-20T02:20:00.000000000Z [INFO] TRADER-001.RiskEngine: This is a test\n"
            );
        }

        #[rstest]
        fn test_shutdown_drains_backlog_tail() {
            const N: usize = 1000;

            // Configure file logging at Info level
            let config = LoggerConfig {
                stdout_level: LevelFilter::Off,
                fileout_level: LevelFilter::Info,
                ..Default::default()
            };

            let temp_dir = tempdir().expect("Failed to create temporary directory");
            let file_config = FileWriterConfig {
                directory: Some(temp_dir.path().to_str().unwrap().to_string()),
                ..Default::default()
            };

            let log_guard = Logger::init_with_config(
                TraderId::from("TRADER-TAIL"),
                UUID4::new(),
                config,
                file_config,
            )
            .expect("Failed to initialize logger");

            // Use static time for reproducibility
            logging_clock_set_static_mode();
            logging_clock_set_static_time(1_700_000_000_000_000);

            // Enqueue a known number of messages synchronously
            for i in 0..N {
                log::info!(component = "TailDrain"; "BacklogTest {i}");
            }

            // Drop guard to trigger shutdown (bypass + close + drain)
            drop(log_guard);

            // Wait until the file exists and contains at least N lines with our marker
            let mut count = 0usize;
            wait_until(
                || {
                    if let Some(log_file) = std::fs::read_dir(&temp_dir)
                        .expect("Failed to read directory")
                        .filter_map(Result::ok)
                        .find(|entry| entry.path().is_file())
                    {
                        let log_file_path = log_file.path();
                        if let Ok(contents) = std::fs::read_to_string(log_file_path) {
                            count = contents
                                .lines()
                                .filter(|l| l.contains("BacklogTest "))
                                .count();
                            count >= N
                        } else {
                            false
                        }
                    } else {
                        false
                    }
                },
                Duration::from_secs(5),
            );

            assert_eq!(count, N, "Expected all pre-shutdown messages to be written");
        }

        #[rstest]
        fn test_log_component_level_filtering() {
            let config =
                LoggerConfig::from_spec("stdout=Info;fileout=Debug;RiskEngine=Error").unwrap();

            let temp_dir = tempdir().expect("Failed to create temporary directory");
            let file_config = FileWriterConfig {
                directory: Some(temp_dir.path().to_str().unwrap().to_string()),
                ..Default::default()
            };

            let log_guard = Logger::init_with_config(
                TraderId::from("TRADER-001"),
                UUID4::new(),
                config,
                file_config,
            );

            logging_clock_set_static_mode();
            logging_clock_set_static_time(1_650_000_000_000_000);

            log::info!(
                component = "RiskEngine";
                "This is a test"
            );

            drop(log_guard); // Ensure log buffers are flushed

            wait_until(
                || {
                    if let Some(log_file) = std::fs::read_dir(&temp_dir)
                        .expect("Failed to read directory")
                        .filter_map(Result::ok)
                        .find(|entry| entry.path().is_file())
                    {
                        let log_file_path = log_file.path();
                        let log_contents = std::fs::read_to_string(log_file_path)
                            .expect("Error while reading log file");
                        !log_contents.contains("RiskEngine")
                    } else {
                        false
                    }
                },
                Duration::from_secs(3),
            );

            assert!(
                std::fs::read_dir(&temp_dir)
                    .expect("Failed to read directory")
                    .filter_map(Result::ok)
                    .any(|entry| entry.path().is_file()),
                "Log file exists"
            );
        }

        #[rstest]
        fn test_logging_to_file_in_json_format() {
            let config =
                LoggerConfig::from_spec("stdout=Info;is_colored;fileout=Debug;RiskEngine=Info")
                    .unwrap();

            let temp_dir = tempdir().expect("Failed to create temporary directory");
            let file_config = FileWriterConfig {
                directory: Some(temp_dir.path().to_str().unwrap().to_string()),
                file_format: Some("json".to_string()),
                ..Default::default()
            };

            let log_guard = Logger::init_with_config(
                TraderId::from("TRADER-001"),
                UUID4::new(),
                config,
                file_config,
            );

            logging_clock_set_static_mode();
            logging_clock_set_static_time(1_650_000_000_000_000);

            log::info!(
                component = "RiskEngine";
                "This is a test"
            );

            let mut log_contents = String::new();

            drop(log_guard); // Ensure log buffers are flushed

            wait_until(
                || {
                    if let Some(log_file) = std::fs::read_dir(&temp_dir)
                        .expect("Failed to read directory")
                        .filter_map(Result::ok)
                        .find(|entry| entry.path().is_file())
                    {
                        let log_file_path = log_file.path();
                        log_contents = std::fs::read_to_string(log_file_path)
                            .expect("Error while reading log file");
                        !log_contents.is_empty()
                    } else {
                        false
                    }
                },
                Duration::from_secs(3),
            );

            assert_eq!(
                log_contents,
                "{\"timestamp\":\"1970-01-20T02:20:00.000000000Z\",\"trader_id\":\"TRADER-001\",\"level\":\"INFO\",\"color\":\"NORMAL\",\"component\":\"RiskEngine\",\"message\":\"This is a test\"}\n"
            );
        }

        #[rstest]
        fn test_init_sets_logging_is_initialized_flag() {
            let config = LoggerConfig::default();
            let file_config = FileWriterConfig::default();

            let guard = Logger::init_with_config(
                TraderId::from("TRADER-001"),
                UUID4::new(),
                config,
                file_config,
            );
            assert!(guard.is_ok());
            assert!(logging_is_initialized());

            drop(guard);
            assert!(!logging_is_initialized());
        }

        #[rstest]
        fn test_reinit_after_guard_drop_fails() {
            let config = LoggerConfig::default();
            let file_config = FileWriterConfig::default();

            let guard1 = Logger::init_with_config(
                TraderId::from("TRADER-001"),
                UUID4::new(),
                config.clone(),
                file_config.clone(),
            );
            assert!(guard1.is_ok());
            drop(guard1);

            // Re-init fails because log crate's set_boxed_logger only works once per process
            let guard2 = Logger::init_with_config(
                TraderId::from("TRADER-002"),
                UUID4::new(),
                config,
                file_config,
            );
            assert!(guard2.is_err());
        }

        #[rstest]
        fn test_bypass_before_init_prevents_logging() {
            logging_set_bypass();
            assert!(LOGGING_BYPASSED.load(Ordering::Relaxed));

            let temp_dir = tempdir().expect("Failed to create temporary directory");
            let config = LoggerConfig {
                fileout_level: LevelFilter::Debug,
                ..Default::default()
            };
            let file_config = FileWriterConfig {
                directory: Some(temp_dir.path().to_str().unwrap().to_string()),
                ..Default::default()
            };

            let guard = Logger::init_with_config(
                TraderId::from("TRADER-001"),
                UUID4::new(),
                config,
                file_config,
            );
            assert!(guard.is_ok());

            log::info!(
                component = "TestComponent";
                "This should be bypassed"
            );
            std::thread::sleep(Duration::from_millis(100));
            drop(guard);

            // Bypass flag remains permanently set (no reset mechanism)
            assert!(LOGGING_BYPASSED.load(Ordering::Relaxed));
        }

        #[rstest]
        fn test_module_level_filtering() {
            // Configure module-level filters (note: requires :: to be a module filter):
            // - nautilus::adapters=Warn (general adapter logs at Warn+)
            // - nautilus::adapters::okx=Debug (OKX adapter logs at Debug+)
            let config = LoggerConfig::from_spec(
                "stdout=Off;fileout=Trace;nautilus::adapters=Warn;nautilus::adapters::okx=Debug",
            )
            .unwrap();

            let temp_dir = tempdir().expect("Failed to create temporary directory");
            let file_config = FileWriterConfig {
                directory: Some(temp_dir.path().to_str().unwrap().to_string()),
                ..Default::default()
            };

            let log_guard = Logger::init_with_config(
                TraderId::from("TRADER-MOD"),
                UUID4::new(),
                config,
                file_config,
            )
            .expect("Failed to initialize logger");

            logging_clock_set_static_mode();
            logging_clock_set_static_time(1_650_000_000_000_000);

            // Log from nautilus::adapters::okx::websocket - should pass (Debug allowed)
            log::debug!(
                component = "nautilus::adapters::okx::websocket";
                "OKX debug message"
            );

            // Log from nautilus::adapters::okx - should pass (Debug allowed)
            log::info!(
                component = "nautilus::adapters::okx";
                "OKX info message"
            );

            // Log from nautilus::adapters::binance - should be filtered (only Warn+ allowed)
            log::info!(
                component = "nautilus::adapters::binance";
                "Binance info message SHOULD NOT APPEAR"
            );

            // Log from nautilus::adapters::binance at Warn - should pass
            log::warn!(
                component = "nautilus::adapters::binance";
                "Binance warn message"
            );

            // Log from unrelated component - should pass (no filter)
            log::trace!(
                component = "Portfolio";
                "Portfolio trace message"
            );

            drop(log_guard);

            wait_until(
                || {
                    std::fs::read_dir(&temp_dir)
                        .expect("Failed to read directory")
                        .filter_map(Result::ok)
                        .any(|entry| entry.path().is_file())
                },
                Duration::from_secs(3),
            );

            let log_file_path = std::fs::read_dir(&temp_dir)
                .expect("Failed to read directory")
                .filter_map(Result::ok)
                .find(|entry| entry.path().is_file())
                .expect("No log file found")
                .path();

            let log_contents =
                std::fs::read_to_string(log_file_path).expect("Error reading log file");

            assert!(
                log_contents.contains("OKX debug message"),
                "OKX debug should pass (longer prefix wins)"
            );
            assert!(
                log_contents.contains("OKX info message"),
                "OKX info should pass"
            );
            assert!(
                log_contents.contains("Binance warn message"),
                "Binance warn should pass"
            );
            assert!(
                log_contents.contains("Portfolio trace message"),
                "Unfiltered component should pass"
            );
            assert!(
                !log_contents.contains("SHOULD NOT APPEAR"),
                "Binance info should be filtered (adapters=Warn)"
            );
        }
    }

    #[cfg(all(feature = "simulation", madsim))]
    mod sim_tests {
        use std::sync::atomic::Ordering;

        use super::*;
        use crate::logging::LOGGING_BYPASSED;

        #[rstest]
        fn test_init_under_madsim_skips_writer_thread_and_forces_bypass() {
            let config = LoggerConfig {
                bypass_logging: false,
                ..Default::default()
            };
            let temp_dir = tempdir().expect("Failed to create temporary directory");
            let file_config = FileWriterConfig {
                directory: Some(temp_dir.path().to_str().unwrap().to_string()),
                ..Default::default()
            };

            let _guard = Logger::init_with_config(
                TraderId::from("TRADER-SIM"),
                UUID4::new(),
                config,
                file_config,
            )
            .expect("init should succeed under simulation");

            assert!(LOGGING_INITIALIZED.load(Ordering::SeqCst));
            assert!(
                LOGGING_BYPASSED.load(Ordering::SeqCst),
                "bypass must be forced under cfg(madsim) even when config disables it"
            );
            assert!(
                LOGGER_HANDLE
                    .lock()
                    .expect("LOGGER_HANDLE mutex should not be poisoned")
                    .is_none(),
                "writer thread must not be spawned under cfg(madsim)"
            );
        }
    }
}