test_logging.rs 2.1 KB

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