Class LogFilter

  • All Implemented Interfaces:
    java.io.Serializable , org.apache.jmeter.protocol.http.util.accesslog.Filter

    @AutoService(value = Filter.class) 
    public class LogFilter
     implements Filter, Serializable
                        

    Description: LogFilter is a basic implementation of Filter interface. This implementation will keep a record of the filtered strings to avoid repeating the process unnecessarily.

    The current implementation supports replacing the file extension. The reason for supporting this is from first hand experience porting an existing website to Tomcat + JSP. Later on we may want to provide the ability to replace the whole filename. If the need materializes, we can add it later.

    Example of how to use it is provided in the main method. An example is provided below.

    
    testf = new LogFilter();
    String[] incl = { "hello.html", "index.html", "/index.jsp" };
    String[] theFiles = {
        "/test/hello.jsp", "/test/one/hello.html", "hello.jsp", "hello.htm", "/test/open.jsp",
        "/test/open.html", "/index.jsp", "/index.jhtml", "newindex.jsp", "oldindex.jsp", "oldindex1.jsp",
        "oldindex2.jsp", "oldindex3.jsp", "oldindex4.jsp", "oldindex5.jsp", "oldindex6.jsp", "/test/index.htm"
    };
    testf.excludeFiles(incl);
    System.out.println(" ------------ exclude test -------------");
    for (String theFile : theFiles) {
     boolean fl = testf.isFiltered(theFile);
     String line = testf.filter(theFile);
     if (line != null) {
        System.out.println("the file: " + line);
     }
    }
    
    As a general note. Both isFiltered and filter() have to be called. Calling either one will not produce the desired result. isFiltered(string) will tell you if a string should be filtered. The second step is to filter the string, which will return null if it is filtered and replace any part of the string that should be replaced.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      LogFilter()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      void setReplaceExtension(String oldext, String newext) The method will replace the file extension with the new one.
      void includeFiles(Array<String> filenames) Give the filter a list of files to include
      void excludeFiles(Array<String> filenames) Give the filter a list of files to exclude
      void includePattern(Array<String> regexp) Give the filter a set of regular expressions to filter with for inclusion.
      void excludePattern(Array<String> regexp) Give the filter a set of regular expressions to filter with for exclusion.
      boolean isFiltered(String path, TestElement el) In the case of log filtering the important thing is whether the log entry should be used.
      boolean incFile(String text) Method implements the logic for filtering file name inclusion.
      boolean excFile(String text) Method implements the logic for filtering file name exclusion.
      boolean replaceExtension(String text) Method uses indexOf to replace the old extension with the new extension.
      String filter(String text) The current implementation checks the boolean if the text should be used or not.
      Pattern createPattern(String pattern) create a new pattern object from the string.
      void reset() Tell the filter when the parsing has reached the end of the log file and is about to begin again.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LogFilter

        LogFilter()
    • Method Detail

      • setReplaceExtension

         void setReplaceExtension(String oldext, String newext)

        The method will replace the file extension with the new one. You can either provide the extension without the period ".", or with. The method will check for period and add it if it isn't present.

      • includeFiles

         void includeFiles(Array<String> filenames)

        Give the filter a list of files to include

        Parameters:
        filenames - list of files to include
      • excludeFiles

         void excludeFiles(Array<String> filenames)

        Give the filter a list of files to exclude

        Parameters:
        filenames - list of files to exclude
      • includePattern

         void includePattern(Array<String> regexp)

        Give the filter a set of regular expressions to filter with for inclusion. This method hasn't been fully implemented and test yet. The implementation is not complete.

        Parameters:
        regexp - list of regular expressions
      • excludePattern

         void excludePattern(Array<String> regexp)

        Give the filter a set of regular expressions to filter with for exclusion. This method hasn't been fully implemented and test yet. The implementation is not complete.

        Parameters:
        regexp - list of regular expressions
      • isFiltered

         boolean isFiltered(String path, TestElement el)

        In the case of log filtering the important thing is whether the log entry should be used. Therefore, the method will only return true if the entry should be used. Since the interface defines both inclusion and exclusion, that means by default inclusion filtering assumes all entries are excluded unless it matches. In the case of exclusion filtering, it assumes all entries are included unless it matches, which means it should be excluded.

        Parameters:
        path - path to be tested
        Returns:

        true if entry should be excluded

      • incFile

         boolean incFile(String text)

        Method implements the logic for filtering file name inclusion. The method iterates through the array and uses indexOf. Once it finds a match, it won't bother with the rest of the filenames in the array.

        Parameters:
        text - name of the file to tested (must not be null)
        Returns:

        boolean include

      • excFile

         boolean excFile(String text)

        Method implements the logic for filtering file name exclusion. The method iterates through the array and uses indexOf. Once it finds a match, it won't bother with the rest of the filenames in the array.

        Parameters:
        text - name of the file to be tested (must not be null)
        Returns:

        boolean exclude

      • replaceExtension

         boolean replaceExtension(String text)

        Method uses indexOf to replace the old extension with the new extension. It might be good to use regular expression, but for now this is a simple method. The method isn't designed to replace multiple instances of the text, since that isn't how file extensions work. If the string contains more than one instance of the old extension, only the first instance will be replaced.

        Parameters:
        text - name of the file in which the extension should be replaced (must not be null)
        Returns:

        true if the extension could be replaced, false otherwise

      • filter

         String filter(String text)

        The current implementation checks the boolean if the text should be used or not. isFilter( string) has to be called first.

      • createPattern

         Pattern createPattern(String pattern)

        create a new pattern object from the string.

        Parameters:
        pattern - string representation of the perl5 compatible regex pattern
        Returns:

        compiled Pattern, or null if no pattern could be compiled

      • reset

         void reset()

        Tell the filter when the parsing has reached the end of the log file and is about to begin again. Gives the filter a chance to adjust it's values, if needed.