Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
171 lines (127 sloc) 4.97 KB

resurfaceio-logger-java

© 2016-2019 Resurface Labs Inc.

Logging usage of Java cloud apps, with user privacy by design.

Contents

Dependencies

Requires Java 8+ and Java servlet classes, which are not included. No other dependencies to conflict with your app.

Installing with Maven

Add this section to pom.xml:

<dependency>
    <groupId>io.resurface</groupId>
    <artifactId>resurfaceio-logger</artifactId>
    <version>RELEASE</version>
</dependency>

Add this section too if servlet classes are not already available.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>RELEASE</version>
</dependency>

Logging From Servlet Filter

After installing the library, add a logging filter to web.xml.

<filter>
    <filter-name>HttpLoggerForServlets</filter-name>
    <filter-class>io.resurface.HttpLoggerForServlets</filter-class>
    <init-param>
        <param-name>url</param-name>
        <param-value>https://...</param-value>
    </init-param>
    <init-param>
        <param-name>rules</param-name>
        <param-value>include strict</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>HttpLoggerForServlets</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Add a CDATA section when specifying multiple rules at once like this:

    <init-param>
        <param-name>rules</param-name>
        <param-value><![CDATA[
            include strict
            sample 10
        ]]></param-value>
    </init-param>

Logging From Spring Boot

After installing the library, configure a FilterRegistrationBean to add a logging servlet filter.

@Bean
public FilterRegistrationBean httpLoggerFilter() {
    FilterRegistrationBean frb = new FilterRegistrationBean();
    frb.setFilter(new io.resurface.HttpLoggerForServlets());
    frb.setName("HttpLoggerForServlets");
    frb.addUrlPatterns("/*");
    frb.addInitParameter("url", "https://...");
    frb.addInitParameter("rules", "include strict");
    return frb;
}

Logging From Spark Framework

After installing the library, create a logger and call it from the routes of interest.

import io.resurface.HttpLogger;

HttpLogger logger = new HttpLogger("https://...", "include strict");

get("/hello", (request, response) -> {
    String response_body = "Hello World";
    logger.log(request.raw(), response.raw(), response_body);
    return response_body;
});

post("/hello_post", (request, response) -> {
    String response_body = "POSTED: " + request.body();
    logger.log(request.raw(), response.raw(), response_body, request.body());
    return response_body;
});

Alternatively configure an after filter to log across multiple routes at once.

after((request, response) -> {
    if (response.body() != null) {  // log successful responses only, not 404/500s
        logger.log(request.raw(), response.raw(), response.body(), request.body());
    }
});

Logging From Jersey

After installing the library, register a logger as a Jersey filter/interceptor. Note this will only log usage when a response body is returned.

ResourceConfig resourceConfig = new ResourceConfig(...);
resourceConfig.register(new io.resurface.HttpLoggerForJersey("https://...", "include strict"));
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, false);

Logging With API

Loggers can be directly integrated into your application using our API. This requires the most effort compared with the options described above, but also offers the greatest flexibility and control.

API documentation

Protecting User Privacy

Loggers always have an active set of rules that control what data is logged and how sensitive data is masked. All of the examples above apply a predefined set of rules (include strict), but logging rules are easily customized to meet the needs of any application.

Logging rules documentation

You can’t perform that action at this time.