Log4j configurators

Configurators

Now that we know how the three main components of Log4J are used, we can take a look at how these components are configured for actual use. Log4J can be configured programmatically or by using configuration files. For our examples, we use configuration files.

Log4J currently supports two types of configuration files: a regular Java properties file and an XML properties file. We will use a Java properties file for our examples. A Log4J properties file can be divided into three sections. Each section maps to a Category, an Appender, or a Layout. The following code snippet contains a sample properties file.

## CATEGORIES ## #define a category named chapter11.Log4JApp log4j.category.chapter11.Log4JApp=WARN, console, file ## APPENDERS ## # define an appender named console, which is set to be a ConsoleAppender log4j.appender.console=org.apache.log4j.ConsoleAppender # define an appender named file, which is set to be a RollingFileAppender log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=log.txt ## LAYOUTS ## # assign a layout to both appenders log4j.appender.console.layout=org.apache.log4j.SimpleLayout log4j.appender.file.layout=org.apache.log4j.SimpleLayout

The best way to examine this file is to start from the top with the CATEGORIES section. In this section, we define a new Category named chapter11.Log4JApp.

In its definition, we are setting the log Priority to WARN. We are also assigning two Appenders to this Category, console and file, which are both defined in the APPENDERS section.

The next section is the APPENDERS section, which defines the available Appenders for this configuration. Here, we define two Appenders: console and file. The console Appender uses the ConsoleAppender class that we spoke of earlier, which simply logs all messages to the application console.

The file Appender uses the RollingFileAppender, which appends all of the log messages to a log file named by the log4j.appender.file.File property. The final section of this file is the LAYOUTS section.

In this section, we are directing both the console and file Appenders to use the SimpleLayout, which is represented by org.apache.log4j.SimpleLayout. In summary, this configuration file defines a Category named chapter11.Log4JApp that logs all messages with a Priority that is greater than or equal to WARN to both the console and a file named log.txt in a format similar to the following:

DEBUG - This is the log message!

To load this configuration, you would use the static PropertyConfigurator.configure() method with the path to the Log4J properties file. The following line of code gives an example of this. PropertyConfigurator.configure(propfile);

Using Log4J in an Application

Before we continue, we would like to mention that we are Java professionals with 8+ years of experience in Java web Hosting. If you would like a stable VPS Hosting with Java and great support to back you up, consider what we have to offer!

Now that we have a basic understanding of Log4J and its components, let's actually look at how it can be used.

We are going to do this by creating a standard Java application that loads a Log4J properties file and uses the defined categories to log its messages.

There is really nothing special about this file: it starts by loading two categories cat and childcat, with the first being the parent, referencing the chapter11.Log4JApp Category definition, and the second referencing chapter11.Log4JApp.child Category definition, which is the child of the first.

After these two categories are loaded, we begin making log requests to them. For each Category, we are calling all of the priority-driven logging methods, passing them a simple message with the category name appended to the end.

The result is a log file named log.txt that contains all of the log messages with a priority of WARN or greater. An example of this file after a single execution can be found in the following snippet:

  • WARN - This is a log message from the chapter11.Log4JApp
  • ERROR - This is a log message from the chapter11.Log4JApp
  • FATAL - This is a log message from the chapter11.Log4JApp
  • WARN - This is a log message from the chapter11.Log4JApp.child
  • ERROR - This is a log message from the chapter11.Log4JApp child
  • FATAL - This is a log message from the chapter11.Log4JApp.child

As you look over this file, you should note that the childcat did in fact inherit the cat's Priority and Appender

Contact sales!