deribit_fix/utils.rs
1/******************************************************************************
2 Author: Joaquín Béjar García
3 Email: jb@taunais.com
4 Date: 6/3/26
5******************************************************************************/
6
7//! Utility functions for the Deribit FIX client
8
9use std::env;
10use std::sync::Once;
11use tracing::Level;
12use tracing_subscriber::FmtSubscriber;
13
14static INIT: Once = Once::new();
15
16/// Sets up the logger for the application.
17///
18/// The logger level is determined by the `DERIBIT_LOG_LEVEL` environment variable.
19/// If the variable is not set, it defaults to `INFO`.
20///
21/// This function can be called multiple times safely - it will only initialize
22/// the logger on the first call.
23///
24/// # Example
25///
26/// ```rust,no_run
27/// use deribit_fix::utils::setup_logger;
28///
29/// fn main() {
30/// setup_logger();
31/// // Application code here
32/// }
33/// ```
34pub fn setup_logger() {
35 INIT.call_once(|| {
36 let log_level = env::var("DERIBIT_LOG_LEVEL")
37 .unwrap_or_else(|_| "INFO".to_string())
38 .to_uppercase();
39
40 let level = match log_level.as_str() {
41 "DEBUG" => Level::DEBUG,
42 "ERROR" => Level::ERROR,
43 "WARN" => Level::WARN,
44 "TRACE" => Level::TRACE,
45 _ => Level::INFO,
46 };
47
48 let subscriber = FmtSubscriber::builder().with_max_level(level).finish();
49
50 if tracing::subscriber::set_global_default(subscriber).is_ok() {
51 tracing::debug!("Log level set to: {}", level);
52 }
53 });
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_setup_logger_can_be_called_multiple_times() {
62 setup_logger();
63 setup_logger();
64 // Should not panic
65 }
66}