Log4j 1.2.x basic example

  • 10 February 2017
  • ADM

 

Log4j 1.2.x basic example - images/logos/java.jpg

 

This tutorial will present basic example of log4j 1.2.x usage.

Step 1 - create the project

Create the project. If you need help follow How to create Java Application with Eclipse for standard Java application or How to create an Application with Maven in Eclipse for a Maven project.

Step 2 - add the dependency

For a Maven project declares the following dependency:

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

For the non-Maven user, download the jar from the log4j official page and put it in the project library path manually.

Step 3 - configuration

Create the log4j.properties file.

# Root logger option - if you need more info go with DEBUG
log4j.rootLogger=INFO, file, stdout
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
 
log4j.appender.file.File=D:\\admfactory.com\\log.txt
log4j.appender.file.MaxFileSize=15MB
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}] [%-5p] %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%-5p] %c{1}:%L - %m%n

For more details check the log4j.properties basic examples tutorial.

The file location for Maven project is src/main/resources and for standard java application in the root of the src folder.

Step 4 - log a message

package com.admfactory.log;

import org.apache.log4j.Logger;

public class Log4jMessage {

    private static org.apache.log4j.Logger log = Logger.getLogger(Log4jMessage.class);

    public static void main(String[] args) {
	log.trace("Trace Message!");
	log.debug("Debug Message!");
	log.info("Info Message!");
	log.warn("Warn Message!");
	log.error("Error Message!");
	log.fatal("Fatal Message!");
    }
}

For more details on levels follow the Log4j Logging Levels tutorial.

Output

[2017-02-11 00:05:11] [INFO ] Log4jMessage:12 - Info Message!
[2017-02-11 00:05:11] [WARN ] Log4jMessage:13 - Warn Message!
[2017-02-11 00:05:11] [ERROR] Log4jMessage:14 - Error Message!
[2017-02-11 00:05:11] [FATAL] Log4jMessage:15 - Fatal Message!

Step 5 - log an exception

To generate an exception we can easily a substring from a null String variable.

package com.admfactory.log;

import org.apache.log4j.Logger;

public class Log4jException {

    private static org.apache.log4j.Logger log = Logger.getLogger(Log4jException.class);

    public static void main(String[] args) {
	try {
	    String str1 = null;
	    String str2 = str1.substring(2);
	} catch (Exception e) {
	    log.error("Something went wrong: ", e);
	}
    }
}

Output

[2017-02-11 00:12:56] [ERROR] Log4jException:14 - Something went wrong: 
java.lang.NullPointerException
	at com.admfactory.log.Log4jException.main(Log4jException.java:12)

 

References