Reading:  

What is Log4J. An absolute beginners tutorial.


Configuration

Configuration of Log4j 2 has below option:

  • Configuration through files written in JSON,YAML or XML.
  • Implementation of Configuraion Implementation and ConfigurationFactory.
  • Use APIs in Configuration interface.
  • Calling methods on internal Logger class.

Automatic Configuration:

At time of Installation the Log4j can automatically configure by default. It will check for Configuration Factory plugin and arrange based on order of highest to lowest. JSON,YAML and XML are the 3 configuration implementation :

  • Log4j checks for "log4j.configurationFile" system property or check for  ConfigurationFactory  matching file extension and load using ConfigurationFactory.
  • If file is not set YAML ConfigurationFactory will check for log4j2-config.yaml or log4j2- config.yml in classpath.
  • If file is not set JSON ConfigurationFactory will check for log4j2- config.json or log4j2- config.jsn in classpath.
  • If file is not set XML ConfigurationFactory will check for log4j2- config.xml or log4j2- config.jxml in classpath.
  • If configuration file cannot be found , DefaultConfiguration will be used.
import com.test.Draw;
// log4j classes import
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
 
public class MyTestApp {
// Define a static logger variable so that it references the // Logger instance named "MyTestApp". private static final Logger logger_new = LogManager.getLogger(MyTestApp.class); public static void main(final String... args) { logger_new.trace("Start of Application."); Draw d = new Draw(); if (!d.LocatePoint()) { logger_new.error("Didn't find it"); } logger_new.trace("Exiting application."); } }

 

MyTestApp will import log4j related classes. It define a static logger variable with same name as MytestApp.

Log4j provide a default configuration if it cannot find configuration file, DefaultConfiguration class will set following:

  • A root logger with attached ConsoleAppender.
  • A PatternLayout having pattern "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" attached to ConsoleAppender

The default Log4j will assign root logger to Level.ERROR.

17:15:01.540 [main] ERROR MyTestApp - Didn't find it.

The Default Configuration file in XML format is as below:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

 

Debug Level:

The options of DEBUG level are:

  • TRACE
  • ERROR
  • FATAL
  • ALL
  • DEBUG
  • INFO

Appenders:

Apache log4j has Appender objects primarily responsible for printing log messages to files,socket,consoles,event logs etc.

Property

Description

layout

Layout object helps formatting the logging information.

target

The target may be a file, a console

level

The level is used to control filteration of log messages.

threshold

The threshold level defines the appender logging level.

filter

The Filter objects analyze logging information based on level matching

XML format configuration as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders type="Console" name="STDOUT">
</Appenders>
<Appenders type="File" name="File" fileName="${filename}">
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

 

Layout:

The possible options of Layout Appender are:

  • DateLayout
  • HTMLLayout
  • PatternLayout
  • SimpleLayout
  • XMLLayout

Using XMLLayout andHTMLLayout can generate log in XML and HTML format as well.

 

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!