Class LogFilter

  • All Implemented Interfaces:
    javax.servlet.Filter

    public class LogFilter
    extends Object
    implements javax.servlet.Filter
    LogFilter logs content of http requests and responses. You can use them to debug your web API. It logs following events
  • start or the request with method and path (and 'vvv' sequence indicating that relevant log is bellow)
  • possible unhandled exception (ussualy not present)
  • request path headers and payload
  • response status headers and payload It enable variety of configure 'where', 'what' and 'how' to log messages. And also there is possible to configure when to log and when not. This is usefull as possibility to configure filter presence itself is ussually pretty complicated. Filter configuration There is only API way to configure the filter. So if you have no possibility to use API directy you must inherit from LogFilter configure and use your inherited (and configured) instance. Filter configuration - where Consumer is abstraction target for log message. By default fileter log messages using jdk14 logging API. It uses sk.antons.web.filter.log.LogFilter logger and produce messages to FINEST level. If you want to use different API for logging you simply implement Consumer and ConsumerStatus (check if logging is enabled) interfaces and configure filter Example for slf4j and debug level.
       filter.consumer(
                   (message) -> { log.debug(message); } 
                   , () -> { return log.isDebugEnabled();} );
     
    Filter configuration - when LogFilter was implemented as example of usage RequestLimiter class/lib. By default is LogFilter configured with empty RequestLimiter so it is applied for all requests where is configured. But it is possible to limit LogFilter usage by configuration of RequestLimiter. See this class for configuration possibilities. Example for limit LogFilter functionality using path and method
       filter.limit()
                   .path()
                        .include("/foo/**")
                        .exclude("/foo/bar", "POST");
     
    Filter configuration - what
    • filter.requestBeforePrefix("REQ") If it is set to null no start request message will be displayed
    • filter.requestPrefix("REQ") If it is set to null no request message will be displayed
    • filter.responsePrefix("RES") If it is set to null no response message will be displayed
    Filter configuration - how
    • filter.logHeaders(true) If it is set to false no header information is included in request message and response message
    • filter.logPayload(true) If it is set to false no payload information is included in request message and response message. There are also filter.printable() which define if payload can be displayed depending on contentType.
    • filter.truncateTo(0) If it is set to value >0 printable payload will be truncated to this value. (usefull for variety of base64 values....)
    • filter.truncateLineTo(0) If it is set to value >0 each line of printable payload will be truncated to this value. (usefull for variety of base64 values followed by another usefful information where truncateTo skip that information)
    • filter.truncateJsonelementTo(0) If it is set to value >0 and content is filter.jsonable() each string literal will be truncated to this value. (usefull for variety of base64 attributes in json)
    • filter.forceOneLine(true) If it is set to false content is not formated otherwise itis formated to one line. New line characters are escaped with \\n. Json content is formated to one line in inative form.
    Filter configuration - example This is simple example for springboot and jdk14 default logging
Author:
antons