Skip to content

Instrumenting Servlets

Karsten Schnitter edited this page Jul 2, 2018 · 12 revisions

Introduction

Servlet instrumentation is implemented as a standard servlet filter as defined in the Java Servlet Specification, Version 3.0 .

Enabling the Feature

In order to make this feature available in your application, you need to add the corresponding library to your Maven dependencies in your application's pom.xml like this, assuming that you've also defined a property cf-logging-version that refers to the latest version of this feature:

<!-- We're using the Servlet Filter instrumentation -->
<dependency>
   <groupId>com.sap.hcp.cf.logging</groupId>
   <artifactId>cf-java-logging-support-servlet</artifactId>
   <version>${cf-logging-version}</version>
</dependency>

Enabling the Servlet Filter

The easiest way to enable that servlet filter is to declare it in your application's web.xml file. To do so, add the following lines to that file, as we've done in the sample application:

<filter-name>request-logging</filter-name>
   <filter-class>com.sap.hcp.cf.logging.servlet.filter.RequestLoggingFilter</filter-class>
</filter>
<filter-mapping>
   <filter-name>request-logging</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

Enabling the Servlet Filter in Spring Boot

You can use FilterRegistrationBean to enable the servlet filter in Spring Boot. Add the following bean to your @Configuration file:

@Bean
public FilterRegistrationBean loggingFilter() {
	FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
	filterRegistrationBean.setFilter(new RequestLoggingFilter());
	filterRegistrationBean.setName("request-logging");
	filterRegistrationBean.addUrlPatterns("/*");
	filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST);
	return filterRegistrationBean;
}
Clone this wiki locally