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.

Log4J Concepts

Let’s spend a minute looking at how the most basic components of log4j fit together. Figure 3 illustrates this.

Basic log4j components

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.

logger

This is a named entity—where you control the name—with which you interact directly from your Java code to do logging.

Appender

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 ...

Get Log4J now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.