Setting Filter at Handler level

Java logging provides log levels at logger and handler level, to filter the messages.

To make sure only required messages are logged.

We can extend this functionality and provide our own filter, which will filter messages based on message’s metadata in addition to filtering by log levels.

We can add this filter at logger level and also at handler level.

In this post I will explain how to create a custom filter at handler level.

For this example I will create a custom filter which will print messages only from particular logger.

We create a custom filter by implementing the Filter interface as shown below

Custom Filter


package logging;

import java.util.logging.Filter;
import java.util.logging.LogRecord;

public class CustomFilter1 implements Filter {
    private String loggerName;
    
    @Override
    public boolean isLoggable(LogRecord logRecord) {
        if(logRecord.getLoggerName().equals(loggerName))
            return true;
        else
            return false;
    }

    public String getLoggerName() {
        return loggerName;
    }

    public void setLoggerName(String loggerName) {
        this.loggerName = loggerName;
    }
}

In the above code, I implement isLoggable method to check for LogRecord’s logger name, where LogRecord represents the information to be logged.

If the logger name matches the instance’s logger name then the message is logged otherwise ignored.

Main method


1  package logging;
2  
3  import java.io.IOException;
4  import java.util.logging.FileHandler;
5  import java.util.logging.Logger;
6  import java.util.logging.SimpleFormatter;
7  
8  public class LoggingDemo5 {
9   public static void main(String[] args) throws IOException {
10      Logger logger1 = Logger.getLogger("logger1");
11      Logger logger2 = Logger.getLogger("logger2");
12      
13      SimpleFormatter simpleFormatter = new SimpleFormatter();
14      
15      CustomFilter1 customFilter = new CustomFilter1();
16      customFilter.setLoggerName("logger1");
17      
18      FileHandler fileHandler = null;
19      try {
20          fileHandler = new FileHandler("java.log");
21          fileHandler.setFilter(customFilter);
22          fileHandler.setFormatter(simpleFormatter);
23          logger1.addHandler(fileHandler);
24          logger2.addHandler(fileHandler);
25          
26          customFilter.setLoggerName("logger1");
27          logger1.info("Enabling Logger1: Hello my name is Logger1");
28          logger2.info("Enabling Logger1: Hello my name is Logger2");
29          
30          customFilter.setLoggerName("logger2");
31          logger1.info("Enabling Logger2: Hello my name is Logger1");
32          logger2.info("Enabling Logger2: Hello my name is Logger2");
33      } finally {
34          fileHandler.close();
35      }
36  }
37 }

In the above code, at line 15 I create an instance of CustomFilter1 and at line 16 set the logger name. Now the customFilter should log only the messages which are being logged by “logger1”.

At line 20 created an instance of FileHandler and at line 21 set its filter to instance of CustomFilter1.

Output

Oct 01, 2017 12:23:44 PM logging.LoggingDemo5 main
INFO: Enabling Logger1: Hello my name is Logger1
Oct 01, 2017 12:23:44 PM logging.LoggingDemo5 main
INFO: Enabling Logger2: Hello my name is Logger2

As you can see from the output when Logger1 is enabled (by setting the customFilter logger name propety to logger1). It prints only the message from Logger1 and not from Logger2.

Similarly when Logger2 is enabled (by setting the customFilter logger name property to logger2). It prints only the message from logger2 and not from logger1.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s