Skip to content
Related Articles

Related Articles

Improve Article

Logger getLevel() method in Java with Examples

  • Last Updated : 19 Mar, 2019

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:




// 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);
    }
}
Output:
Logger Level = INFO

Program 2:




// 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);
    }
}
Output:
Logger Level = null

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :