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
Table of Contents
- Log4j is a Java library that specializes in logging.
- log4j is a reliable, fast and flexible logging framework (APIs) written in Java.
- Log4j distributed under the Apache Software License
- log4j is a framework to help the programmer output log statements to a variety of output targets.
- With log4j it is possible to enable logging at runtime without modifying the application binary.
- Typically, you can configure Log4j via a configuration file. This means that you can change your logging configuration without requiring code updates. If you need to do something like set the log level to DEBUG for your deployed application in order to track down a particular bug, you can do this without redeploying your application. Instead, you can change your log configuration file, reread the configuration file, and your logging configuration gets updated.
Log4j Vs Stdout
- At its most basic level, you can think of it as a replacement for System.out.println’s in your code.
Why is it better than System.out.println’s?
- To begin with, System.out.println outputs to standard output, which typically is a console window. The output from Log4j can go to the console, but it can also go to an email server, a database table, a log file, or various other destinations.
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
- Logger is the the top-level layer which provides the Logger object
- The Logger object is responsible for capturing logging information.
private static final Logger logger = Logger.getLogger(Test.class);
- The Layout layer provides objects which are used to format logging information in different styles
- Layout objects play an important role in publishing logging information in a way that is human-readable and reusable.
You will get more details on Layout from the official site Log4j Layouts
- This is a lower-level layer which provides Appender objects.
- The Appender object is responsible for publishing logging information to various preferred destinations such as a database, file, console, UNIX etc..
For more details please visit official site Log4j Appenders
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.
TRACE > DEBUG > INFO > WARN > ERROR > FATAL.
- This log4j level helps developer to debug application.
- Level of message logged will be focused on providing support to a application developer.
- This log4j level gives the progress and chosen state information.
- This level will be generally useful for end user. This level is one level higher than DEBUG.
- This log4j level gives a warning about an unexpected event to the user.
- The messages coming out of this level may not halt the progress of the system.
- The ERROR level designates error events that might still allow the application to continue running.
- This level is one level higher than WARN.
- The FATAL level designates very severe error events that will presumably lead the application to abort.
- Once you get this level and it indicates application death.
- This log4j level is used to turn on ALL levels of logging.
- Once this is configured and the levels are not considered.
- This log4j level is opposite to ALL level.
- It turns off all the logging.
Log4j Level Table
|Will Output Messages Log4j Levels|