Saturday 2 August 2014

Filters

Filters are associated with both loggers and handlers. Filters decide whether to forward or discard particular log record.

To create a filter a class must implement Filter interface.

public interface Filter {
    public boolean isLoggable(LogRecord record);
}

'isLoggable' check whether to publish or discard the given log record.

Application without loggers
import java.io.IOException;
import java.util.logging.*;

public class FilterEx {
    static final Logger myLogger = Logger.getLogger("FilterEx");
    
    static void processData(){
        myLogger.info("Started Processing data");
        myLogger.log(Level.SEVERE, "Got Severe Error");
        myLogger.log(Level.WARNING, "Warning Message");
        myLogger.info("Stopped Processing data");
    }
    
    public static void main(String args[]) throws IOException{        
        FileHandler handle1 = null;
        FileHandler handle2 = null;
        
        try{
            handle1 =  new FileHandler("Severe.out");
            handle2 =  new FileHandler("Warning.out");

            /* Adding handlersto myLogger */
            myLogger.addHandler(handle1);
            myLogger.addHandler(handle2);

            /* Setting Level to Logger */
            myLogger.setLevel(Level.ALL);

            processData();
        }
        finally{
            if(handle1 != null)
                handle1.close();
            if(handle2 != null)
                handle2.close();
        }
    }
}

Run the above Application and it creates two files 'Severe.out' and 'Warning.out' which contains SEVERE, INFO and WARNING messages.

but requirement like below.

'Severe.out' : Contains only severe messages.
'Warning.out' : Contains only warning messages.

To achieve the above functionality, filters are useful.

First will create two filters, one to filter warning messages and other to handle severe messages.
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);
    }    
}

Instantiate the filters and set them to respective handlers.

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

public class FilterEx {
    static final Logger myLogger = Logger.getLogger("FilterEx");
    static final FilterSevereMsgOnly severe = new FilterSevereMsgOnly();
    static final FilterWarningMsgOnly warning = new FilterWarningMsgOnly();
    
    static void processData(){
        myLogger.info("Started Processing data");
        myLogger.log(Level.SEVERE, "Got Severe Error");
        myLogger.log(Level.WARNING, "Warning Message");
        myLogger.info("Stopped Processing data");
    }
    
    public static void main(String args[]) throws IOException{        
        FileHandler handle1 = null;
        FileHandler handle2 = null;
        
        try{
            handle1 =  new FileHandler("Severe.out");
            handle2 =  new FileHandler("Warning.out");

            /* Adding handlersto myLogger */
            myLogger.addHandler(handle1);
            myLogger.addHandler(handle2);

            /* Setting Level to Logger */
            myLogger.setLevel(Level.ALL);
            
            handle1.setFilter(severe);
            handle2.setFilter(warning);

            processData();
        }
        finally{
            if(handle1 != null)
                handle1.close();
            if(handle2 != null)
                handle2.close();
        }
    }
}

After running the above Application, it creates two files,
'Severe.out' : Contains only SEVERE messages
'Warning.out' : Contains only WARNING messages.

handle1.setFilter(severe);
set the filter 'severe' to handler handle1.

handle2.setFilter(warning);
set the filter 'warning' to handler handle2.







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment