Logger setFilter() method in Java with Examples

setFilter() method of a Logger class is used to set a filter to control output on this Logger. The filter is passed as a parameter. A Filter is useful to filter out log messages. It can be said that the filter decides the message gets to be logged or not. Filters are represented by the Java interface java.util.logging.Filter. After passing the initial “level” check, the Logger will call this Filter to check if a log record should really be published.

Syntax:

public void setFilter(Filter newFilter)
               throws SecurityException

Parameters: This method accepts one parameter newFilter which represents a filter object.



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 setFilter() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Logger.setFilter() method
  
import java.util.logging.*;
import java.io.IOException;
  
public class GFG {
  
    public static void main(String[] args)
        throws SecurityException, IOException
    {
  
        // Create a logger
        Logger logger
            = Logger.getLogger(GFG.class.getName());
  
        // set a new filter
        logger.setFilter(new MyFilter());
  
        // check filter is null or not by printing
        System.out.println("Filter = "
                           + logger.getFilter());
    }
}
class MyFilter implements Filter {
    public boolean isLoggable(LogRecord record)
    {
        return false;
    }
}

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.setFilter() method
  
import java.util.logging.*;
import java.io.IOException;
  
public class GFG {
  
    public static void main(String[] args)
        throws SecurityException, IOException
    {
  
        // Create a logger
        Logger logger
            = Logger.getLogger(
                GFG.class.getName());
  
        // Set a new filter
        logger.setFilter(new Filter() {
            @Override
            public boolean isLoggable(LogRecord record)
            {
                return true;
            }
        });
  
        // Check filter is null
        // or not by printing
        System.out.println("Filter = "
                           + logger.getFilter());
    }
}

chevron_right


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

References: https://docs.oracle.com/javase/10/docs/api/java/util/logging/Logger.html#setFilter(java.util.logging.Filter)



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.