Skip to content
Related Articles

Related Articles

Logging in Java

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 21 May, 2018
Improve Article
Save Article

Java provides the ability to capture the log files.

The need for Log capture
There are multiple reasons why we may need to capture the application activity.

  • Recording unusual circumstances or errors that may be happening in the program
  • Getting the info about whats going in the application

The details which can be obtained from the logs can vary. Sometimes, we may want a lot of details regarding the issue, or sometimes some light information only.
Like when the application is under development and is undergoing testing phase, we may need to capture a lot of details.

Log Levels
The log levels control the logging details. They determine the extent to which depth the log files are generated. Each level is associated with a numeric value and there are 7 basic log levels and 2 special ones.
We need to specify the desired level of logging every time, we seek to interact with the log system. The basic logging levels are:

LevelValueUsed for
SEVERE1000Indicates some serious failure
WARNING900Potential Problem
INFO800General Info
CONFIG700Configuration Info
FINE500General developer info
FINER400Detailed developer info
FINEST300Specialized Developer Info

Severe occurs when something terrible has occurred and the application cannot continue further. Ex like database unavailable, out of memory.

Warning may occur whenever the user has given wrong input or credentials.
Info is for the use of administrators or advanced users. It denotes mostly the actions that have lead to a change in state for the application.
Configuration Information may be like what CPU the application is running on, how much is the disk and memory space.

Fine Finer and Finest provide tracing information. When what is happening/ has happened in our application.

FINE displays the most important messages out of these.

FINER outputs a detailed tracing message and may include logging calls regarding method entering, exiting, throwing exceptions.

FINEST provides highly detailed tracing message.

Furthermore, there are two special Logging levels

OFFInteger.MAX_VALUECapturing nothing
ALLInteger.MIN_VALUECapturing Everything

Capturing everything may mean every field declaration, definition, every method call, every assignment performed etc.

Java’s Log System
The log system is centrally managed. There is only one application wide log manager which manages both the configuration of the log system and the objects that do the actual logging.
The Log Manager Class provides a single global instance to interact with log files. It has a static method which is named getLogManager

Logger Class
The logger class provides methods for logging. Since LogManager is the one doing actual logging, its instances are accessed using the LogManager‘s getLogger method.
The global logger instance is accessed through Logger class’ static field GLOBAL_LOGGER_NAME. It is provided as a convenience for making casual use of the Logging package.

// Java program to illustrate logging in Java
// The following code shows a basic example how logging 
// works in Java
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.*;
class DemoLogger {
    private final static Logger LOGGER = 
    // Get the Logger from the log manager which corresponds 
    // to the given name <Logger.GLOBAL_LOGGER_NAME here>
    // static so that it is linked to the class and not to
    // a particular log instance because Log Manage is universal
    public void makeSomeLog()
        // add some code of your choice here
        // Moving to the logging part now
        LOGGER.log(Level.INFO, "My first Log Message");
        // A log of INFO level with the message "My First Log Message"
public class GfG {
    public static void main(String[] args)
        DemoLogger obj = new DemoLogger();
        // Generating some log messages through the 
        // function defined above
        LogManager lgmngr = LogManager.getLogManager();
        // lgmngr now contains a reference to the log manager.
        Logger log = lgmngr.getLogger(Logger.GLOBAL_LOGGER_NAME);
        // Getting the global application level logger 
        // from the Java Log Manager
        log.log(Level.INFO, "This is a log message");
        // Create a log message to be displayed
        // The message has a level of Info


May 12, 2018 7:56:33 AM DemoLogger makeSomeLog
INFO: My first Log Message
May 12, 2018 7:56:33 AM GfG main
INFO: This is a log message

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!