We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 18 August 2013

Log4j – logging in File using Property Configuration


In this tutorials we are going to see how to create simple log4j application to logging message into file using Property Configurator with java program.org.apache.log4j.FileAppender used to log the message in file, configuration available in FileAppender is listed below.

Property
Description
immediatlush Default value of immediate flush is true, which means that output stream of the file being flushed with each append operation.
encoding Default value of encoding is platform specific encoding.we can use any possible character-encoding.
threshold Threshold level for appender.We can use any one of level
filename The name of output file
fileAppend Default value of fileAppend is true, which means logging message being append in end of file
bufferedIO Default value of bufferedIO is false.The bufferedIO used to enable or disable buffered writing.
bufferSize Default value of bufferSize is 8kb.If bufferedIO enabled means we can set buffer size.

Create java project called Log4jExample in your Eclipse IDE, then create package called com.javatutorialscorner.log4j.fileappender under Log4jExample,add log4j jar in your build path.I used log4j-1.2.17.jar jar for this application.you can download this jar from Apache web site.Create log4j.properties in your project directory or source directory, add the following configuration
log4j.properties
#The root logger with appender file
logFile = C:/log
log4j.rootLogger = DEBUG, FILE

#The file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${logFile}/log.log

#The layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%r [%t] %p %c %x - %m%n

In this configuration “logFile”  is the path of log output, in which the out put file will be created in C:/log/log.log, “log.log” is Name of the file.

rootLogger used to configure Level and Appender, in this example I used DEBUG as Level and FileAppender is appender(FILE is name of appender).PatternLayout is used to configure log4j message format.

Now create java class called Log4jFileAppenderExample under com.javatutorialscorner.log4j.fileappender package.

Log4jFileAppenderExample.java

package com.javatutorialscorner.log4j.fileappender;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4jFileAppenderExample {
private static final String FILE_PATH = "log4j.properties";
private static final Logger log = Logger
.getLogger(Log4jFileAppenderExample.class);

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if (FILE_PATH == null) {
BasicConfigurator.configure();
log.info("Log4j Configured using Basic Configurator");
} else {
PropertyConfigurator.configure(FILE_PATH);
log.info("Log4j Configured using Property Configurator");
}

log.debug("Debug message by Java Tutorials Corner");
log.info("Info message by Java Tutorials Corner");
log.warn("Warn message by Java Tutorials Corner");
log.error("Error message by Java Tutorials Corner");
log.fatal("Fatal message by Java Tutorials Corner");
}

}

Create instance for Logger class , getLogger(Log4jFileAppenderExample.class) method accept fully qualified class name as its argument. In this example if property file not exist in specified location then BasicConfigurator.configure() used to initialize log4j application otherwise  PropertyConfigurator.configure(FILE_PATH)  loads the configuration from property file . Basic configurator shows the log in console.

now you can run the application, you will see the following output file in specified location, which contains following output.

0 [main] INFO com.javatutorialscorner.log4j.fileappender.Log4jFileAppenderExample  - Log4j Configured using Property Configurator

0 [main] DEBUG com.javatutorialscorner.log4j.fileappender.Log4jFileAppenderExample  - Debug message by Java Tutorials Corner


0 [main] INFO com.javatutorialscorner.log4j.fileappender.Log4jFileAppenderExample  - Info message by Java Tutorials Corner


0 [main] WARN com.javatutorialscorner.log4j.fileappender.Log4jFileAppenderExample  - Warn message by Java Tutorials Corner


0 [main] ERROR com.javatutorialscorner.log4j.fileappender.Log4jFileAppenderExample  - Error message by Java Tutorials Corner


0 [main] FATAL com.javatutorialscorner.log4j.fileappender.Log4jFileAppenderExample  - Fatal message by Java Tutorials Corner


Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Log4j – logging in File using Property Configuration Rating: 5 Reviewed By: eHowToNow