FileAppender

FileAppender

If we want to use FileAppender in Java applications then we have to use the following properties in log4j.properties file.

Syntax

log4j.rootLogger
log4j.appender.Ref_Name
log4j.appender.Ref_Name.File : It will name and location of the file to manage logging messages.
log4j.appender.Ref_Name.layout
log4j.appender.Ref_Name.layout.ConversionProperty

E.g

log4j.rootLogger = ALL, FILE
log4j.appender.FILE = org.apache.log4j.FileAppender
log4j.appender.FILE.File=/home/ashok/logging/ashok.log
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern = %d{yyyy-mm-dd hh:mm:ss } : This is %m%n
package com.ashok.log4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class TestLog4J {
	private static final Logger LOG = LoggerFactory.getLogger(TestLog4J.class);
	public static void main(String[] args) {
		LOG.trace("This is trace message"); 
		LOG.debug("This is debug message");
		LOG.info("This is info message"); 
		LOG.warn("This is warn message"); 
		LOG.error("This is error message"); 
	}
}

If we want to use both ConsoleAppender and FileAppender in Java Applications then we have to define two Ref_Names at ‘log4j.rootLogger’ property.

E.g

log4j.properties

log4j.rootLogger =ALL, CONSOLE, FILE

ConsoleAppender Configuration

og4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern = %d{yyyy-mm-dd hh:mm:ss } %-c %p : This is %m%n

FileAppender Configuration

log4j.appender.FILE = org.apache.log4j.FileAppender
log4j.appender.FILE.File=/home/ashok/logging/ashok.log
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern = %d{yyyy-mm-dd hh:mm:ss } %-c %p : This is %m%n
package com.ashok.log4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class TestLog4J {
	private static final Logger LOG = LoggerFactory.getLogger(TestLog4J.class);
	public static void main(String[] args) {
		LOG.trace("This is trace message"); 
		LOG.debug("This is debug message");
		LOG.info("This is info message"); 
		LOG.warn("This is warn message"); 
		LOG.error("This is error message"); 
	}
}

FileAppender


Scroll to top