Introduction to logging in Java

Logging is the process of writing log messages during the execution of a program. In other words, it is done with a motive to track the flow of application by writing different informative/error/warning/developer-friendly messages maybe on the console, a file or a database. Logging is done so that in future, if we are notified of any bug in production, we can go through the log files of that particular date time, analyse the application flow with the user input received, to finally identify the bug.

A well-written logging code offers quick debugging to identify the bug and helps in easy maintenance.

Log Levels:

There are 5 standard log levels:

  1. DEBUG – This level is mainly used to log messages that are to be used only while debugging the application. Debug messages are those that is rarely turned-on in the production environment.
  2. INFO – This level is basically used to display general messages which developer intentionally wants to display (not for testing but just to track the normal flow of the application). For e.g. messages like “Entering into X method” or “Exiting the X method”. This gives a clear idea of where the control of the program is flowing in normal scenario while trailing the logs.
  3. WARN – This level gives a warning message about an unwanted event to the developer but it may not halt the progress of the running flow.
  4. ERROR – This level is used to display an error messages in unexpected event. For e.g. Say, I have written a method which inserts data in Database. Now if the query execution fails with an exception, I will write an error message using this level in catch block to notify the error. Again this may not halt the execution of program.
  5. FATAL – This level indicates severe error which needs to be addressed by the programmer to avoid undesired results.

Log Level Priorities:

  1. When DEBUG level is set, it displays all other level messages along with Debug logs.
  2. When INFO level is set, the program will display INFO level messages. Along with INFO, it also displays messages with WARN, ERROR and FATAL. But DEBUG level messages are not displayed as INFO level denotes normal flow.
  3. Likewise, When WARN level is set, program will show only messages with WARN, ERROR and FATAL. So, the hierarchy goes as shown in below order for all levels – DEBUG, INFO, WARN, ERROR, FATAL

The beauty of this is that if we set the log level to WARN, then INFO and DEBUG messages will have no performance impact. If you need to get additional information from a production system you just can lower the level to INFO or DEBUG for a short period of time. If the INFO or DEBUG levels kept set for long, the additional logs generated at INFO or DEBUG level would make the log files bigger and harder to read. Adjusting log levels can normally be done at runtime.

Setting the Log Level:

Log4j is a popular and widely used logging framework in java. So we will understand how this Log4j works.

Configuring Log4j:

Let’s see how we can configure log4j and use different logging levels in our java program!!

Just follow the below steps –

  1. Download Log4j JAR from –
  2. Include the JAR file to the build path of your project.

You are now ready to go.

Using BasicConfigurator.configure() method will log all the messages on the console.

When we run the above program, we will be getting the following output:

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.