Saturday 2 August 2014

How Filters work with Log Hierarchy


If logger has a filter, then based on the filter the message passed to handler. If the message is accepted by filter, then it passed to corresponding handlers of the logger and all the handlers of parent loggers. But when a message is passed up the hierarchy, the message is not passed through the Filter's of the parent Logger's.

Will see it by an Example
import java.util.logging.*;
public class FilterSevereMsgOnly implements Filter {

    @Override
    public boolean isLoggable(LogRecord record) {
        return record.getLevel().equals(Level.SEVERE);
    }
    
}

import java.util.logging.*;

public class FilterWarningMsgOnly implements Filter {

    @Override
    public boolean isLoggable(LogRecord record) {
        return record.getLevel().equals(Level.WARNING);
    }    
}

import java.io.IOException;
import java.util.logging.*;

public class LogHierarchy {
    static final Logger lev1 = Logger.getLogger("Level1");
    static final Logger lev2 = Logger.getLogger("Level1.Level2");
    static final Logger lev3 = Logger.getLogger("Level1.Level2.Level3");
    static Handler handler1, handler2, handler3;
    
    static FilterSevereMsgOnly severe = new FilterSevereMsgOnly();
    static FilterWarningMsgOnly warning = new FilterWarningMsgOnly();
        
    static void processData(){
        lev3.info("Processing started");
        /*Do Some processing here*/
        lev3.info("Processing completed");
    }
    
    public static void main(String args[]) throws IOException{
        handler1 = new FileHandler("file1.out");
        handler2 = new FileHandler("file2.out");
        handler3 = new FileHandler("file3.out");
        
        lev1.setFilter(severe);
        lev2.setFilter(warning);
        
        lev1.addHandler(handler1);
        lev2.addHandler(handler2);
        lev3.addHandler(handler3);
       
        processData();
    }
}

Run the above application, it creates 3 files 'file1.out', 'file2.out' and 'file3.out'.

Even though the loggers lev1 and lev2 configure filters like they handle severe and warning messages respectively, All the info messages stored in file1.out and file2.out. Since when a message is passed up the hierarchy, the message is not passed through the Filter's of the parent Logger's. But if the handlers are configured with filters then they filter it before publishing to outside world. Will change the above application and see.

import java.io.IOException;
import java.util.logging.*;

public class LogHierarchy {
    static final Logger lev1 = Logger.getLogger("Level1");
    static final Logger lev2 = Logger.getLogger("Level1.Level2");
    static final Logger lev3 = Logger.getLogger("Level1.Level2.Level3");
    static Handler handler1, handler2, handler3;
    
    static FilterSevereMsgOnly severe = new FilterSevereMsgOnly();
    static FilterWarningMsgOnly warning = new FilterWarningMsgOnly();
        
    static void processData(){
        lev3.info("Processing started");
        /*Do Some processing here*/
        lev3.info("Processing completed");
    }
    
    public static void main(String args[]) throws IOException{
        handler1 = new FileHandler("file1.out");
        handler2 = new FileHandler("file2.out");
        handler3 = new FileHandler("file3.out");
        
        handler1.setFilter(severe);
        handler2.setFilter(warning);
        
        lev1.addHandler(handler1);
        lev2.addHandler(handler2);
        lev3.addHandler(handler3);
       
        processData();
    }
}




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment