Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

7.12. Depending on Apache Log4J

7.12.1. Problem

You need to use Apache Log4J to print statements to a log file for informational or debugging purposes.

7.12.2. Solution

To use Apache Log4J 1.2.15 in a Maven 2 project, add the following dependency to your project's pom.xml:

Example 7.8. Adding a Dependency on Apache Log4J

<dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.15</version>
      <exclusions>
        <exclusion>
          <groupId>javax.jms</groupId>
          <artifactId>jms</artifactId>
        </exclusion>
        <exclusion>
          <groupId>com.sun.jdmk</groupId>
          <artifactId>jmxtools</artifactId>
        </exclusion>
        <exclusion>
          <groupId>com.sun.jmx</groupId>
          <artifactId>jmxri</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

7.12.3. Discussion

Apache Log4J (formerly known as Jakarta Log4J) is a highly configurable logging framework providing hierarchical loggers, various log destinations, and log formats. Messages are written to Log4J Logger objects, which represent a specific category in a hierarchy of log categories; for example, the com.discursive.Blah category is a child of the com.discursive category. All messages sent to a child category are sent to each ancestor in a tree of Logger categories. A category can be assigned an Appender and a Layout; an Appender controls where a message is sent, and a Layout defines the formatting and contents of a message. Log4J ships with a number of Appender implementations, including SMTPAppender, RollingFileAppender, SocketAppender, SyslogAppender, and NTEventLogAppender. Log4J also ships with a number of Layout implementations, including XMLLayout, PatternLayout, HTMLLayout, and DateLayout.

7.12.4. See Also

For more information about the Apache Logging Services project, see the Logging Services project page at http://logging.apache.org. For more information about the Apache Log4J project, see the Log4J project page at http://logging.apache.org/log4j/docs/.


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.