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
//! `tracing` subscriber setup, driven by `DERIBIT_LOG_LEVEL`.
//!
//! Installs a [`FmtSubscriber`] as the process-global default. The
//! level comes from the `DERIBIT_LOG_LEVEL` environment variable read
//! on the first call; subsequent calls are no-ops.
use env;
use Once;
use Level;
use FmtSubscriber;
static INIT: Once = new;
/// Install the `tracing` subscriber used by every example and binary
/// in the crate.
///
/// The level is read from the `DERIBIT_LOG_LEVEL` environment variable
/// once, on the first call. Recognised values: `TRACE`, `DEBUG`,
/// `INFO`, `WARN`, `ERROR`. Anything else (or an unset variable)
/// defaults to `INFO`.
///
/// The subscriber is registered as the *process-global* default via
/// [`tracing::subscriber::set_global_default`], which `tracing` allows
/// only once per process. A [`Once`] guard makes subsequent calls to
/// this function safe: they return immediately without touching the
/// already-installed subscriber, so applications that wire logging
/// from multiple entry points (tests, libs, `main`) do not race.
///
/// Changing `DERIBIT_LOG_LEVEL` after the first call has no effect —
/// tracing does not support replacing the global default at runtime.
///
/// # Example
///
/// ```rust,no_run
/// use deribit_websocket::utils::setup_logger;
///
/// // Set log level via environment variable (unsafe in Rust 2024 edition)
/// unsafe {
/// std::env::set_var("DERIBIT_LOG_LEVEL", "DEBUG");
/// }
///
/// // Initialize the logger
/// setup_logger();
///
/// // Now you can use tracing macros
/// tracing::info!("Logger initialized");
/// ```