xebia-logfilter-extras

Filter to dump request and response for Servlet API

License

License

GroupId

GroupId

fr.xebia.extras
ArtifactId

ArtifactId

xebia-logfilter-extras
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

xebia-logfilter-extras
Filter to dump request and response for Servlet API
Source Code Management

Source Code Management

https://github.com/xebia-france/xebia-logfilter-extras

Download xebia-logfilter-extras

How to add to project

<!-- https://jarcasting.com/artifacts/fr.xebia.extras/xebia-logfilter-extras/ -->
<dependency>
    <groupId>fr.xebia.extras</groupId>
    <artifactId>xebia-logfilter-extras</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/fr.xebia.extras/xebia-logfilter-extras/
implementation 'fr.xebia.extras:xebia-logfilter-extras:1.0.1'
// https://jarcasting.com/artifacts/fr.xebia.extras/xebia-logfilter-extras/
implementation ("fr.xebia.extras:xebia-logfilter-extras:1.0.1")
'fr.xebia.extras:xebia-logfilter-extras:jar:1.0.1'
<dependency org="fr.xebia.extras" name="xebia-logfilter-extras" rev="1.0.1">
  <artifact name="xebia-logfilter-extras" type="jar" />
</dependency>
@Grapes(
@Grab(group='fr.xebia.extras', module='xebia-logfilter-extras', version='1.0.1')
)
libraryDependencies += "fr.xebia.extras" % "xebia-logfilter-extras" % "1.0.1"
[fr.xebia.extras/xebia-logfilter-extras "1.0.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.5

provided (1)

Group / Artifact Type Version
javax.servlet : servlet-api jar 2.5

test (3)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.0.7
junit : junit jar 4.8.1
org.mockito : mockito-all jar 1.9.5

Project Modules

There are no modules declared in this project.

Build Status

xebia-logfilter-extras

This project provide a ServletFilter that can dump request and response to the SLF4J API implementation in use. When the logger has a DEBUG level, it will wrap request/response and intercept data from the body. If level is higher than DEBUG, the filter will simply call the filter chain without doing anything.

Dumping can be enabled separately for request and response. To provide easy coupling between request and response the filter will generate an Integer base ID when processing.

It is possible to limit the size of the dump using the maxDumpSizeInKB init parameter. This parameter defines the max size of the dump in Kilo Bytes, beware that it defaults to 500KB.

This project depends only on SLF4J API, and servlet 2.5 API considered as provided. It is built using Java 1.7 using code style 1.7 at compile time. If you need older code style or any other feature ask for it using the issue tracker.

Using the RequestLoggerFilter

RequestLoggerFilter is a ServletFilter that Dump full request and response to an SLF4J logger.

  • RequestLoggerFilter.request - Dumps the request if debug is enabled
  • RequestLoggerFilter.response - Dumps the response if debug is enabled
  • RequestLoggerFilter.headers - Add Http Headers to request and response logs if debug is enabled

Mapping sample

Here is an extract from a war web.xml, configuring the RequestLoggerFilter to be able to dump every request and response on /rest/* URL pattern.

  <filter>
    <filter-name>requestLoggerFilter</filter-name>
    <filter-class>fr.xebia.extras.filters.logfilters.RequestLoggerFilter</filter-class>
    <init-param>
        <param-name>maxDumpSizeInKB</param-name>
        <param-value>500</param-value>
      </init-param>
  </filter>
  
  <!-- ... -->
    
  <filter-mapping>
      <filter-name>requestLoggerFilter</filter-name>
      <url-pattern>/rest/*</url-pattern>
  </filter-mapping>

logback configuration

In this sample we use logback, but since we use slf4j, you can use any implementation of the API to configure it. The main things to notice are the loggers names. Please also notice that headers logger is only used to enable the dumping of the headers, no logs are appended to it.

<!--
Dumping HTTP requests and response with their headers to the console appender
 using the RequestLoggerFilter
-->
   <logger name="RequestLoggerFilter.request" level="debug" additivity="false">
       <appender-ref ref="console"/>
   </logger>

   <logger name="RequestLoggerFilter.response" level="info" additivity="false">
       <appender-ref ref="console"/>
   </logger>

   <logger name="RequestLoggerFilter.headers" level="info" additivity="false">
       <appender-ref ref="console"/>
   </logger>

Maven dependency

This project is available via maven central repository

For release version:

<dependency>
  <groupId>fr.xebia.extras</groupId>
  <artifactId>xebia-logfilter-extras</artifactId>
  <version>1.0.1</version>
</dependency>

For SNAPSHOT version:

<dependency>
  <groupId>fr.xebia.extras</groupId>
  <artifactId>xebia-logfilter-extras</artifactId>
  <version>1.0.2-SNAPSHOT</version>
</dependency>
fr.xebia.extras

Publicis Sapient Engineering

Communauté Tech de Publicis Sapient, nous créons des logiciels de haute qualité. Nos terrains de jeu : Data, Cloud, Dev front, back, mobile, Coaching agile

Versions

Version
1.0.1
1.0.0