Home / Java Techs / Java / Simple steps to log messages in Java programs using Log4j

Simple steps to log messages in Java programs using Log4j

Print Friendly, PDF & Email

This article explains a bit of the importance of application logging and log4j API in Java. The following paragraphs will give you an overview of how logging mechanism works in Java application and how use one of the common logging APIs to log messages.

The importance of logging

Logging is a vital part in the majority of applications. It enables to record most of events as well as what are going on your application that save a lot hours of developers, system administrators, especially the support team. To implement logging mechanism properly, you need to find the answer of some common questions in developing the application which are what to log, when to log, how much to log, how to control logging and how correlate it with your exception system (see more in [1]).

What does log4j API do?

Like I said, Log4J is one of Java based logging APIs that helps you recording likely messages in your program.

Example 1: we create a class named SimpleStatistics.java

To declare a log instance, we can use one of the following syntax:

Then, we use the following statements to record informative, debugging or warning message.

What does commons-logging framework do?

Commons-logging is an abstraction layer for logging frameworks so it doesn’t log anything itself.

Example 2: we get back the example introduced above and modified a bit

Output

Regardless of ways you use to log messages, the output could look like the following images

From the console

log1

log2

From the log file

The logs.log file is created automatically once you run the application at first. We will discuss on how to customise the log configuration to generate log messages in a favourite form later on in this article.

log4

Customise the logs file

By default, a file named logs.log is created in the logs folder. As you see in the above screenshot, we have two files: logs.log and running.log. The former records messages coming from different log points (i.e. classes using logging API). In the toy application, I create two classes in the same package. The logging messages of both classes have been saved in the logs.log file under the pattern.

However, log API enables us customise a lot of things either prettifying the messages or avoiding lost of messages due to over capacity of the logs file. It creates a default file named log4j.properties (if you use log4j API) or commons-logging.properties  (if you use commons-logging framework) in the src/resources  folder. The following is the basic settings created at the first run.

log4j.properties

commons-logging.properties

Commonly modified configurations in log properties file are often interested in

  • Enable/disable log message out the standard output
  • Rename the logs file and replace the location where the logs file is stored
  • Change the maximum size of the logs file
  • Use full package name rather than class name
  • ….

Source code

I pushed my work to bitbucket in case you want to review.

References

[1] Application Logging: What, When, and How

[2] commons-logging logfactory example

[3] https://examples.javacodegeeks.com/enterprise-java/log4j/log4j-properties-example/

[4] https://www.mkyong.com/logging/log4j-log4j-properties-examples/

comments

About Admin

programming all time and taking a bread for the lunch hahaha… When you are engaging your work, nobody can attract you doing another thing. Otherwise, should should move to other jobs or are not interested in your job enough.