log4j

log4j Hello World Example Hello World Example of log4j. This instantiates a log4j logger class and logs with ERROR level on the logger class.

To run this you need log4j.jar which can be downloaded from

http://logging.apache.org/log4j/1.2/download.html

And Add log4j jar to the Java Class Path ( Eclipse : Right Click on Project > Bulid Path > Add External Archives … )

package com.aayush.logging;

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

public class Intro1 {

public static Logger logger = Logger.getLogger(Intro1.class);

public static void main(String[] args) {
System.out.println("Hello World, Sorry I would be using log4j");
BasicConfigurator.configure();

logger.error("Hello log4j");

}

}

Using log4j.xml to add appenders to the logging configuration. You can see that three types of appenders are used

  • File Appender - org.apache.log4j.FileAppender
  • Rolling File Appender - org.apache.log4j.RollingFileAppender
  • Console Appender - org.apache.log4j.ConsoleAppender

As name specifies, the rolling file appender have features to rolling file based on certain criteria. In this following log4j.xml we setup has file roling based on 10KB and limit the Backup Index to 5.

Similarly we can use org.apache.log4j.DailyRollingFileAppender to roll logs on daily basis as well. Log4j has various appenders support, and you can also create your own appenders as well.

Besides that in RollingFileAppender, filter is also being used to filter LevelMin and LevelMax we want to filter in the appender.

package com.aayush.logging;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Intro1 {

	public static Logger logger = Logger.getLogger(Intro1.class);
	
	
	public static void main(String[] args) {
		System.out.println("Hello World, Sorry I would be using log4j");
		DOMConfigurator.configure("src/log4j.xml");
		for ( int i=0; i <= 1000; i++ )  {
			logger.error("Hello log4j");
			logger.debug("Hello log4j");
		}
		

	}

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
	<appender name="file" class="org.apache.log4j.FileAppender">
		<param name="file" value="log.out" />
		<param name="immediateFlush" value="true" />
		<param name="threshold" value="debug" />
		<param name="append" value="false" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
		</layout>
	</appender>

	<appender name="debugfile" class="org.apache.log4j.RollingFileAppender">
		<param name="maxFileSize" value="10KB" />
		<param name="maxBackupIndex" value="5" />
		<param name="File" value="debug.log" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
		</layout>
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="LevelMin" value="debug" />
			<param name="LevelMax" value="debug" />
		</filter>
	</appender>

		<appender name="console" class="org.apache.log4j.ConsoleAppender">
			<param name="Target" value="System.out" />
			<layout class="org.apache.log4j.PatternLayout">
				<param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
			</layout>
		</appender>

                <appender name="mail" class="org.apache.log4j.net.SMTPAppender">
	               <param name="SMTPHost" value="relay.aayushtuladhar.com" />
	               <param name="From" value="aayush.tuladhar@gmail.com	" />
	               <param name="To" value="aayush.tuladhar@gmail.com" />
	               <param name="Subject" value="[LOG] ..." />
	               <param name="BufferSize" value="1" />
	               <param name="threshold" value="error" />
	               <layout class="org.apache.log4j.PatternLayout">
		             <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
	               </layout>
              </appender>

		<root>
			<priority value="all"></priority>
			<appender-ref ref="console"/>
			<appender-ref ref="debugfile" />
			<appender-ref ref="file" />
			
		</root>
	</log4j:configuration>