How to configure log4j in eclipse - Java @ Desk

Saturday, June 8, 2013

How to configure log4j in eclipse

Log4J Implementation in Eclipse:
There are three implementations for configuration of log4j in eclipse.

Required files for the implementation are:
1) log4j.jar
2) log4j.properties
3) mylog4j.properties

Create a new Java project and name it as LoggerImpl
Create log4j.properties file inside src folder

# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, theConsoleAppender

# settings for the console appender
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Create package com.logger and create Log4JImplementation.java

package com.logger;

import org.apache.log4j.Logger;

public class Log4JImplementation {
 static Logger log = Logger.getLogger(Log4JImplementation.class);

 public static void main(String[] args) {
  log.debug("This is a debug message");
  log.info("This is an info message");
 }
}

Output:

0    [main] INFO  com.logger.Log4JImplementation  - This is an info message

As you can see, only the info message is visible in console but the debug message is not printed. This is because of the debug level set to INFO in log4j.properties file. Change the properties as shown below:

log4j.rootLogger=DEBUG, theConsoleAppender

Output:

0    [main] DEBUG com.logger.Log4JImplementation  - This is a debug message
0    [main] INFO  com.logger.Log4JImplementation  - This is an info message

Other references on log4j implementation in eclipse
How to configure log4j in eclipse
Configure log4j in eclipse using PropertyConfigurator class
Configure log4j in eclipse without using log4j.properties file







No comments:

Post a Comment