Skip to main content

nautilus_common/logging/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! The logging framework for Nautilus systems.
17//!
18//! This module implements a high-performance logging subsystem that operates in a separate thread
19//! using an MPSC channel for log message delivery. The system uses reference counting to track
20//! active `LogGuard` instances, ensuring the logging thread completes all pending writes before
21//! termination.
22//!
23//! # LogGuard Reference Counting
24//!
25//! The logging system maintains a global count of active `LogGuard` instances using an atomic
26//! counter (`LOGGING_GUARDS_ACTIVE`). When a `LogGuard` is created, the counter is incremented,
27//! and when dropped, it's decremented. When the last `LogGuard` is dropped (counter reaches zero),
28//! the logging thread is properly joined to ensure all buffered log messages are written to their
29//! destinations before the process terminates.
30//!
31//! The system supports a maximum of 255 concurrent `LogGuard` instances. Attempting to create
32//! more will cause a panic.
33
34pub mod config;
35pub mod headers;
36pub mod logger;
37pub mod macros;
38pub mod writer;
39
40#[cfg(feature = "tracing-bridge")]
41pub mod bridge;
42
43use std::{
44    collections::HashMap,
45    env,
46    str::FromStr,
47    sync::{
48        OnceLock,
49        atomic::{AtomicBool, AtomicU8, Ordering},
50    },
51};
52
53use ahash::AHashMap;
54use log::LevelFilter;
55// Re-exports
56pub use macros::{log_debug, log_error, log_info, log_trace, log_warn};
57use nautilus_core::{UUID4, time::get_atomic_clock_static};
58use nautilus_model::identifiers::TraderId;
59use ustr::Ustr;
60
61use self::{
62    logger::{LogGuard, Logger, LoggerConfig},
63    writer::FileWriterConfig,
64};
65use crate::enums::LogLevel;
66
67pub const RECV: &str = "<--";
68pub const SEND: &str = "-->";
69pub const CMD: &str = "[CMD]";
70pub const EVT: &str = "[EVT]";
71pub const DOC: &str = "[DOC]";
72pub const RPT: &str = "[RPT]";
73pub const REQ: &str = "[REQ]";
74pub const RES: &str = "[RES]";
75
76static LOGGING_INITIALIZED: AtomicBool = AtomicBool::new(false);
77static LOGGING_BYPASSED: AtomicBool = AtomicBool::new(false);
78static LOGGING_REALTIME: AtomicBool = AtomicBool::new(true);
79static LOGGING_COLORED: AtomicBool = AtomicBool::new(true);
80static LOGGING_GUARDS_ACTIVE: AtomicU8 = AtomicU8::new(0);
81static LAZY_GUARD: OnceLock<Option<LogGuard>> = OnceLock::new();
82
83/// Returns whether the core logger is enabled.
84pub fn logging_is_initialized() -> bool {
85    LOGGING_INITIALIZED.load(Ordering::Relaxed)
86}
87
88/// Ensures logging is initialized on first use.
89///
90/// If `NAUTILUS_LOG` is set, initializes the logger with the specified config.
91/// Otherwise, initializes with INFO level to stdout. This enables lazy
92/// initialization for Rust-only binaries that don't go through the Python
93/// kernel initialization.
94///
95/// Returns `true` if logging is available (either already initialized or
96/// successfully lazy-initialized), `false` otherwise.
97pub fn ensure_logging_initialized() -> bool {
98    if LOGGING_INITIALIZED.load(Ordering::SeqCst) {
99        return true;
100    }
101
102    LAZY_GUARD.get_or_init(|| {
103        let config = env::var("NAUTILUS_LOG")
104            .ok()
105            .and_then(|spec| LoggerConfig::from_spec(&spec).ok())
106            .unwrap_or_default();
107
108        Logger::init_with_config(
109            TraderId::default(),
110            UUID4::default(),
111            config,
112            FileWriterConfig::default(),
113        )
114        .ok()
115    });
116
117    LOGGING_INITIALIZED.load(Ordering::SeqCst)
118}
119
120/// Sets the logging subsystem to bypass mode.
121pub fn logging_set_bypass() {
122    LOGGING_BYPASSED.store(true, Ordering::Relaxed);
123}
124
125/// Shuts down the logging subsystem.
126pub fn logging_shutdown() {
127    // Perform a graceful shutdown: prevent new logs, signal Close, drain and join.
128    // Delegates to logger implementation which has access to the internals.
129    crate::logging::logger::shutdown_graceful();
130}
131
132/// Returns whether the core logger is using ANSI colors.
133pub fn logging_is_colored() -> bool {
134    LOGGING_COLORED.load(Ordering::Relaxed)
135}
136
137/// Sets the global logging clock to real-time mode.
138pub fn logging_clock_set_realtime_mode() {
139    LOGGING_REALTIME.store(true, Ordering::Relaxed);
140}
141
142/// Sets the global logging clock to static mode.
143pub fn logging_clock_set_static_mode() {
144    LOGGING_REALTIME.store(false, Ordering::Relaxed);
145}
146
147/// Sets the global logging clock static time with the given UNIX timestamp (nanoseconds).
148pub fn logging_clock_set_static_time(time_ns: u64) {
149    let clock = get_atomic_clock_static();
150    clock.set_time(time_ns.into());
151}
152
153/// Initialize logging.
154///
155/// Logging should be used for Python and sync Rust logic which is most of
156/// the components in the [nautilus_trader](https://pypi.org/project/nautilus_trader) package.
157/// Logging can be configured to filter components and write up to a specific level only
158/// by passing a configuration using the `NAUTILUS_LOG` environment variable.
159///
160/// Should only be called once during an applications run, ideally at the
161/// beginning of the run.
162///
163/// # Errors
164///
165/// Returns an error if the logging subsystem fails to initialize.
166pub fn init_logging(
167    trader_id: TraderId,
168    instance_id: UUID4,
169    config: LoggerConfig,
170    file_config: FileWriterConfig,
171) -> anyhow::Result<LogGuard> {
172    Logger::init_with_config(trader_id, instance_id, config, file_config)
173}
174
175#[must_use]
176pub const fn map_log_level_to_filter(log_level: LogLevel) -> LevelFilter {
177    match log_level {
178        LogLevel::Off => LevelFilter::Off,
179        LogLevel::Trace => LevelFilter::Trace,
180        LogLevel::Debug => LevelFilter::Debug,
181        LogLevel::Info => LevelFilter::Info,
182        LogLevel::Warning => LevelFilter::Warn,
183        LogLevel::Error => LevelFilter::Error,
184    }
185}
186
187/// Parses a string into a [`LevelFilter`].
188///
189/// # Errors
190///
191/// Returns an error if the provided string is not a valid `LevelFilter`.
192pub fn parse_level_filter_str(s: &str) -> anyhow::Result<LevelFilter> {
193    let mut log_level_str = s.to_string().to_uppercase();
194    if log_level_str == "WARNING" {
195        log_level_str = "WARN".to_string();
196    }
197    LevelFilter::from_str(&log_level_str)
198        .map_err(|_| anyhow::anyhow!("Invalid log level string: '{s}'"))
199}
200
201/// Parses component-specific log levels from a JSON value map.
202///
203/// # Errors
204///
205/// Returns an error if a JSON value in the map is not a string or is not a valid log level.
206pub fn parse_component_levels(
207    original_map: Option<HashMap<String, serde_json::Value>>,
208) -> anyhow::Result<AHashMap<Ustr, LevelFilter>> {
209    match original_map {
210        Some(map) => {
211            let mut new_map = AHashMap::new();
212
213            for (key, value) in map {
214                let ustr_key = Ustr::from(&key);
215                let s = value.as_str().ok_or_else(|| {
216                    anyhow::anyhow!(
217                        "Component log level for '{key}' must be a string, was: {value}"
218                    )
219                })?;
220                let lvl = parse_level_filter_str(s)?;
221                new_map.insert(ustr_key, lvl);
222            }
223            Ok(new_map)
224        }
225        None => Ok(AHashMap::new()),
226    }
227}
228
229/// Logs that a task has started.
230pub fn log_task_started(task_name: &str) {
231    log::debug!("Started task '{task_name}'");
232}
233
234/// Logs that a task has stopped.
235pub fn log_task_stopped(task_name: &str) {
236    log::debug!("Stopped task '{task_name}'");
237}
238
239/// Logs that a task is being awaited.
240pub fn log_task_awaiting(task_name: &str) {
241    log::debug!("Awaiting task '{task_name}'");
242}
243
244/// Logs that a task was aborted.
245pub fn log_task_aborted(task_name: &str) {
246    log::debug!("Aborted task '{task_name}'");
247}
248
249/// Logs that there was an error in a task.
250pub fn log_task_error(task_name: &str, e: &anyhow::Error) {
251    log::error!("Error in task '{task_name}': {e}");
252}
253
254#[cfg(test)]
255mod tests {
256    use rstest::rstest;
257
258    use super::*;
259
260    #[rstest]
261    #[case("DEBUG", LevelFilter::Debug)]
262    #[case("debug", LevelFilter::Debug)]
263    #[case("Debug", LevelFilter::Debug)]
264    #[case("DeBuG", LevelFilter::Debug)]
265    #[case("INFO", LevelFilter::Info)]
266    #[case("info", LevelFilter::Info)]
267    #[case("WARNING", LevelFilter::Warn)]
268    #[case("warning", LevelFilter::Warn)]
269    #[case("WARN", LevelFilter::Warn)]
270    #[case("warn", LevelFilter::Warn)]
271    #[case("ERROR", LevelFilter::Error)]
272    #[case("error", LevelFilter::Error)]
273    #[case("OFF", LevelFilter::Off)]
274    #[case("off", LevelFilter::Off)]
275    #[case("TRACE", LevelFilter::Trace)]
276    #[case("trace", LevelFilter::Trace)]
277    fn test_parse_level_filter_str_case_insensitive(
278        #[case] input: &str,
279        #[case] expected: LevelFilter,
280    ) {
281        let result = parse_level_filter_str(input).unwrap();
282        assert_eq!(result, expected);
283    }
284
285    #[rstest]
286    #[case("INVALID")]
287    #[case("DEBG")]
288    #[case("WARNINGG")]
289    #[case("")]
290    #[case("INFO123")]
291    fn test_parse_level_filter_str_invalid_returns_error(#[case] invalid_input: &str) {
292        let result = parse_level_filter_str(invalid_input);
293
294        assert!(result.is_err());
295        assert!(
296            result
297                .unwrap_err()
298                .to_string()
299                .contains("Invalid log level")
300        );
301    }
302
303    #[rstest]
304    fn test_parse_component_levels_valid() {
305        let mut map = HashMap::new();
306        map.insert(
307            "Strategy1".to_string(),
308            serde_json::Value::String("DEBUG".to_string()),
309        );
310        map.insert(
311            "Strategy2".to_string(),
312            serde_json::Value::String("info".to_string()),
313        );
314
315        let result = parse_component_levels(Some(map)).unwrap();
316
317        assert_eq!(result.len(), 2);
318        assert_eq!(result[&Ustr::from("Strategy1")], LevelFilter::Debug);
319        assert_eq!(result[&Ustr::from("Strategy2")], LevelFilter::Info);
320    }
321
322    #[rstest]
323    fn test_parse_component_levels_non_string_value_returns_error() {
324        let mut map = HashMap::new();
325        map.insert(
326            "Strategy1".to_string(),
327            serde_json::Value::Number(123.into()),
328        );
329
330        let result = parse_component_levels(Some(map));
331
332        assert!(result.is_err());
333        assert!(result.unwrap_err().to_string().contains("must be a string"));
334    }
335
336    #[rstest]
337    fn test_parse_component_levels_invalid_level_returns_error() {
338        let mut map = HashMap::new();
339        map.insert(
340            "Strategy1".to_string(),
341            serde_json::Value::String("INVALID_LEVEL".to_string()),
342        );
343
344        let result = parse_component_levels(Some(map));
345
346        assert!(result.is_err());
347        assert!(
348            result
349                .unwrap_err()
350                .to_string()
351                .contains("Invalid log level")
352        );
353    }
354
355    #[rstest]
356    fn test_parse_component_levels_none_returns_empty() {
357        let result = parse_component_levels(None).unwrap();
358        assert_eq!(result.len(), 0);
359    }
360
361    #[rstest]
362    fn test_logging_clock_set_static_mode() {
363        logging_clock_set_static_mode();
364        assert!(!LOGGING_REALTIME.load(Ordering::Relaxed));
365    }
366
367    #[rstest]
368    fn test_logging_clock_set_realtime_mode() {
369        logging_clock_set_realtime_mode();
370        assert!(LOGGING_REALTIME.load(Ordering::Relaxed));
371    }
372
373    #[rstest]
374    fn test_logging_clock_set_static_time() {
375        let test_time: u64 = 1_700_000_000_000_000_000;
376        logging_clock_set_static_time(test_time);
377        let clock = get_atomic_clock_static();
378        assert_eq!(clock.get_time_ns(), test_time);
379    }
380
381    #[rstest]
382    fn test_logging_set_bypass() {
383        logging_set_bypass();
384        assert!(LOGGING_BYPASSED.load(Ordering::Relaxed));
385    }
386
387    #[rstest]
388    fn test_map_log_level_to_filter() {
389        assert_eq!(map_log_level_to_filter(LogLevel::Off), LevelFilter::Off);
390        assert_eq!(map_log_level_to_filter(LogLevel::Trace), LevelFilter::Trace);
391        assert_eq!(map_log_level_to_filter(LogLevel::Debug), LevelFilter::Debug);
392        assert_eq!(map_log_level_to_filter(LogLevel::Info), LevelFilter::Info);
393        assert_eq!(
394            map_log_level_to_filter(LogLevel::Warning),
395            LevelFilter::Warn
396        );
397        assert_eq!(map_log_level_to_filter(LogLevel::Error), LevelFilter::Error);
398    }
399
400    #[rstest]
401    fn test_ensure_logging_initialized_returns_consistent_value() {
402        // This test verifies ensure_logging_initialized() can be called safely.
403        // Due to Once semantics, we can only test one code path per process.
404        //
405        // With nextest (process isolation per test):
406        // - If NAUTILUS_LOG is unset, this returns false.
407        // - If NAUTILUS_LOG is set externally, it may return true.
408        //
409        // The key invariant: multiple calls return the same value.
410        let first_call = ensure_logging_initialized();
411        let second_call = ensure_logging_initialized();
412
413        assert_eq!(
414            first_call, second_call,
415            "ensure_logging_initialized must be idempotent"
416        );
417        assert_eq!(
418            first_call,
419            logging_is_initialized(),
420            "ensure_logging_initialized return value must match logging_is_initialized()"
421        );
422    }
423
424    #[rstest]
425    fn test_ensure_logging_initialized_fast_path() {
426        // If logging is already initialized, the fast path returns true immediately.
427        // This test documents the expected behavior.
428        if logging_is_initialized() {
429            assert!(
430                ensure_logging_initialized(),
431                "Fast path should return true when already initialized"
432            );
433        }
434        // If not initialized, we can't test the initialization path here
435        // without side effects that affect other tests.
436    }
437}