Skip to content
Related Articles

Related Articles

Improve Article

Logger getFilter() Method in Java with Examples

  • Last Updated : 20 Mar, 2019
Geek Week

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :