7 Advanced Log4j
This chapter covers
- Creating a simple application with logging
- Filtering log messages based on intent
- Creating message classes for reusing logging code
- Making use of the ThreadContext for adding contextual information
- Using the fluent logging API
In this chapter, we will build a simple application that uses a similar structure to a real application. Within this codebase, we will implement some advanced logging features. That way, logging is not theoretical, and you can adapt the ideas into your own projects.
I will not explain all the details of the application, but the nuts and bolts: How the general behavior works so that you can understand the logging behaviors. For the rest, I will add comments in the code repository.
The application is a simple command-line system. It lets us add and remove people from the passenger list. In addition, we will be able to see whether this flight is fully booked.
7.1 The Airplane App
To run this application, please start it using your favorite editor or by executing this command in the shell:
mvn exec:java -Dexec.mainClass="de.grobmeier.manning.log4j.basic.AirplaneBasicApp"
You’ll find several versions of the code in this book’s code repository. I want to encourage you to run the code and specifically play around with the configurations I provided as we go through the chapter.