19 June 2014

Using log4j


Log4j is one of the utility library to log the messages. Add “log4j-xxx.jar” and “log4j.xml” into your classpath.

For more references Log4j.

LoggerTest.java
package org.metadata.core.logger;

import org.apache.log4j.Logger;

public class LoggerTest {
      private Logger logger = Logger.getLogger(LoggerTest.class);

      public void test() {
            logger.debug("This is debug Message.");
            logger.info("This is info Message.");
            logger.warn("This is warn Message.");
            logger.error("This is error Message.");
            logger.fatal("This is fatal Message.");
      }

      public static void main(String[] args) {
            LoggerTest loggerTest = new LoggerTest();
            loggerTest.test();
      }
}

log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <param name="Threshold" value="DEBUG"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] %m%n"/>
        </layout>
    </appender>
    <appender name="APPLICATION" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="D:/application.log"/>
        <param name="MaxBackupIndex" value="10"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS} |%5p| [%c] | %m%n"/>
        </layout>
    </appender>
    <logger name="org.metadata.core">
        <level value="DEBUG"/>
        <appender-ref ref="CONSOLE"/>
    </logger>
</log4j:configuration>

Log Level

In log4j.xml file, as the log level is “DEBUG”, program will print out the all of the messages. If log level is “WARN”, program will print WARN, ERROR and FATAL messages. Log level order is as below.

DEBUG
The DEBUG Level designates fine-grained informational events that are most useful to debug an application.
INFO
The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
WARN
The WARN level designates potentially harmful situations.
ERROR
The ERROR level designates error events that might still allow the application to continue running.
FATAL
The FATAL level designates very severe error events that will presumably lead the application to abort.

Output in Console
16:39:48,455 DEBUG [org.metadata.core.logger.LoggerTest] This is debug Message.
16:39:48,455 INFO  [org.metadata.core.logger.LoggerTest] This is info Message.
16:39:48,455 WARN  [org.metadata.core.logger.LoggerTest] This is warn Message.
16:39:48,455 ERROR [org.metadata.core.logger.LoggerTest] This is error Message.
16:39:48,455 FATAL [org.metadata.core.logger.LoggerTest] This is fatal Message.

Printing the log message into the file. Change “appender-ref” in log4j.xml file. According to the following configuration, program will print out the message into “D:/application.log” file.

<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <param name="Threshold" value="DEBUG"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] %m%n"/>
        </layout>
    </appender>
    <appender name="APPLICATION" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="D:/application.log"/>
        <param name="MaxBackupIndex" value="10"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS} |%5p| [%c] | %m%n"/>
        </layout>
    </appender>
    <logger name="org.metadata.core">
        <level value="DEBUG"/>
        <appender-ref ref="APPLICATION"/>
    </logger>
</log4j:configuration>

Output in application.log
16:39:48,455 DEBUG [org.metadata.core.logger.LoggerTest] This is debug Message.
16:39:48,455 INFO  [org.metadata.core.logger.LoggerTest] This is info Message.
16:39:48,455 WARN  [org.metadata.core.logger.LoggerTest] This is warn Message.
16:39:48,455 ERROR [org.metadata.core.logger.LoggerTest] This is error Message.
16:39:48,455 FATAL [org.metadata.core.logger.LoggerTest] This is fatal Message.

No comments:

Post a Comment

Like us on Facebook