Home » Core Java » util » logging » Set filter on Logger handler

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Set filter on Logger handler

This is an example of how to set a filter on a Logger’s Handler. The Filter is used to provide control over what is logged, beyond the control that the levels provide. Each Logger and each Handler can have a filter associated with it. The Logger or Handler will call the isLoggable(LogRecord record) method to check if a given LogRecord should be published. Setting filter on Logger handler implies that you should:

  • Create a new FileHandler that will append to a specific file.
  • Create a new Filter that will override the isLoggable(LogRecord record) method of the Handler, in order to check which messages will be published.
  • Set the Filter to control the handler’s output, with the setFilter(Filter newFilter) API method of the Handler.
  • Create a new Logger instance and add the handler to the logger, with the addHandler(Handler handler) API method of the Logger.
  • Invoke the setLevel(Level newLevel) API method of the Logger to set a Level to the logger.
  • Log messages using one of the logger’s methods. In the example the info(String msg) method is used to log messages in level INFO. Since the Logger’s Level is set to FINEST the messages should be all forwarded to the Handler, but since the Filter is used only messages that start with IMPORTANT will be writen in the log file by the FileHandler.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.logging.FileHandler;
import java.util.logging.Filter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class SetFilterOnLoggerHandler {
	
	public static void main(String[] args) throws Exception {
		
		boolean append = false;
	    FileHandler handler = new FileHandler("default.log", append);
	    
	    handler.setFilter(new Filter() {
			@Override
			public boolean isLoggable(LogRecord record) {
				return record.getMessage().startsWith("IMPORTANT");
			}
		});

	    Logger logger = Logger.getLogger("com.javacodegeeks.snippets.core");
	    logger.addHandler(handler);
	    logger.setLevel(Level.FINEST);
	    
	    logger.info("info message");
	    logger.info("IMPORTANT info message");
		
	}

}

Output:

Nov 19, 2011 3:56:13 PM com.javacodegeeks.snippets.core.SetFilterOnLoggerHandler main
INFO: info message
Nov 19, 2011 3:56:13 PM com.javacodegeeks.snippets.core.SetFilterOnLoggerHandler main
INFO: IMPORTANT info message

default.log

<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
  <date>2011-11-19T15:56:13</date>
  <millis>1321710973344</millis>
  <sequence>1</sequence>
  <logger>com.javacodegeeks.snippets.core</logger>
  <level>INFO</level>
  <class>com.javacodegeeks.snippets.core.SetFilterOnLoggerHandler</class>
  <method>main</method>
  <thread>10</thread>
  <message>IMPORTANT info message</message>
</record>
</log>

 
This was an example of how to set a filter on a Logger’s Handler in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area from our partners over at ZipRecruiter

 

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns