Saturday, January 11, 2025
Google search engine
HomeLanguagesJavaLogger getFilter() Method in Java with Examples

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:




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

RELATED ARTICLES

Most Popular

Recent Comments