Open In App

Logger getFilter() Method in Java with Examples

Last Updated : 20 Mar, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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);
    }
}


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

Program 2:




// 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;
    }
}


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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads