Configuring and Using Log4j
Most problems I’ve ever had when using log4j are related to bad configuration, usually stemming from a basic lack of understanding of how the various components of log4j work together. The core concepts of log4j are not well understood by the average user, or there wouldn’t be as many configuration problems as there are. In this section, we will take a look at the core concepts of the log4j architecture, along with an example for each, taken from the Example Application used with the Short Cut.
Each concept will be presented with an example so the reader can see how it works right out of the gate. Simple usage patterns are presented here for Appender usage and where to place the configuration file and how to name it.
Let’s spend a minute looking at how the most basic components of log4j fit together. Figure 3 illustrates this.
Figure 3. Basic log4j components
Below is a short description of each component in Figure 3. We will look at each of these in more detail in later sections.
This is a named entity—where you control the name—with which you interact directly from your Java code to do logging.
This represents a destination for logging output such as a file, the console, or even a database. Log4j comes packaged with a number of appenders that represent the most common log destinations you will need, but extensible so that if we have ...