Exercise: Logger Trait

Let’s design a simple logging utility, using a trait Logger with a log method. Code which might log its progress can then take an &impl Logger. In testing, this might put messages in the test logfile, while in a production build it would send messages to a log server.

However, the StderrLogger given below logs all messages, regardless of verbosity. Your task is to write a VerbosityFilter type that will ignore messages above a maximum verbosity.

This is a common pattern: a struct wrapping a trait implementation and implementing that same trait, adding behavior in the process. What other kinds of wrappers might be useful in a logging utility?

  1. use std::fmt::Display;
  2. pub trait Logger {
  3. /// Log a message at the given verbosity level.
  4. fn log(&self, verbosity: u8, message: impl Display);
  5. }
  6. struct StderrLogger;
  7. impl Logger for StderrLogger {
  8. fn log(&self, verbosity: u8, message: impl Display) {
  9. eprintln!("verbosity={verbosity}: {message}");
  10. }
  11. }
  12. fn do_things(logger: &impl Logger) {
  13. logger.log(5, "FYI");
  14. logger.log(2, "Uhoh");
  15. }
  16. // TODO: Define and implement `VerbosityFilter`.
  17. fn main() {
  18. let l = VerbosityFilter { max_verbosity: 3, inner: StderrLogger };
  19. do_things(&l);
  20. }