Logger getLevel() method in Java with Examples

The getLevel() method of the Logger class in Java is used to get the log Level that has been specified for this Logger instance. Every Logger has specific log levels and if the result is null, which means that this logger’s effective level will be inherited from its parent.

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. To know more about Log Levels, refer this Log Levels in Logging

Syntax:



public Level getLevel()

Parameters: This method accepts no parameters.

Return value: This method returns Level which represents level of logger.

Below programs illustrate the getLevel() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Logger.getLevel() method
  
import java.util.logging.Logger;
import java.util.logging.Level;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a Logger
        Logger logger
            = Logger.getLogger(
                GFG.class.getName());
  
        // Get level of logger
        Level level
            = logger.getLevel();
  
        // If logger level is null
        // then take a level of the parent of logger
  
        if (level == null && logger.getParent() != null) {
            level = logger.getParent().getLevel();
        }
  
        System.out.println("Logger Level = " + level);
    }
}

chevron_right


Output:

Logger Level = INFO

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Logger.getLevel() method
  
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a Logger
        Logger logger
            = Logger.getLogger(
                ArrayList.class.getName());
  
        // Get level of logger
        Level level = logger.getLevel();
  
        System.out.println("Logger Level = "
                           + level);
    }
}

chevron_right


Output:

Logger Level = null

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/logging/Logger.html#getLevel()



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.