What is logging?

  • Tracing program execution during development
  • Debugging
  • Providing an audit trail for a program
  • Recording soft-errors (lost connections, etc.) for monitoring performance and troubleshooting
  • A logging framework lets you use different logging levels

Adding logging code

  • Replaces System.out.println() calls throughout
  • Utility of logging code is improved if it’s consistent—logs can be searched.
  • Five recognized message priorities: DEBUG, INFO, WARN, ERROR & FATAL

Installing Log4J

  • Download a zip or compressed tar file from Apache.org website
  • Uncompress to a local directory
  • In your Eclipse project’s properties, Locate Java Build Path – On Libraries page, click on Add External Jars
  • Browse for your Log4J directory, and locate the log4J jar file in the /dist/lib directory
  • Click Open, followed by OK

Creating a Log4J configuration file

The easiest way to configure Log4J is to add a log4j.properties file to your source directory. There are many options available which we won’t cover here, but essentially you need to define:

  • A logger
  • An appender
  • A pattern layout

Log4j Set-Up Process

The following example uses the default root logger, appends to the console and prints the date, time, message priority, thread and message

Step 1 – Download the Log4j library

If you are using Maven 2 for your project, you do not need to download the binary. You can simply declare it as a dependency in your Maven 2 pom.xml and Maven will grab the library for you. Declare it as follows

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

Step 2 – Import the jar file into your web project

If your using Maven 2, you can skip this step; after declaring the dependency, Maven will put the jar file in the proper place automatically when you build.

Import the log4j jar file into your JavaBuild Path

Download from here: Downloadlog4j1217jar

Step 3 – Create Log4j Properties file

In Eclipse, right click on the src folder

Select New > Others > File

Create file with name log4j.properties and add below property settings

# Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log
log4j.appender.file.File=C:\log\applog.log
#log4j.appender.file.File=C:\Program Files\Apache Software Foundation\Tomcat 7.0\geologs\applog.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} UTC %-5p %c{1}:%L - %m%n

Step 5 – Put logging code in your classes

The last thing you need to do is drop some logging code in a class and test this whole setup.

Add the following to the imports section of your java code:

import org.apache.log4j.Logger;
static Logger log = Logger.getLogger(MyClassName.class);

Throw some logging statements in your code where you know they’ll be fired right away when you run your app. For example:

log.trace("Hello");
log.debug("Hello");
log.info("Hello");
log.error("Hello");
log.warn("Hello");
log.fatal("Hello");

Step 6 – Run your app and make sure it works

Finally, run your app and make sure it works. You should see log lines in your file where you setup your appender.