Java log4j Tutorial - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts



Log4j is a Java library containing standard features of logging. In developing applications, generally, we use the console-based output (using System.out.println() ) to check the functioning of the application and debug the application. But in standard applications, the console-based output is never used for numerous reasons.

The reasons are listed below:

The output of System.out.println() is shown temporarily. The output is erased after closing the console.

System.out.println() is a single threaded process.

The huge volume of logs cannot be managed by System.out.println().

The output of log4j can be redirected to the console, a table of a database, files or email, etc.

Log4j provides a significant benefit by setting different levels of logging, like TRACE, DEBUG, INFO, WARN, ERROR, and FATAL. If a particular log level is set, logging will be performed for that level and only for the levels above it according to the hierarchy.

If the log level is set to ERROR, logging will be performed for errors and fatal only. If the log level is set to INFO, logging will be performed for infos, warns, errors, and fatals. Generally, the log level to DEBUG in the development phase, and the log level are set to INFO or higher when the application is deployed on the server. Moreover, using log4j, different log levels can be set to different class files.

Note that log levels can be changed without changing the source code of deployed applications if log4j is configured using the configuration file.

To start with log4j, you have to download the .jar file. You can download it from the Apache website. In this tutorial, we will discuss, how to configure log4j for Eclipse IDE.

Architecture

This API follows layered architecture, which makes the design easy, extendable, and flexible. The objects of this framework are categorized as core objects and support objects.

The core objects are Logger, Layout, and Appender.

The support objects are Level, Filter, ObjectRenderer, and LogManager.

log4j.properties

This is a configuration file containing a set of key-value pairs. All the logging-related configuration parameters are set in this file according to the user's requirements.

Happy Exploring!

No comments:

Post a Comment