Logging to multiple appenders (OR LOG FILES)

How to add Logs in File by using Log4j

In this tutorial we will discuss how we can have multiple appenders, in other words how we can log the message into more than one appenders like file and console . In the example given below, all logs with a level INFO or higher will be logged to both the console and a file named application.log.


How to add Logs in File by using Log4j

In this tutorial we will discuss how we can have multiple appenders, in other words how we can log the message into more than one appenders like file and console . In the example given below, all logs with a level INFO or higher will be logged to both the console and a file named application.log.


log4j.properties file

log4j.rootLogger=INFO, CA, FA

# Console appender configuration
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%r [%t] %5p (%F:%L) - %m%n

# File appender configuration
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=D:/log/application.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%r [%t] %5p (%F:%L) - %m%n

You can also set the logger level for each appender separately.Since we didn't set any log level explicitly for both the appenders it will inherit the rootLogger level, that is INFO which we declared in the properties file.


Create Java Class to test log message

package com.jwt.log;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

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

public class Log4jDemo {

	static final Logger logger = Logger.getLogger(Log4jDemo.class);
	static final String LOG_PROPERTIES_FILE = "log4j.properties";

	public static void main(String[] args) {
		new Log4jDemo();
		// sample info log message
		logger.info("leaving the main method of Log4JDemo");
	}

	public Log4jDemo() {
		initializeLogger();
		// sample info log message
		logger.info("Log4jDemo - leaving the constructor ...");
	}

	private void initializeLogger() {
		Properties logProperties = new Properties();

		try {
			// load log4j properties configuration file
			logProperties.load(new FileInputStream(LOG_PROPERTIES_FILE));
			PropertyConfigurator.configure(logProperties);
			logger.info("Logging initialized.");
		} catch (IOException e) {
			logger.error("Unable to load logging property :", e);
		}
		try {
			FileInputStream fstream = new FileInputStream("D:\\textfile.txt");
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				System.out.println(strLine);
			}
			in.close();
		} catch (FileNotFoundException fe) {
			logger.error("File Not Found", fe);
			logger.warn("This is a warning message");
			logger.trace("This message will not be logged since log level is set as DEBUG");
		} catch (IOException e) {
			logger.error("IOEXception occured:", e);
		}
	}
}


After executing the program, application.log file will be generated inside "D:/log" folder at the same time logs will be displayed on the console with the following entry.

0 [main]  INFO (Log4jDemo.java:38) - Logging initialized.
8 [main] ERROR (Log4jDemo.java:52) - File Not Found
java.io.FileNotFoundException: D:\textfile.txt (The system cannot find the file specified)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.(FileInputStream.java:106)
	at java.io.FileInputStream.(FileInputStream.java:66)
	at com.jwt.log.Log4jDemo.initializeLogger(Log4jDemo.java:43)
	at com.jwt.log.Log4jDemo.(Log4jDemo.java:26)
	at com.jwt.log.Log4jDemo.main(Log4jDemo.java:20)
12 [main]  WARN (Log4jDemo.java:53) - This is a warning message
12 [main]  INFO (Log4jDemo.java:28) - Log4jDemo - leaving the constructor ...
12 [main]  INFO (Log4jDemo.java:22) - leaving the main method of Log4JDemo





comments powered by Disqus