Log4j Property File

Put below log4j.properties file in default package

Jar Source from Apache : Log4j Jar

#
# log4j.properties
#
#--------ABOUT LOG4J-------------
#1. After Defining Class write this Statement[final Logger logger = Logger.getLogger(Demo.class);]
#2. Levels of logging DEBUG-INFO-WARN-ERROR-FATAL.
#3. System.out.println("hello") means logger.info("hello").
#4. For error in catch use logger.error(e)[Exception e]
#---------END--------------------
#------PROPERTY DEFINE-------------
log4j.rootLogger=WARN, W1, W2
log4j.rootLogger=INFO, I1, I2
log4j.rootLogger=ERROR, E1, E2
log4j.rootLogger=DEBUG, D1, D2
log4j.rootLogger=TRACE, T1, T2
#-----------END-------------------

#-----------FILE APPENDER--------------
log4j.appender.W1=org.apache.log4j.RollingFileAppender
log4j.appender.W1.File=C:/logs/log.log
log4j.appender.W1.MaxFileSize=2MB
log4j.appender.W1.MaxBackupIndex=2
log4j.appender.W1.layout=org.apache.log4j.PatternLayout
log4j.appender.W1.layout.ConversionPattern=%d{dd/MMM/yyyy HH:mm:ss} %p %c - %m%n
#
log4j.appender.I1=org.apache.log4j.RollingFileAppender
log4j.appender.I1.File=C:/logs/log.log
log4j.appender.I1.MaxFileSize=2MB
log4j.appender.I1.MaxBackupIndex=2
log4j.appender.I1.layout=org.apache.log4j.PatternLayout
log4j.appender.I1.layout.ConversionPattern=%d{dd/MMM/yyyy HH:mm:ss} %p %c - %m%n
#
log4j.appender.E1=org.apache.log4j.RollingFileAppender
log4j.appender.E1.File=C:/logs/log.log
log4j.appender.E1.MaxFileSize=2MB
log4j.appender.E1.MaxBackupIndex=2
log4j.appender.E1.layout=org.apache.log4j.PatternLayout
log4j.appender.E1.layout.ConversionPattern=%d{dd/MMM/yyyy HH:mm:ss} %p %c - %m%n
#
log4j.appender.D1=org.apache.log4j.RollingFileAppender
log4j.appender.D1.File=C:/logs/log.log
log4j.appender.D1.MaxFileSize=2MB
log4j.appender.D1.MaxBackupIndex=2
log4j.appender.D1.layout=org.apache.log4j.PatternLayout
log4j.appender.D1.layout.ConversionPattern=%d{dd/MMM/yyyy HH:mm:ss} %p %c - %m%n
#
log4j.appender.T1=org.apache.log4j.RollingFileAppender
log4j.appender.T1.File=C:/logs/log.log
log4j.appender.T1.MaxFileSize=2MB
log4j.appender.T1.MaxBackupIndex=2
log4j.appender.T1.layout=org.apache.log4j.PatternLayout
log4j.appender.T1.layout.ConversionPattern=%d{dd/MMM/yyyy HH:mm:ss} %p %c - %m%n
#---------------END---------------------#

#-------------CONSOLE-------------------#
log4j.appender.W2=org.apache.log4j.ConsoleAppender
log4j.appender.W2.layout=org.apache.log4j.PatternLayout
log4j.appender.W2.layout.ConversionPattern=%d{dd/MMM/yyyy HH:mm:ss} %p %c - %m%n
#
log4j.appender.I2=org.apache.log4j.ConsoleAppender
log4j.appender.I2.layout=org.apache.log4j.PatternLayout
log4j.appender.I2.layout.ConversionPattern=%d{dd/MMM/yyyy HH:mm:ss} %p %c - %m%n
#
log4j.appender.E2=org.apache.log4j.ConsoleAppender
log4j.appender.E2.layout=org.apache.log4j.PatternLayout
log4j.appender.E2.layout.ConversionPattern=%d{dd/MMM/yyyy HH:mm:ss} %p %c - %m%n
#
log4j.appender.D2=org.apache.log4j.ConsoleAppender
log4j.appender.D2.layout=org.apache.log4j.PatternLayout
log4j.appender.D2.layout.ConversionPattern=%d{dd/MMM/yyyy HH:mm:ss} %p %c - %m%n
#
log4j.appender.T2=org.apache.log4j.ConsoleAppender
log4j.appender.T2.layout=org.apache.log4j.PatternLayout
log4j.appender.T2.layout.ConversionPattern=%d{dd/MMM/yyyy HH:mm:ss} %p %c - %m%n
#-------------END----------------------#

Sample Main Class…


package log4jdemo;

import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
import org.apache.log4j.spi.LoggerFactory;

public class Main {
static final Logger logger = Logger.getLogger(Main.class);

public void call(String name) {
NDC.push(name);
logger.debug("Main method starts here...");
try {
int b = 100;
logger.info("Ans : " + b);
Integer a = 5 / 0;
} catch (Exception e) {
logger.error(""+e);
}
logger.trace(NDC.pop());
}

public static void main(String[] args) {
Main main = new Main();
main.call("taher");
}
}
Advertisements
This entry was posted in Log4j. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s