An open API service indexing awesome lists of open source software.

https://github.com/resurfaceio/logger-java

Log API calls with Java
https://github.com/resurfaceio/logger-java

api-logger http-logger java jersey logger-java servlet-filter spark-framework spring-boot

Last synced: 8 days ago
JSON representation

Log API calls with Java

Awesome Lists containing this project

README

        

# resurfaceio-logger-java
Easily log API requests and responses to your own security data lake.

[![Maven Central](https://img.shields.io/maven-central/v/io.resurface/resurfaceio-logger)](https://maven-badges.herokuapp.com/maven-central/io.resurface/resurfaceio-logger)
[![CodeFactor](https://www.codefactor.io/repository/github/resurfaceio/logger-java/badge)](https://www.codefactor.io/repository/github/resurfaceio/logger-java)
[![License](https://img.shields.io/github/license/resurfaceio/logger-java)](https://github.com/resurfaceio/logger-java/blob/master/LICENSE)
[![Contributing](https://img.shields.io/badge/contributions-welcome-green.svg)](https://github.com/resurfaceio/logger-java/blob/master/CONTRIBUTING.md)

## 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`:

```xml

io.resurface
resurfaceio-logger
2.2.0

```

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

```xml

javax.servlet
javax.servlet-api
RELEASE

```

## Logging From Servlet Filter

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

```xml

HttpLoggerForServlets
io.resurface.HttpLoggerForServlets

url
http://localhost:7701/message


rules
include debug

HttpLoggerForServlets
/*

```

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

```xml

rules


```

## Logging From Spring Boot

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

```java
@Bean
public FilterRegistrationBean httpLoggerFilter() {
FilterRegistrationBean frb = new FilterRegistrationBean();
frb.setFilter(new io.resurface.HttpLoggerForServlets());
frb.setName("HttpLoggerForServlets");
frb.addUrlPatterns("/*");
frb.addInitParameter("url", "http://localhost:7701/message");
frb.addInitParameter("rules", "include debug");
return frb;
}
```

## Logging From Spark Framework

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

```java
import io.resurface.*;

HttpLogger logger = new HttpLogger("http://localhost:7701/message", "include debug");

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

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

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

```java
after((request, response) -> {
if (response.body() != null) { // log successful responses only, not 404/500s
HttpMessage.send(logger, 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.

```java
ResourceConfig resourceConfig = new ResourceConfig(...);
resourceConfig.register(new io.resurface.HttpLoggerForJersey("http://localhost:7701/message", "include debug"));
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, false);
```

## Logging With API

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

[API documentation](API.md)

## 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 debug`),
but logging rules are easily customized to meet the needs of any application.

Logging rules documentation

---
© 2016-2024 Graylog, Inc.