nautilus_common/python/
logging.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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
16use std::collections::HashMap;
17
18use log::LevelFilter;
19use nautilus_core::{UUID4, python::to_pyvalue_err};
20use nautilus_model::identifiers::TraderId;
21use pyo3::prelude::*;
22use ustr::Ustr;
23
24use crate::{
25    enums::{LogColor, LogLevel},
26    logging::{
27        self, headers,
28        logger::{self, LogGuard, LoggerConfig},
29        logging_clock_set_realtime_mode, logging_clock_set_static_mode,
30        logging_clock_set_static_time, logging_set_bypass, map_log_level_to_filter,
31        parse_level_filter_str,
32        writer::FileWriterConfig,
33    },
34};
35
36#[pymethods]
37impl LoggerConfig {
38    /// Creates a [`LoggerConfig`] from a spec string.
39    ///
40    /// # Errors
41    ///
42    /// Returns a Python exception if the spec string is invalid.
43    #[staticmethod]
44    #[pyo3(name = "from_spec")]
45    pub fn py_from_spec(spec: String) -> PyResult<Self> {
46        Self::from_spec(&spec).map_err(to_pyvalue_err)
47    }
48}
49
50#[pymethods]
51impl FileWriterConfig {
52    #[new]
53    #[pyo3(signature = (directory=None, file_name=None, file_format=None, file_rotate=None))]
54    #[must_use]
55    pub fn py_new(
56        directory: Option<String>,
57        file_name: Option<String>,
58        file_format: Option<String>,
59        file_rotate: Option<(u64, u32)>,
60    ) -> Self {
61        Self::new(directory, file_name, file_format, file_rotate)
62    }
63}
64
65/// Initialize tracing.
66///
67/// Tracing is meant to be used to trace/debug async Rust code. It can be
68/// configured to filter modules and write up to a specific level only using
69/// by passing a configuration using the `RUST_LOG` environment variable.
70///
71/// # Safety
72///
73/// Should only be called once during an applications run, ideally at the
74/// beginning of the run.
75///
76/// # Errors
77///
78/// Returns an error if tracing subscriber fails to initialize.
79#[pyfunction()]
80#[pyo3(name = "init_tracing")]
81pub fn py_init_tracing() -> PyResult<()> {
82    logging::init_tracing().map_err(to_pyvalue_err)
83}
84
85/// Initialize logging.
86///
87/// Logging should be used for Python and sync Rust logic which is most of
88/// the components in the [nautilus_trader](https://pypi.org/project/nautilus_trader) package.
89/// Logging can be configured to filter components and write up to a specific level only
90/// by passing a configuration using the `NAUTILUS_LOG` environment variable.
91///
92/// # Safety
93///
94/// Should only be called once during an applications run, ideally at the
95/// beginning of the run.
96/// Initializes logging via Python interface.
97///
98/// # Errors
99///
100/// Returns a Python exception if logger initialization fails.
101#[pyfunction]
102#[pyo3(name = "init_logging")]
103#[allow(clippy::too_many_arguments)]
104#[pyo3(signature = (trader_id, instance_id, level_stdout, level_file=None, component_levels=None, directory=None, file_name=None, file_format=None, file_rotate=None, is_colored=None, is_bypassed=None, print_config=None, log_components_only=None))]
105pub fn py_init_logging(
106    trader_id: TraderId,
107    instance_id: UUID4,
108    level_stdout: LogLevel,
109    level_file: Option<LogLevel>,
110    component_levels: Option<HashMap<String, String>>,
111    directory: Option<String>,
112    file_name: Option<String>,
113    file_format: Option<String>,
114    file_rotate: Option<(u64, u32)>,
115    is_colored: Option<bool>,
116    is_bypassed: Option<bool>,
117    print_config: Option<bool>,
118    log_components_only: Option<bool>,
119) -> PyResult<LogGuard> {
120    let level_file = level_file.map_or(LevelFilter::Off, map_log_level_to_filter);
121
122    let config = LoggerConfig::new(
123        map_log_level_to_filter(level_stdout),
124        level_file,
125        parse_component_levels(component_levels),
126        log_components_only.unwrap_or(false),
127        is_colored.unwrap_or(true),
128        print_config.unwrap_or(false),
129    );
130
131    let file_config = FileWriterConfig::new(directory, file_name, file_format, file_rotate);
132
133    if is_bypassed.unwrap_or(false) {
134        logging_set_bypass();
135    }
136
137    logging::init_logging(trader_id, instance_id, config, file_config).map_err(to_pyvalue_err)
138}
139
140#[pyfunction()]
141#[pyo3(name = "logger_flush")]
142pub fn py_logger_flush() {
143    log::logger().flush()
144}
145
146fn parse_component_levels(
147    original_map: Option<HashMap<String, String>>,
148) -> HashMap<Ustr, LevelFilter> {
149    match original_map {
150        Some(map) => {
151            let mut new_map = HashMap::new();
152            for (key, value) in map {
153                let ustr_key = Ustr::from(&key);
154                let value = parse_level_filter_str(&value);
155                new_map.insert(ustr_key, value);
156            }
157            new_map
158        }
159        None => HashMap::new(),
160    }
161}
162
163/// Create a new log event.
164#[pyfunction]
165#[pyo3(name = "logger_log")]
166pub fn py_logger_log(level: LogLevel, color: LogColor, component: &str, message: &str) {
167    logger::log(level, color, Ustr::from(component), message);
168}
169
170/// Logs the standard Nautilus system header.
171#[pyfunction]
172#[pyo3(name = "log_header")]
173pub fn py_log_header(trader_id: TraderId, machine_id: &str, instance_id: UUID4, component: &str) {
174    headers::log_header(trader_id, machine_id, instance_id, Ustr::from(component));
175}
176
177/// Logs system information.
178#[pyfunction]
179#[pyo3(name = "log_sysinfo")]
180pub fn py_log_sysinfo(component: &str) {
181    headers::log_sysinfo(Ustr::from(component));
182}
183
184#[pyfunction]
185#[pyo3(name = "logging_clock_set_static_mode")]
186pub fn py_logging_clock_set_static_mode() {
187    logging_clock_set_static_mode();
188}
189
190#[pyfunction]
191#[pyo3(name = "logging_clock_set_realtime_mode")]
192pub fn py_logging_clock_set_realtime_mode() {
193    logging_clock_set_realtime_mode();
194}
195
196#[pyfunction]
197#[pyo3(name = "logging_clock_set_static_time")]
198pub fn py_logging_clock_set_static_time(time_ns: u64) {
199    logging_clock_set_static_time(time_ns);
200}
201
202/// A thin wrapper around the global Rust logger which exposes ergonomic
203/// logging helpers for Python code.
204///
205/// It mirrors the familiar Python `logging` interface while forwarding
206/// all records through the Nautilus logging infrastructure so that log levels
207/// and formatting remain consistent across Rust and Python.
208#[pyclass(
209    module = "nautilus_trader.core.nautilus_pyo3.common",
210    name = "Logger",
211    unsendable
212)]
213#[derive(Debug, Clone)]
214pub struct PyLogger {
215    name: Ustr,
216}
217
218impl PyLogger {
219    pub fn new(name: &str) -> Self {
220        Self {
221            name: Ustr::from(name),
222        }
223    }
224}
225
226#[pymethods]
227impl PyLogger {
228    /// Create a new `Logger` instance.
229    #[new]
230    #[pyo3(signature = (name="Python"))]
231    fn py_new(name: &str) -> Self {
232        Self::new(name)
233    }
234
235    /// The component identifier carried by this logger.
236    #[getter]
237    fn name(&self) -> &str {
238        &self.name
239    }
240
241    /// Emit a TRACE level record.
242    #[pyo3(name = "trace")]
243    fn py_trace(&self, message: &str, color: Option<LogColor>) {
244        self._log(LogLevel::Trace, color, message);
245    }
246
247    /// Emit a DEBUG level record.
248    #[pyo3(name = "debug")]
249    fn py_debug(&self, message: &str, color: Option<LogColor>) {
250        self._log(LogLevel::Debug, color, message);
251    }
252
253    /// Emit an INFO level record.
254    #[pyo3(name = "info")]
255    fn py_info(&self, message: &str, color: Option<LogColor>) {
256        self._log(LogLevel::Info, color, message);
257    }
258
259    /// Emit a WARNING level record.
260    #[pyo3(name = "warning")]
261    fn py_warning(&self, message: &str, color: Option<LogColor>) {
262        self._log(LogLevel::Warning, color, message);
263    }
264
265    /// Emit an ERROR level record.
266    #[pyo3(name = "error")]
267    fn py_error(&self, message: &str, color: Option<LogColor>) {
268        self._log(LogLevel::Error, color, message);
269    }
270
271    /// Emit an ERROR level record with the active Python exception info.
272    #[pyo3(name = "exception")]
273    #[pyo3(signature = (message="", color=None))]
274    fn py_exception(&self, py: Python, message: &str, color: Option<LogColor>) {
275        let mut full_msg = message.to_owned();
276
277        if pyo3::PyErr::occurred(py) {
278            let err = PyErr::fetch(py);
279            let err_str = err.to_string();
280            if full_msg.is_empty() {
281                full_msg = err_str;
282            } else {
283                full_msg = format!("{full_msg}: {err_str}");
284            }
285        }
286
287        self._log(LogLevel::Error, color, &full_msg);
288    }
289
290    /// Flush buffered log records.
291    #[pyo3(name = "flush")]
292    fn py_flush(&self) {
293        log::logger().flush();
294    }
295
296    fn _log(&self, level: LogLevel, color: Option<LogColor>, message: &str) {
297        let color = color.unwrap_or(LogColor::Normal);
298        logger::log(level, color, self.name, message);
299    }
300}