1
0

common.rs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //! Common utilities for integration examples
  2. pub mod logger {
  3. use fern::colors::{Color, ColoredLevelConfig};
  4. use log::debug;
  5. pub fn set_up_logging(bin_name: &'static str) {
  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.clone().info(Color::Green);
  19. // here we set up our fern Dispatch
  20. fern::Dispatch::new()
  21. .format(move |out, message, record| {
  22. out.finish(format_args!(
  23. "{color_line}[{level}{color_line}] {message}\x1B[0m",
  24. color_line = format_args!(
  25. "\x1B[{}m",
  26. colors_line.get_color(&record.level()).to_fg_str()
  27. ),
  28. level = colors_level.color(record.level()),
  29. message = message,
  30. ));
  31. })
  32. // set the default log level. to filter out verbose log messages from dependencies, set
  33. // this to Warn and overwrite the log level for your crate.
  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(bin_name, log::LevelFilter::Info)
  40. // .level_for("pretty_colored", log::LevelFilter::Trace)
  41. // output to stdout
  42. .chain(std::io::stdout())
  43. .apply()
  44. .unwrap();
  45. }
  46. }