1
0

lib.rs 3.0 KB

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