Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

How to set up a logging filter in Java

Abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

A filter as the name suggests, is used to filter out the log messages. Every log message is passed through the filter, and the filter decides which messages get logged. Hence, it is important to configure filters for loggers in Java.

The setFilter() method

The setFilter() method is used to set a custom logging filter. A custom logging filter can be defined by implementing the java.util.logging.Filter interface. The interface consists of one method called isLoggable() where every log record/message is passed to this method. The method returns a boolean value that indicates whether the log record should be published.

Syntax

logger.setFilter(Filter customFilter);
boolean isLoggable(LogRecord record);

Parameters

  • customFilter: This is the custom filter that needs to be set on the logger.

  • record:This is the log record.

Return value

The isLoggable() method returns true if the log record has to be published. Otherwise, it returns false.

Code example

Let’s look at the code below:

import java.util.logging.Filter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
Logger logger = Logger.getLogger(Main.class.getName());
logger.setFilter(new CustomFilter());
logger.info("This is an info message");
logger.warning("This is a warning message");
}
}
class CustomFilter implements Filter {
public boolean isLoggable(LogRecord logRecord) {
return logRecord.getLevel().getName().equals("INFO");
}
}

Note: Replace the INFO keyword with WARNING in line 17 to display the warning level log.

Code explanation

  • Lines 1-3: Relevant classes and packages are imported.
  • Line 8: A logger is created with the name of the Main class.
  • Line 9: We set the filter of the logger to our custom filter via the setFilter() method. An instance of CustomFilter class is passed as the parameter.
  • Line 10: An INFO level log message is sent to the logger.
  • Line 11: A WARNING level log message is sent to the logger.
  • Lines 15-19: A custom filter by the name CustomFilter is defined here which implements the Filter interface. The isLoggable() method is overridden here that only returns true for INFO level log messages and false for all other level log messages.

Note: The key observation here is that only the INFO level log gets printed to the console while the WARNING level log is ignored.

RELATED TAGS

java

CONTRIBUTOR

Abhilash
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring