LogRecord setLevel() method in Java with Examples

The setLevel() method of java.util.logging.LogRecord is used to set the level of logging message, for example Level.INFO for this LogRecord Object.

Syntax:

public void setLevel(Level level)

Parameters: This method accepts level which is the logging message level.

Return: This method returns nothing.

Below programs illustrate setLevel() method:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate setLevel() method
  
import java.util.logging.Level;
import java.util.logging.LogRecord;
  
import java.util.logging.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create LogRecord object
        LogRecord logRecord = new LogRecord(
            Level.parse("800"),
            "Hi Logger");
  
        System.out.println(
            "Previous Level: "
            + logRecord.getLevel().getName());
  
        // set level of the LogRecord
        logRecord.setLevel(Level.FINEST);
  
        // print result
        System.out.println(
            "New Level: "
            + logRecord.getLevel().getName());
    }
}

chevron_right


Output:

Previous Level: INFO
New Level: FINEST

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate setLevel() method
  
import java.util.logging.Level;
import java.util.logging.LogRecord;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create LogRecord object
        LogRecord logRecord = new LogRecord(
            Level.parse("400"),
            "GFG Logger");
  
        System.out.println(
            "Previous Level: "
            + logRecord.getLevel().getName());
  
        // set level of the LogRecord
        logRecord.setLevel(Level.WARNING);
  
        // print result
        System.out.println(
            "New Level: "
            + logRecord.getLevel().getName());
    }
}

chevron_right


Output:

Previous Level: FINER
New Level: WARNING

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

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.