Logger getFilter() Method in Java with Examples

The getFilter() method of the Logger class is used to get the current filter for this Logger instance. A Filter is useful to filter out log messages. we can say that filter decide the message gets logged or not. Filters are represented by the Java interface java.util.logging.Filter

Syntax:

public Filter getFilter()

Parameters: This method accepts do not accepts any parameter.



Return value: This method returns the current filter for this Logger.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Logger.getFilter() 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("com.core");
  
        // set a new filter
        logger.setFilter(new Filter() {
            @Override
            public boolean isLoggable(LogRecord record)
            {
                return true;
            }
        });
  
        // get Filter
        Filter filter = logger.getFilter();
  
        // check filter is null or not by printing
        System.out.println("Filter = " + filter);
    }
}

chevron_right


Output:
The output printed on eclipse IDE is shown below-

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Logger.getFilter() 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("com.javacode.core");
  
        // set a new filter
        logger.setFilter(new MyFilter());
  
        // get Filter
        Filter filter = logger.getFilter();
  
        // check filter is null or not by printing
        System.out.println("Filter = " + filter);
    }
}
class MyFilter implements Filter {
    public boolean isLoggable(LogRecord record)
    {
        return false;
    }
}

chevron_right


Output:
The output printed on eclipse IDE is shown below-

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



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.