In our application, logging is a vital functionality that we will import when creating project. It makes us to detect bugs, problems in logging file when program runs at runtime mode.

So, in this article, we will learn how to configure log4j 1.x framework in our project.

Let’s get started.

Table of contents

The structure of Log4j

Below is an image that describe Log4j 1.x’s architecture. It’s very important because understanding its architecture will help us to configure correctly.

  • Logger Object

    It is topmost level layer of Log4j 1.x. It is used to contain logging information to write some places that we want.

  • Appender Object

    It is a lower-level layer of Log4j 1.x. It delivers the Appender objects which is responsible for publishing the logging information to the several 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.

  • Layout Object

    This layer is used to formatting all our logging information what we want.

  • Level Object

    • FATAL: shows messages at a FATAL level only.

    • ERROR: Shows messages classified as ERROR and FATAL.

    • WARNING: Shows messages classified as WARNING, ERROR, and FATAL.

    • INFO: Shows messages classified as INFO, WARNING, ERROR, and FATAL.

    • DEBUG: Shows messages classified as DEBUG, INFO, WARNING, ERROR, and FATAL.

    • TRACE : Shows messages classified as TRACE,DEBUG, INFO, WARNING, ERROR, and FATAL.

    • ALL : Shows messages classified as TRACE,DEBUG, INFO, WARNING, ERROR, and FATAL.

    • OFF : No log messages display.

    In the log4j framework, the following is order of the levels.


  • Log Manager

    LogManager is responsible for managing all information in by reading it at the first time.

Some packages that need for Log4j 1.x

Below is some packages of Log4j 1.x that we need to install. All of them is put into pom.xml file in Maven project.



Configure Log4j 1.x with properties file

Below is an file that we need to configure when utilizing Log4j 1.x.

# The following properties set the logging levels and log appender.
# The log4j.rootCategory variable defines the default log level and one or more appenders.
# For the console, use 'S'.  For the daily rolling file, use 'R'.
# For an HTML formatted log, use 'H'.
# Possible Log Levels : FATAL, ERROR, WARN, INFO, DEBUG
log4j.rootCategory = INFO, S

# Define the root logger with logging's level and appender files: file, console
log4j.rootLogger=INFO, file, console

# Define the file appender

# Set the append to false, overwrite

# Set the name of the file

# Set the maximum file size before rollover

# Set the the backup index


# Define the layout for file appender
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n

# Define encoding for output file

# Configure logger with name = "com.manhpd". It is also associated with appenders: file, console, console, file

log4j.logger.nameLogger=nameLoggingLevel, appender_1, appender_2

Then, it is an test file for using file.

public class App 
    private static Logger logger = LogManager.getLogger(App.class);

    public static void main( String[] args ) throws InterruptedException {
        for(int i = 0; i < 2000; i++) {
  "This is the " + i + " time I say 'Hello World'.");

Wrapping up

  • Understanding configurations of Log4j 1.x such as all parts of Log4j, functionality of each part, …
  • Configure xml or properties file easily.
  • In order to use Log4j 1.x in Spring boot, we can do like this link.


Mark number line in log file