Logger setLevel() method in Java with Examples

setLevel() method of a Logger class used to set the log level to describe which message levels will be logged by this logger. The level we want to set is passed as a parameter. Message levels lower than passed log level value will be discarded by the logger. The level value Level.OFF can be used to turn off logging.

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 void setLevel(Level newLevel)
              throws SecurityException

Parameters: This method accepts one parameter newLevel which represents the new value for the log level.

Return value: This method returns nothing.

Exception: This method throws SecurityException if a security manager exists, this logger is not anonymous, and the caller does not have LoggingPermission(“control”).

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Logger.setLevel() method
  
import java.util.logging.*;
  
public class GFG {
  
    public static void main(String[] args)
        throws SecurityException
    {
  
        // Create a logger
        Logger logger
            = Logger.getLogger(
                GFG.class.getName());
  
        // Set log levels
        logger.setLevel(Level.FINEST);
  
        // Print log level
        System.out.println("Log Level = "
                           + logger.getLevel());
    }
}

chevron_right


Output:
The output printed on console of Eclipse is shown below-

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Logger.setLevel() method
  
import java.util.logging.*;
  
public class GFG {
  
    public static void main(String[] args)
        throws SecurityException
    {
  
        // Create a logger
        Logger logger
            = Logger.getLogger(
                GFG.class.getName());
  
        // Set log levels
        logger.setLevel(Level.WARNING);
  
        // Print log level
        System.out.println("Log Level = "
                           + logger.getLevel());
    }
}

chevron_right


Output:
The output printed on console output is shown below-

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



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.