Java ProgrammingLearn about log4j Key Components & Its Installation In Java

Learn about log4j Key Components & Its Installation In Java

JAVA is one of the most extensively used programming language. Java programmers require a mechanism to log the application operation details in order to understand its overall processing and health at any point of time. Such a logging mechanism can be implemented into the Java-based application code through the log4j logging framework. Log4j logging mechanism has also ported to the C, C++, C#, Python, Ruby, Perl, and Eiffel languages. It is a consistent, flexible and easy to implement logging framework which itself is coded in the Java language. Its distribution is under the Apache Software License.

Overview and key components of log4j framework
Log4j framework is used in the Java/J2EE applications as well as applications written in the other programming languages like C, C++, C#, Ruby, Perl, Python, and Eiffel languages. It is a configurable framework at runtime through the configuration files. The log priorities levels are set in the configuration file, and based on these priorities the application logs are generated and written to the desired destination such as a plain file on a machine`s hard disk, a database (i.e. Oracle, MySQL, etc.), and a log management system (LMS), a console, UNIX filesystem, etc. for viewing purpose.

Log4j has the following three key object components.
• Loggers: Logger objects are responsible for capturing logging information.
• Appenders: Appender objects are responsible for publishing the logging information to the desired destinations such as a plain file on a hard disk, a database (i.e. Oracle, MySQL, etc.), and a log management system (LMS), a console, UNIX filesystem, etc.
• Layouts: Layout objects are responsible for the formatting of the logging information into different layouts or styles.

Log4j Logging Mechanism Features
• Log4j logging mechanism is always thread-safe and hence it can be used in the heavy multithreaded systems.
• Log4j logging mechanism is known to be fail-stop.
• Log4j logging mechanism has no restrictions to a predefined set of facilities in log4j.
• Log4j logging mechanism can support multiple output Appenders per logger in the application code.
• Log4j logging mechanism extends it support for internationalization.
• Log4j logging mechanism has a robust design which can effectively handle Java Exceptions from the beginning.
• Log4j logging mechanism has the Appender interface through which logs can be written to the desired destination such as a plain file on a machine or hard disk, a database (i.e. Oracle, MySQL, etc.), and a log management system (LMS), a console, UNIX filesystem, etc. for the view purpose.
• Log4j logging mechanism has optimized operating speed logging mechanism.
• Log4j logging mechanism is based on a named logger hierarchy.
• Log4j logging mechanism possesses different levels of priorities such as ALL, TRACE, DEBUG, INFO, WARN, ERROR and FATAL for the view process of logs.
• Log4j logging mechanism permits changes to the log output format through the extension of the Layout class.
• Log4j logging mechanism permits changes to the logging behaviour at run time through the modification of the properties in the configuration file.

Pros and cons of Logging using log4j
• The Logging mechanism is a must for the Software development as it assists developers in rapid debugging, smooth maintenance, analyzing, and well organized storage of an application’s runtime information.
• The Logging mechanism should not generate verbose logs as it will slow down the operating speed of your application and the overall performance. Also, the logs verbosity should not result into scrolling blindness. In the Log4j design, these concerns are alleviated which makes it is reliable, fast and extensible API.

Log4j installation and Integration
In this article, we will use eclipse and the maven plugin in order to build a Java based application using log4j logging mechanism. These are the following steps.
Step 1: – Download the maven plugin [apache-maven-3.5.0] from the following link. Unzip the downloaded package under the path [C:\work\app].

Maven Plugin Download Link

Step 2: – Set up the following windows operating system environment variables as M2_REPO, PATH M2_HOME, and MAVEN_OPTS as shown below.

S No

Variable Name

Variable value












-Xms256m -Xmx512m

Step 3: – Open a new Maven Project in eclipse and navigate in eclipse as New  others… Maven Project as shown below.
Maven Project in eclipse
Step 4: – In this step, you need to enter the group id, artifact id and version field as the Archetype parameters are required for a maven project as shown below.
Update Details in Maven Projects
Step 5: – Open pom.xml file for the maven project ‘log4j_logging_demo’, which you just created and add the maven repository URL for log4j and the target path for the file as shown below.

<project xmlns="" xmlns:xsi=""





Step 6: – The Maven tool will automatically download the required APIs for the log4j logging mechanism and set the project build path for the Java/J2EE application as shown below.

log4j logging mechanism view

At this point, the installation of the log4j logging mechanism has completed and you are ready to implement it in your Java/J2EE application.

Conclusion: –
In this article, we discussed the Log4j logging mechanism overview, features, and its pros and cons for an application logging. Further on, we demonstrated various steps to install Log4j using Maven environment on the windows machine and used eclipse to start developing and building a Java application using Maven as a project build tool.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -