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
- Host: GitHub
- URL: https://github.com/resurfaceio/logger-java
- Owner: resurfaceio
- License: apache-2.0
- Created: 2016-01-23T20:26:27.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-01-08T14:32:52.000Z (over 1 year ago)
- Last Synced: 2025-03-28T00:45:04.986Z (26 days ago)
- Topics: api-logger, http-logger, java, jersey, logger-java, servlet-filter, spark-framework, spring-boot
- Language: Java
- Homepage:
- Size: 544 KB
- Stars: 13
- Watchers: 5
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# resurfaceio-logger-java
Easily log API requests and responses to your own security data lake.[](https://maven-badges.herokuapp.com/maven-central/io.resurface/resurfaceio-logger)
[](https://www.codefactor.io/repository/github/resurfaceio/logger-java)
[](https://github.com/resurfaceio/logger-java/blob/master/LICENSE)
[](https://github.com/resurfaceio/logger-java/blob/master/CONTRIBUTING.md)## Contents
- Dependencies
- Installing With Maven
- Logging From Servlet Filter
- Logging From Spring Boot
- Logging From Spark Framework
- Logging From Jersey
- Logging With API
- Protecting User Privacy
## 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.
---
© 2016-2024 Graylog, Inc.