dioxus_logger/
lib.rs

1use tracing::{
2    subscriber::{set_global_default, SetGlobalDefaultError},
3    Level,
4};
5
6pub use tracing;
7
8/// Attempt to initialize the subscriber if it doesn't already exist, with default settings.
9///
10/// See [`crate::init`] for more info.
11///
12/// If you're doing setup before your `dioxus::launch` function that requires lots of logging, then
13/// it might be worth calling this earlier than launch.
14///
15/// `dioxus::launch` calls this for you automatically and won't replace any facade you've already set.
16///
17/// # Example
18///
19/// ```rust,no_run
20/// use dioxus::prelude::*;
21/// use tracing::info;
22///
23/// fn main() {
24///     dioxus::logger::initialize_default();
25///
26///     info!("Doing some work before launching...");
27///
28///     dioxus::launch(App);
29/// }
30///
31/// #[component]
32/// fn App() -> Element {
33///     info!("App rendered");
34///     rsx! {
35///         p { "hi" }
36///     }
37/// }
38/// ```
39pub fn initialize_default() {
40    if tracing::dispatcher::has_been_set() {
41        return;
42    }
43
44    if cfg!(debug_assertions) {
45        _ = init(Level::DEBUG);
46    } else {
47        _ = init(Level::INFO);
48    }
49}
50
51/// Initialize `dioxus-logger` with a specified max filter.
52///
53/// Generally it is best to initialize the logger before launching your Dioxus app.
54/// Works on Web, Desktop, Fullstack, and Liveview.
55///
56/// # Example
57///
58/// ```rust,no_run
59/// use dioxus::prelude::*;
60/// use dioxus::logger::tracing::{Level, info};
61///
62/// fn main() {
63///     dioxus::logger::init(Level::INFO).expect("logger failed to init");
64///     dioxus::launch(App);
65/// }
66///
67/// #[component]
68/// fn App() -> Element {
69///     info!("App rendered");
70///     rsx! {
71///         p { "hi" }
72///     }
73/// }
74/// ```
75pub fn init(level: Level) -> Result<(), SetGlobalDefaultError> {
76    /*
77    The default logger is currently set to log in fmt mode (meaning print directly to stdout)
78
79    Eventually we want to change the output mode to be `json` when running under `dx`. This would let
80    use re-format the tracing spans to be better integrated with `dx`
81    */
82
83    #[cfg(target_arch = "wasm32")]
84    {
85        use tracing_subscriber::layer::SubscriberExt;
86        use tracing_subscriber::Registry;
87
88        let layer_config = tracing_wasm::WASMLayerConfigBuilder::new()
89            .set_max_level(level)
90            .build();
91        let layer = tracing_wasm::WASMLayer::new(layer_config);
92        let reg = Registry::default().with(layer);
93
94        console_error_panic_hook::set_once();
95        set_global_default(reg)
96    }
97
98    #[cfg(not(target_arch = "wasm32"))]
99    {
100        let sub = tracing_subscriber::FmtSubscriber::builder().with_max_level(level);
101
102        if !dioxus_cli_config::is_cli_enabled() {
103            return set_global_default(sub.finish());
104        }
105
106        // todo(jon): this is a small hack to clean up logging when running under the CLI
107        // eventually we want to emit everything as json and let the CLI manage the parsing + display
108        set_global_default(sub.without_time().with_target(false).finish())
109    }
110}