Package sk.antons.web.filter.log
Class LogFilter
- java.lang.Object
-
- sk.antons.web.filter.log.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 methodfilter.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.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.
- Author:
- antons
-
-
Constructor Summary
Constructors Constructor Description LogFilter()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description LogFilterconsumer(Consumer consumer, ConsumerStatus consumerStatus)Setup consumer of LogFilter messagesvoiddestroy()voiddoFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)protected voiddoFilterInternal(ServletRequestWrapper request, ServletResponseWrapper response, javax.servlet.FilterChain filterChain)LogFilterheaders(boolean value)Configure printing of header info in request and response.LogFilteridentity(boolean value)Configure printing of request to print principal name.voidinit(javax.servlet.FilterConfig fc)static LogFilterinstance()LogFilterjsonable(Jsonable jsonable)Setup jsonable decision provider.RequestLimiter<LogFilter>limit()Configure filter processing limitations.LogFilteroneLine(boolean value)Configure printing of payload as one lineLogFilterpayloads(boolean value)Configure printing of payload info in request and response.LogFilterprintable(Printable printable)Setup printable decision provider.LogFilterrequestBeforePrefix(String value)String to identify start request message if it is null message is not printed at all.protected voidrequestData(ServletRequestWrapper request, StringBuilder pathbuff, StringBuilder requestheaderbuff, StringBuilder requestpayloadbuff)LogFilterrequestHeaderFilter(String value)Add filter for displayed header info.LogFilterrequestHeaders(boolean value)Configure printing of header info in request.LogFilterrequestPayloads(boolean value)Configure printing of payload info in request.LogFilterrequestPrefix(String value)String to identify request info message if it is null message is not printed at all.protected voidresponseData(ServletResponseWrapper response, StringBuilder responseheadersbuff, StringBuilder responsepayloadbuff)LogFilterresponseHeaderFilter(String value)Add filter for displayed header info.LogFilterresponseHeaders(boolean value)Configure printing of header info in response.LogFilterresponsePayloads(boolean value)Configure printing of payload info in response.LogFilterresponsePrefix(String value)String to identify response info message if it is null message is not printed at all.LogFiltertruncateJsonLiteral(int value)Configure max length of string literal of jsonable payload to printLogFiltertruncateLineTo(int value)Configure max line length of printable payload to printLogFiltertruncateTo(int value)Configure max length of printable payload to print
-
-
-
Method Detail
-
instance
public static LogFilter instance()
-
doFilter
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws IOException, javax.servlet.ServletException- Specified by:
doFilterin interfacejavax.servlet.Filter- Throws:
IOExceptionjavax.servlet.ServletException
-
init
public void init(javax.servlet.FilterConfig fc) throws javax.servlet.ServletException- Specified by:
initin interfacejavax.servlet.Filter- Throws:
javax.servlet.ServletException
-
destroy
public void destroy()
- Specified by:
destroyin interfacejavax.servlet.Filter
-
limit
public RequestLimiter<LogFilter> limit()
Configure filter processing limitations.- Returns:
-
consumer
public LogFilter consumer(Consumer consumer, ConsumerStatus consumerStatus)
Setup consumer of LogFilter messages- Parameters:
consumer- message consumer (default is LogConsumer)consumerStatus- consumer on/off status provider (default ia LogConsumerStatus)- Returns:
- this
-
printable
public LogFilter printable(Printable printable)
Setup printable decision provider.- Parameters:
printable- printable decision provider (decision ia SiplePrintable)- Returns:
- this
-
jsonable
public LogFilter jsonable(Jsonable jsonable)
Setup jsonable decision provider.- Parameters:
printable- jsonable decision provider (decision ia SipleJsonablw)- Returns:
- this
-
headers
public LogFilter headers(boolean value)
Configure printing of header info in request and response.- Parameters:
value- true if info should be logged- Returns:
- this
-
payloads
public LogFilter payloads(boolean value)
Configure printing of payload info in request and response.- Parameters:
value- true if info should be logged- Returns:
- this
-
requestHeaders
public LogFilter requestHeaders(boolean value)
Configure printing of header info in request.- Parameters:
value- true if info should be logged- Returns:
- this
-
requestPayloads
public LogFilter requestPayloads(boolean value)
Configure printing of payload info in request.- Parameters:
value- true if info should be logged- Returns:
- this
-
responseHeaders
public LogFilter responseHeaders(boolean value)
Configure printing of header info in response.- Parameters:
value- true if info should be logged- Returns:
- this
-
responsePayloads
public LogFilter responsePayloads(boolean value)
Configure printing of payload info in response.- Parameters:
value- true if info should be logged- Returns:
- this
-
oneLine
public LogFilter oneLine(boolean value)
Configure printing of payload as one line- Parameters:
value- true if info should be logged- Returns:
- this
-
truncateTo
public LogFilter truncateTo(int value)
Configure max length of printable payload to print- Parameters:
value- max value of payload (0 unlimited)- Returns:
- this
-
truncateLineTo
public LogFilter truncateLineTo(int value)
Configure max line length of printable payload to print- Parameters:
value- max value of payload line (0 unlimited)- Returns:
- this
-
truncateJsonLiteral
public LogFilter truncateJsonLiteral(int value)
Configure max length of string literal of jsonable payload to print- Parameters:
value- max value of string literal (0 unlimited)- Returns:
- this
-
requestBeforePrefix
public LogFilter requestBeforePrefix(String value)
String to identify start request message if it is null message is not printed at all.- Parameters:
value- start request prefix (default 'REQ')- Returns:
- this
-
requestPrefix
public LogFilter requestPrefix(String value)
String to identify request info message if it is null message is not printed at all.- Parameters:
value- start request prefix (default 'REQ')- Returns:
- this
-
responsePrefix
public LogFilter responsePrefix(String value)
String to identify response info message if it is null message is not printed at all.- Parameters:
value- start request prefix (default 'RES')- Returns:
- this
-
requestHeaderFilter
public LogFilter requestHeaderFilter(String value)
Add filter for displayed header info.- Parameters:
value- name of the header property to be displayed- Returns:
- this
-
responseHeaderFilter
public LogFilter responseHeaderFilter(String value)
Add filter for displayed header info.- Parameters:
value- name of the header property to be displayed- Returns:
- this
-
identity
public LogFilter identity(boolean value)
Configure printing of request to print principal name.- Parameters:
value- true if info should be logged- Returns:
- this
-
doFilterInternal
protected void doFilterInternal(ServletRequestWrapper request, ServletResponseWrapper response, javax.servlet.FilterChain filterChain) throws javax.servlet.ServletException, IOException
- Throws:
javax.servlet.ServletExceptionIOException
-
requestData
protected void requestData(ServletRequestWrapper request, StringBuilder pathbuff, StringBuilder requestheaderbuff, StringBuilder requestpayloadbuff)
-
responseData
protected void responseData(ServletResponseWrapper response, StringBuilder responseheadersbuff, StringBuilder responsepayloadbuff)
-
-