• September 9, 2014
  • Log4j
  • Comments Off on Log4j example.

Log4j example.

Let us discuss the use of Log4j with the help of below example. In this example we use BasicConfigurator for Log4j configurations. BasicConfigurator use ConsoleAppender and PatternLayout for all loggers.

Example:

Log4jTest:

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
 
/**
 * This class is used to show the use of 
 * Log4j with the BasicConfigurator.
 * @author javawithease
 */
public class Log4jTest {
	//Get the Logger object.
	private static Logger log = Logger.getLogger(Log4jTest.class);
 
	public static void main(String[] args) {
		//Configuring Log4j,It will log all messages on console.
		//BasicConfigurator use ConsoleAppender and PatternLayout 
		//for all loggers.
		BasicConfigurator.configure();
 
		//logger messages
		log.debug("Log4j debug message test.");
		log.info("Log4j info message test.");		
	}
}

Output:

0 [main] DEBUG com.javawithease.business.Log4jTest 
 - Log4j debug message test.
1 [main] INFO com.javawithease.business.Log4jTest 
 - Log4j info message test.

Download this example.
 
Next Topic: Log4j example using log4j properties file.
Previous Topic: Logger class.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 


Comments are closed.