multiple_max_level_hints.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #![cfg(feature = "std")]
  2. use tracing::Level;
  3. use tracing_mock::*;
  4. #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
  5. #[test]
  6. fn multiple_max_level_hints() {
  7. // This test ensures that when multiple subscribers are active, their max
  8. // level hints are handled correctly. The global max level should be the
  9. // maximum of the level filters returned by the two `Subscriber`'s
  10. // `max_level_hint` method.
  11. //
  12. // In this test, we create a subscriber whose max level is `INFO`, and
  13. // another whose max level is `DEBUG`. We then add an assertion to both of
  14. // those subscribers' `enabled` method that no metadata for `TRACE` spans or
  15. // events are filtered, since they are disabled by the global max filter.
  16. fn do_events() {
  17. tracing::info!("doing a thing that you might care about");
  18. tracing::debug!("charging turboencabulator with interocitor");
  19. tracing::warn!("extremely serious warning, pay attention");
  20. tracing::trace!("interocitor charge level is 10%");
  21. tracing::error!("everything is on fire");
  22. }
  23. let (subscriber1, handle1) = subscriber::mock()
  24. .named("subscriber1")
  25. .with_max_level_hint(Level::INFO)
  26. .with_filter(|meta| {
  27. let level = dbg!(meta.level());
  28. assert!(
  29. level <= &Level::DEBUG,
  30. "a TRACE event was dynamically filtered by subscriber1"
  31. );
  32. level <= &Level::INFO
  33. })
  34. .event(event::mock().at_level(Level::INFO))
  35. .event(event::mock().at_level(Level::WARN))
  36. .event(event::mock().at_level(Level::ERROR))
  37. .done()
  38. .run_with_handle();
  39. let (subscriber2, handle2) = subscriber::mock()
  40. .named("subscriber2")
  41. .with_max_level_hint(Level::DEBUG)
  42. .with_filter(|meta| {
  43. let level = dbg!(meta.level());
  44. assert!(
  45. level <= &Level::DEBUG,
  46. "a TRACE event was dynamically filtered by subscriber2"
  47. );
  48. level <= &Level::DEBUG
  49. })
  50. .event(event::mock().at_level(Level::INFO))
  51. .event(event::mock().at_level(Level::DEBUG))
  52. .event(event::mock().at_level(Level::WARN))
  53. .event(event::mock().at_level(Level::ERROR))
  54. .done()
  55. .run_with_handle();
  56. let dispatch1 = tracing::Dispatch::new(subscriber1);
  57. tracing::dispatcher::with_default(&dispatch1, do_events);
  58. handle1.assert_finished();
  59. let dispatch2 = tracing::Dispatch::new(subscriber2);
  60. tracing::dispatcher::with_default(&dispatch2, do_events);
  61. handle2.assert_finished();
  62. }