Site icon Narayana Tutorial

Log4j Brief Introduction


In this tutorial, we are going to explaining Log4j brief introduction, why log4j, log4j package software, log4j components and log4j level. We are sharing Log4j examples to understand log4j framework easily.

Log4j Brief Introduction

Why Log4j

Log4j Vs Stdout

Why is it better than System.out.println’s?

Log4j Software

Log4j API package is distributed under the Apache Software License, a full-fledged open source license certified by the open source initiative.

The latest log4j version, including full-source code, class files and documentation can be found at http://logging.apache.org/log4j

 

Log4j logger contains three main components namely logger, appender and layout

Log4j Components

Logger

Syntax

private static final Logger logger = Logger.getLogger(Test.class);

Layout

Examples

  1. PatternLayout
  2. CSV
  3. HTML
  4. XML
  5. JSON

You will get more details on Layout from the official site Log4j Layouts

Appender

Example

  1. ConsoleAppender
  2. FileAppender
  3. FailoverAppender
  4. FlumeAppender

For more details please visit official site Log4j Appenders

 

Log4j Level

One of the distinctive features of log4j is the notion of hierarchical loggers. Using loggers it is possible to selectively control which log statements are output at arbitrary granularity.

log4j is designed with three goals in mind: reliability, speed and flexibility. There is a tight balance between these requirements. We believe that log4j strikes the right balance.

Log4j provides five standard levels of logging

log4j allows you to create custom levels.

Another great benefit of Log4j is that different levels of logging can be set. The levels are hierarchical and are as follows: TRACE, DEBUG, INFO, WARN, ERROR, and FATAL.

Hierarchical representation

TRACE > DEBUG > INFO > WARN > ERROR > FATAL.

DEBUG Level

INFO Level

WARN Level

ERROR Level

FATAL Level

ALL Level

OFF Level

Log4j Level Table

Will Output Messages Log4j Levels
Log4j Levels TRACE DEBUG INFO WARN ERROR FATAL
TRACE Y Y Y Y Y Y
DEBUG N Y Y Y Y Y
INFO N N Y Y Y Y
WARN N N N Y Y Y
ERROR N N N N Y Y
FATAL N N N N N Y
ALL Y Y Y Y Y Y
OFF N N N N N N

Graphical Representation

Log4j Level

Reference Links: