Reading:  

What is Log4J. An absolute beginners tutorial.


Log Formatting

The layout objects in Apache log4j allows formatting logging data based various layouts. Based on application specific way layout object can formats logging data.

The Layout Types:

Org.apache.log4j.Layout is the base class for different type of layout class in log4j API. The different Layout supported by log4j:

  • DateLayout
  • HTMLLayout
  • PatternLayout
  • SeralizedLayout
  • XMLLayout
  • Syslog

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<File name="MyFile" fileName=" Applicationdemo.log ">
<PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="MyFile " level="warn"/>
</root>
</loggers>
</configuration>

Example program

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
 
public class Log4jConfigurationExample
{
    static final Logger logger_c = LogManager.getLogger(Log4jConfigurationExample.class.getName());
    public static void main(String[] args)
    {
        Log4jConfigurationExampleobj = new Log4jConfigurationExample();
           obj. ExecuteRun(“test”);
    }

	private void ExecuteRun (String parameter){
		if(logger_c.isDebugEnabled()){
			logger_c.debug("Message debug : " + parameter);
		}

		if(logger_c.isInfoEnabled()){
			logger_c.info("Message info : " + parameter);
		}

		logger_c.warn("Message warn : " + parameter);
		logger_c.error("Message error : " + parameter);
		logger_c.fatal("Message fatal : " + parameter);

	}
}

 Above program has layout set to PatternLayout along with Configuration set to display the date and time of error or warning reported by the system

2015-03-01 19:00:05 DEBUG Log4jConfigurationExample:19 - This is debug : test
2015-03-01 19:00:05 INFO  Log4jConfigurationExample:23 - This is info : test
2015-03-01 19:00:05 WARN  Log4jConfigurationExample:26 - This is warn : test
2015-03-01 19:00:05 ERROR Log4jConfigurationExample:27 - This is error : test
2015-03-01 19:00:05 FATAL Log4jConfigurationExample:28 - This is fatal : test

 

Description

This tutorial is aimed at learners who want to get an understaind on what Log4J is and what it is used for. This tutorial have 10 part to it as shown below

  1. Overview    
  2. How to Install Log4J
  3. Understanding Architecture
  4. Getting started with Log4J Configuration
  5. Sample Program
  6. Logging Methods
  7. Understanding different Logging Levels
  8. Log4J log formatting
  9. How to log in files
  10. How to log in database using Log4J

We hope that this tutorial will help you out with Log4J. Please note that this tutorial is for absolute starters and does not go into much depth. Let us know how we can improve by sending  your feedback.



Prerequisites

A good understanding of Java programming language is required.

Audience

Absolute beginners

Author: Subject Coach
Added on: 7th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!