logging.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //! Dioxus ships out-of-the-box with tracing hooks that integrate with the Dioxus-CLI.
  2. //!
  3. //! The built-in tracing-subscriber automatically sets up a wasm panic hook and wires up output
  4. //! to be consumed in a machine-readable format when running under `dx`.
  5. //!
  6. //! You can disable the built-in tracing-subscriber or customize the log level yourself.
  7. //!
  8. //! By default:
  9. //! - in `dev` mode, the default log output is `debug`
  10. //! - in `release` mode, the default log output is `info`
  11. //!
  12. //! To use the dioxus logger in your app, simply call any of the tracing functions (info!(), warn!(), error!())
  13. use dioxus::logger::tracing::{debug, error, info, warn, Level};
  14. use dioxus::prelude::*;
  15. fn main() {
  16. // `dioxus::logger::init` is optional and called automatically by `dioxus::launch`.
  17. // In development mode, the `Debug` tracing level is set, and in release only the `Info` level is set.
  18. // You can call it yourself manually in the cases you:
  19. // - want to customize behavior
  20. // - aren't using `dioxus::launch` (i.e. custom fullstack setups) but want the integration.
  21. // The Tracing crate is the logging interface that the dioxus-logger uses.
  22. dioxus::logger::init(Level::INFO).expect("Failed to initialize logger");
  23. dioxus::launch(app);
  24. }
  25. fn app() -> Element {
  26. rsx! {
  27. div {
  28. h1 { "Logger demo" }
  29. button {
  30. onclick: move |_| warn!("Here's a warning!"),
  31. "Warn!"
  32. }
  33. button {
  34. onclick: move |_| error!("Here's an error!"),
  35. "Error!"
  36. }
  37. button {
  38. onclick: move |_| {
  39. debug!("Here's a debug");
  40. warn!("The log level is set to info so there should not be a debug message")
  41. },
  42. "Debug!"
  43. }
  44. button {
  45. onclick: move |_| info!("Here's an info!"),
  46. "Info!"
  47. }
  48. }
  49. }
  50. }