logging.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. //! One tracing setup for every binary.
  2. //!
  3. //! Filter with `VICTORY_LOG` (falls back to `RUST_LOG`), e.g.
  4. //! `VICTORY_LOG=victory_loader=trace,info`. Debug and trace output exist
  5. //! only in builds with the `diagnostics` feature; a plain release build
  6. //! logs info and above whatever the filter asks for.
  7. use tracing_subscriber::EnvFilter;
  8. use tracing_subscriber::filter::LevelFilter;
  9. use tracing_subscriber::layer::SubscriberExt;
  10. use tracing_subscriber::util::SubscriberInitExt;
  11. /// The most verbose level this build can log.
  12. const CAP: LevelFilter = if cfg!(feature = "diagnostics") {
  13. LevelFilter::TRACE
  14. } else {
  15. LevelFilter::INFO
  16. };
  17. pub fn init(default_level: &str) {
  18. let filter = std::env::var("VICTORY_LOG")
  19. .or_else(|_| std::env::var("RUST_LOG"))
  20. .ok()
  21. .and_then(|v| EnvFilter::try_new(v).ok())
  22. .unwrap_or_else(|| EnvFilter::new(default_level));
  23. let _ = tracing_subscriber::registry()
  24. .with(CAP)
  25. .with(filter)
  26. .with(
  27. tracing_subscriber::fmt::layer()
  28. .with_target(true)
  29. .with_writer(std::io::stderr),
  30. )
  31. .try_init();
  32. }