logger.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //! Debug error logging.
  2. use std::ffi::OsStr;
  3. use ansi_term::{Colour, ANSIString};
  4. /// Sets the internal logger, changing the log level based on the value of an
  5. /// environment variable.
  6. pub fn configure<T: AsRef<OsStr>>(ev: Option<T>) {
  7. let ev = match ev {
  8. Some(v) => v,
  9. None => return,
  10. };
  11. let env_var = ev.as_ref();
  12. if env_var.is_empty() {
  13. return;
  14. }
  15. if env_var == "trace" {
  16. log::set_max_level(log::LevelFilter::Trace);
  17. }
  18. else {
  19. log::set_max_level(log::LevelFilter::Debug);
  20. }
  21. let result = log::set_logger(GLOBAL_LOGGER);
  22. if let Err(e) = result {
  23. eprintln!("Failed to initialise logger: {}", e);
  24. }
  25. }
  26. #[derive(Debug)]
  27. struct Logger;
  28. const GLOBAL_LOGGER: &Logger = &Logger;
  29. impl log::Log for Logger {
  30. fn enabled(&self, _: &log::Metadata<'_>) -> bool {
  31. true // no need to filter after using ‘set_max_level’.
  32. }
  33. fn log(&self, record: &log::Record<'_>) {
  34. let open = Colour::Fixed(243).paint("[");
  35. let level = level(record.level());
  36. let close = Colour::Fixed(243).paint("]");
  37. eprintln!("{}{} {}{} {}", open, level, record.target(), close, record.args());
  38. }
  39. fn flush(&self) {
  40. // no need to flush with ‘eprintln!’.
  41. }
  42. }
  43. fn level(level: log::Level) -> ANSIString<'static> {
  44. match level {
  45. log::Level::Error => Colour::Red.paint("ERROR"),
  46. log::Level::Warn => Colour::Yellow.paint("WARN"),
  47. log::Level::Info => Colour::Cyan.paint("INFO"),
  48. log::Level::Debug => Colour::Blue.paint("DEBUG"),
  49. log::Level::Trace => Colour::Fixed(245).paint("TRACE"),
  50. }
  51. }