Java Logging

The log files can be captured using Java. We may require the application activity to be captured for a variety of reasons.

  1. Keeping track of any unusual events or potential programme errors
  2. Obtaining information about what is happening in the application
  3. It is also used by web servers to store information about website visits. The IP address of each visitor, the time of the visit, and the pages accessed are normally included in this data.

For example, when the application is being developed and tested, we might need to record a lot of information.

Logging Levels

A log message’s importance can be determined by its log level, also known as log severity. It is a straightforward method of separating log events from one another that is also very effective. All you need to do is look at the severity first if your application uses log levels properly.

  • Severe – This level indicates that something dreadful has occurred and need an immediate action. It can be database unavailability  , out of memory , etc.
  • Warning – The user may receive a warning if they provide incorrect credentials or input.
  • Info – It is intended for advanced users or administrators. It primarily refers to the actions that have caused the application’s state to change.
  • Config – It include things like the type of CPU the application is using, the size of the disc, and the amount of memory.
  • Fine – It highlights most significant messages.
  • Finer – It outputs detail tracing messages
  • Finest – It offers extremely thorough tracing messages.

In addition to this, there are 2 more logging levels

  • OFF – It has a value of Integer.MAX_VALUE and used for capturing nothing
  • ALL – It has a value of Interger.MIN_VALUE and used for capturing everything

Java’s Log System

There is only one application-wide log manager, and it is in charge of both the configuration of the log system and the objects that actually perform the logging. A single global instance is provided by the Log Manager Class for use in communicating with log files. It contains the static method getLogManager.


Java Code

import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.logging.*;

public class solution {
  public static void main(String[] args) {
    Logger1 obj = new Logger1();

    // lgmngr contains a reference to the log manager.
    LogManager lgmngr = LogManager.getLogManager();
    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

class Logger1 {
  private final static Logger LOGGER =

  // To ensure that the Logger is linked to the class and not a specific log instance 
  //because Log Manage is global, obtain the Logger from the log manager that 
  //corresponds to the given name "Logger.GLOBAL LOGGER NAME here" static
  public void CreateLog() {
    // A log of INFO level with the message " First Log Message"
    LOGGER.log(Level.INFO, "First Log Message");



Aug 01, 2022 4:05:39 PM Logger1 CreateLog
INFO: First Log Message
Aug 01, 2022 4:05:39 PM solution main
INFO: This is a log message

Special thanks to Shreyas Vishwakarma for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article