Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaLogger getLevel() method in Java with Examples

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:




// 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()

RELATED ARTICLES

Most Popular

Recent Comments