Logging is a very important part of your application. You can be the best developer and never write a bug, but a service you depend on may go down, or the framework you're using may have an issue, so you need to be able to diagnose it without attaching a debugger to your production server. But if you're not testing that your logging works, you won't be sure until one of these disaster scenarios occur whether it's actually hooked in correctly, as well as not being able to catch if someone accidentally removes a logging line in a refactor.
A very popular logging library in the Java ecosystem is [SLF4J](https://www.slf4j.org/), and we'll look at how we can test we've set things up correctly.
The repository for this article can be found at [<i class="fa fa-gitlab"></i> jamietanna/slf4j-testing](https://gitlab.com/jamietanna/slf4j-testing), and the examples are based on [davidxxx's response on Stack Overflow](https://stackoverflow.com/a/52229629).
Let's say that we have this example class that does some processing of data, as well as logging:
LOGGER.debug("The boolean passed in has value {}",logErrors);
// do some stuff
LOGGER.info("this is logging something else");
// do some other stuff
}
}
```
To test that these logs are send correctly, we're able to hook into the underlying Logback code and get a `ListAppender` object, which contains all the log events for a given `Logger`. Note that it's best to this into separate utility class so we can re-use the code around the project, instead of having these lines duplicated across test classes.
.tuple("The boolean passed in has value {}","The boolean passed in has value false",
Level.DEBUG));
}
}
```
Notice that when we are using a format string we have two checks on the log message - `getMessage` returns the raw message (including format placeholders), but `getFormattedMessage` returns the expanded log message.