log4j is a very popular logging library for Java development. It provides convenient and flexible logging mechanism as well as fast performance.
Putting log4j into your code is pretty easy
There are two ways to use log4j
1. LOG4J.PROPERTIES
#This is the first line of your log4j.properties, where you have to mention the level of #logging you want (ALL,DEBUG,INFO,WARN,ERROR,FATAL) then you have to mention the #appenders name. Appender is where you want to print your logs (on console or in a log #file)
log4j.rootLogger=DEBUG,Appender1, Appender2
#Defination of Appenders - no need to memorize the classname - you can get it from #log4j jar.
# A) mention the name of appender (ConsoleAppender or FileAppender)
# B) mention the appender layout (PatternLayout)
# C) mention the appender format (p=level d=date t=thread c=class x=package #m=message n=newline)
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=bikesh.log
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
package com.test;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Test {
final static Logger logger=Logger.getLogger(Test.class);
public static void main(String args[])
{
PropertyConfigurator.configure("log4j.properties");
logger.debug("This is debug");
logger.info("This is info");
logger.error("This is error");
logger.warn("This is Warn");
logger.fatal("This is fatal");
}
}
OUTPUT
DEBUG 2018-02-20 01:30:09,901 [main] com.test.Test - This is debug
INFO 2018-02-20 01:30:09,901 [main] com.test.Test - This is info
ERROR 2018-02-20 01:30:09,901 [main] com.test.Test - This is error
WARN 2018-02-20 01:30:09,901 [main] com.test.Test - This is Warn
FATAL 2018-02-20 01:30:09,901 [main] com.test.Test - This is fatal
2.LOG4J.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="appender1" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n"/>
</layout>
</appender>
<appender name="appender2" class="org.apache.log4j.FileAppender">
<param name="File" value="applog.txt" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n"/>
</layout>
</appender>
<root>
<priority value="DEBUG"/>
<appender-ref ref="appender1" />
<appender-ref ref="appender2" />
</root>
</log4j:configuration>
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
public class Test {
final static Logger logger=Logger.getLogger(Test.class);
public static void main(String[] args) {
DOMConfigurator.configure("log4j.xml");
logger.debug("This is debug");
logger.info("This is info");
logger.error("This is error");
logger.warn("This is Warn");
logger.fatal("This is fatal");
}
}
levels of Logging can be : ALL, DEBUG, INFO, WARN, ERROR, FATAL, and OFF.
1. Example
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Test {
static final Logger logger=Logger.getLogger(Test.class);
public static void main(String args[]){
BasicConfigurator.configure();
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
}
}
0 [main] DEBUG Test - Sample debug message
0 [main] INFO Test - Sample info message
0 [main] WARN Test - Sample warn message
0 [main] ERROR Test - Sample error message
0 [main] FATAL Test - Sample fatal message
Above example is only for basic knowledge.
Typically, configuring log4j involves following tasks:
1. Configure the root logger
log4j.rootLogger=DEBUG, ConsoleAppender, FileAppender
log4j.rootLogger=level[, appender1, appender2…]
2.Configure individual loggers
log4j.logger.com.test.Database=INFO, ConsoleAppender
log4j.logger.<logger-name>=level[, appender1, appender2…]
3.Specify appenders
Appedners specifies how messages are logged.
log4j.appender.<appender_name>=<appender_class_name>
log4j.appender.<appender_name>.<property1_name>=<property1_value>
log4j.appender.<appender_name>.<property2_name>=<property2_value>
4. Specify the message format
log4j.appender.<appender_name>.layout=<layout_class_name>
log4j.appender.<appender_name>.layout.ConversionPattern=<conversion_pattern>
Each conversion specifier must follow this format:
<percent sign (%)>[format modifiers]<conversion characters>
There are two ways to use log4j
1. LOG4J.PROPERTIES
#This is the first line of your log4j.properties, where you have to mention the level of #logging you want (ALL,DEBUG,INFO,WARN,ERROR,FATAL) then you have to mention the #appenders name. Appender is where you want to print your logs (on console or in a log #file)
log4j.rootLogger=DEBUG,Appender1, Appender2
#Defination of Appenders - no need to memorize the classname - you can get it from #log4j jar.
# A) mention the name of appender (ConsoleAppender or FileAppender)
# B) mention the appender layout (PatternLayout)
# C) mention the appender format (p=level d=date t=thread c=class x=package #m=message n=newline)
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=bikesh.log
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
package com.test;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Test {
final static Logger logger=Logger.getLogger(Test.class);
public static void main(String args[])
{
PropertyConfigurator.configure("log4j.properties");
logger.debug("This is debug");
logger.info("This is info");
logger.error("This is error");
logger.warn("This is Warn");
logger.fatal("This is fatal");
}
}
OUTPUT
DEBUG 2018-02-20 01:30:09,901 [main] com.test.Test - This is debug
INFO 2018-02-20 01:30:09,901 [main] com.test.Test - This is info
ERROR 2018-02-20 01:30:09,901 [main] com.test.Test - This is error
WARN 2018-02-20 01:30:09,901 [main] com.test.Test - This is Warn
FATAL 2018-02-20 01:30:09,901 [main] com.test.Test - This is fatal
2.LOG4J.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="appender1" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n"/>
</layout>
</appender>
<appender name="appender2" class="org.apache.log4j.FileAppender">
<param name="File" value="applog.txt" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n"/>
</layout>
</appender>
<root>
<priority value="DEBUG"/>
<appender-ref ref="appender1" />
<appender-ref ref="appender2" />
</root>
</log4j:configuration>
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
public class Test {
final static Logger logger=Logger.getLogger(Test.class);
public static void main(String[] args) {
DOMConfigurator.configure("log4j.xml");
logger.debug("This is debug");
logger.info("This is info");
logger.error("This is error");
logger.warn("This is Warn");
logger.fatal("This is fatal");
}
}
OUTPUT
DEBUG 2018-02-20 02:07:00,655 [main] Test - This is debug
INFO 2018-02-20 02:07:00,655 [main] Test - This is info
ERROR 2018-02-20 02:07:00,655 [main] Test - This is error
WARN 2018-02-20 02:07:00,655 [main] Test - This is Warn
FATAL 2018-02-20 02:07:00,655 [main] Test - This is fatal
No comments:
Post a Comment