logging.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. use fern::colors::{Color, ColoredLevelConfig};
  2. pub fn set_up_logging() {
  3. // configure colors for the whole line
  4. let colors_line = ColoredLevelConfig::new()
  5. .error(Color::Red)
  6. .warn(Color::Yellow)
  7. // we actually don't need to specify the color for debug and info, they are white by default
  8. .info(Color::White)
  9. .debug(Color::White)
  10. // depending on the terminals color scheme, this is the same as the background color
  11. .trace(Color::BrightBlack);
  12. // configure colors for the name of the level.
  13. // since almost all of them are the same as the color for the whole line, we
  14. // just clone `colors_line` and overwrite our changes
  15. let colors_level = colors_line.info(Color::Green);
  16. // here we set up our fern Dispatch
  17. fern::Dispatch::new()
  18. .format(move |out, message, record| {
  19. out.finish(format_args!(
  20. "{color_line}[{level}{color_line}] {message}\x1B[0m",
  21. color_line = format_args!(
  22. "\x1B[{}m",
  23. colors_line.get_color(&record.level()).to_fg_str()
  24. ),
  25. level = colors_level.color(record.level()),
  26. ));
  27. })
  28. .level(match std::env::var("DIOXUS_LOG") {
  29. Ok(level) => match level.to_lowercase().as_str() {
  30. "error" => log::LevelFilter::Error,
  31. "warn" => log::LevelFilter::Warn,
  32. "info" => log::LevelFilter::Info,
  33. "debug" => log::LevelFilter::Debug,
  34. "trace" => log::LevelFilter::Trace,
  35. _ => {
  36. panic!("Invalid log level: {}", level)
  37. }
  38. },
  39. Err(_) => log::LevelFilter::Info,
  40. })
  41. .chain(std::io::stdout())
  42. .apply()
  43. .unwrap();
  44. }