test_logging.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. pub fn set_up_logging(enabled: bool) {
  2. use fern::colors::{Color, ColoredLevelConfig};
  3. if !enabled {
  4. return;
  5. }
  6. // configure colors for the whole line
  7. let colors_line = ColoredLevelConfig::new()
  8. .error(Color::Red)
  9. .warn(Color::Yellow)
  10. // we actually don't need to specify the color for debug and info, they are white by default
  11. .info(Color::White)
  12. .debug(Color::White)
  13. // depending on the terminals color scheme, this is the same as the background color
  14. .trace(Color::BrightBlack);
  15. // configure colors for the name of the level.
  16. // since almost all of them are the same as the color for the whole line, we
  17. // just clone `colors_line` and overwrite our changes
  18. let colors_level = colors_line.info(Color::Green);
  19. // here we set up our fern Dispatch
  20. // when running tests in batch, the logger is re-used, so ignore the logger error
  21. let _ = fern::Dispatch::new()
  22. .format(move |out, message, record| {
  23. out.finish(format_args!(
  24. "{color_line}[{level}{color_line}] {message}\x1B[0m",
  25. color_line = format_args!(
  26. "\x1B[{}m",
  27. colors_line.get_color(&record.level()).to_fg_str()
  28. ),
  29. level = colors_level.color(record.level()),
  30. message = message,
  31. ));
  32. })
  33. // set the default log level. to filter out verbose log messages from dependencies, set
  34. // this to Warn and overwrite the log level for your crate.
  35. .level(log::LevelFilter::Debug)
  36. // .level(log::LevelFilter::Warn)
  37. // change log levels for individual modules. Note: This looks for the record's target
  38. // field which defaults to the module path but can be overwritten with the `target`
  39. // parameter:
  40. // `info!(target="special_target", "This log message is about special_target");`
  41. // .level_for("dioxus", log::LevelFilter::Debug)
  42. // .level_for("dioxus", log::LevelFilter::Info)
  43. // .level_for("pretty_colored", log::LevelFilter::Trace)
  44. // output to stdout
  45. .chain(std::io::stdout())
  46. .apply();
  47. }