15.4.5 Initializing log4j There are many properties (How to cite a web site) that

15.4.5 Initializing log4j There are many properties that can be configured for the log4j toolkit. In fact, log4j is so flexible that all the configuration options can’t be covered here. The best source of information is the log4j manual itself. You can find the manual online at http://jakarta.apache.org/log4j/docs/documentation.html, and it’s also available locally when you download log4j. Because log4j doesn’t make any assumptions about the environment in which it is running, you need to configure the environment for your particular needs. In other words, no default appenders are configured out of the box. There are various ways in which you can initialize the configuration properties for the log4j environment. We will focus on two related, but distinct, approaches here. 15.4.5.1 Initializing using the log4j.properties file The first approach is to create a file called log4j.properties that contains the necessary configuration elements for your logging needs. This file must follow the guidelines of the java.util.Properties format. One of these guidelines is that each property is in the format key=value. Example 15-5 illustrates a very simple log4j configuration file that logs messages with a logging threshold of INFO to the console using an org.apache.log4j.ConsoleAppender. Example 15-5. An example log4j.properties file # A basic log4j configuration file that creates a singleconsole appender # Create a single console appender that logs INFO and higherlog4j.rootLogger=INFO, stdout # Configure the stdout appender to go to the consolelog4j.appender.stdout=org.apache.log4j.ConsoleAppender # Configure the stdout appender to use the PatternLayoutlog4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller’s filename and line number. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) -%m%n The configuration file shown in Example 15-5 is a very simple example of setting up a single appender in this case, the ConsoleAppender, which directs log messages to System.out. This log4j.properties file must be installed in the WEB-INF/classes directory so that the log4j environment will be able to locate it and use it to configure the logging environment for the web application. If you have multiple web applications, you can have a separate log4j.properties file for each.
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Leave a Reply