Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Commit bd8e9da

Browse files
committed
Add Logback and Filter to log requests
1 parent 1e19ceb commit bd8e9da

File tree

6 files changed

+119
-5
lines changed

6 files changed

+119
-5
lines changed

.env.example

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
TWILIO_ACCOUNT_SID=
2-
TWILIO_API_KEY=
3-
TWILIO_API_SECRET=
4-
TWILIO_CONFIGURATION_SID=
1+
export TWILIO_ACCOUNT_SID=
2+
export TWILIO_API_KEY=
3+
export TWILIO_API_SECRET=
4+
export TWILIO_CONFIGURATION_SID=

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ local.properties
2525
.loadpath
2626
nb-configuration.xml
2727

28+
.idea
29+
*.iml
30+
2831
# Eclipse Core
2932
.project
3033

pom.xml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
<dependency>
1717
<groupId>com.sparkjava</groupId>
1818
<artifactId>spark-core</artifactId>
19-
<version>2.3</version>
19+
<version>2.5.5</version>
20+
<exclusions>
21+
<exclusion>
22+
<groupId>org.slf4j</groupId>
23+
<artifactId>slf4j-simple</artifactId>
24+
</exclusion>
25+
</exclusions>
2026
</dependency>
2127
<dependency>
2228
<groupId>com.google.code.gson</groupId>
@@ -33,10 +39,30 @@
3339
<artifactId>twilio</artifactId>
3440
<version>(7.0,7.9)</version>
3541
</dependency>
42+
<dependency>
43+
<groupId>ch.qos.logback</groupId>
44+
<artifactId>logback-classic</artifactId>
45+
<version>1.2.2</version>
46+
</dependency>
3647
</dependencies>
3748

3849
<build>
3950
<plugins>
51+
<plugin>
52+
<groupId>org.codehaus.mojo</groupId>
53+
<artifactId>exec-maven-plugin</artifactId>
54+
<version>1.1</version>
55+
<executions>
56+
<execution>
57+
<goals>
58+
<goal>java</goal>
59+
</goals>
60+
</execution>
61+
</executions>
62+
<configuration>
63+
<mainClass>com.twilio.Webapp</mainClass>
64+
</configuration>
65+
</plugin>
4066
<plugin>
4167
<groupId>org.apache.maven.plugins</groupId>
4268
<artifactId>maven-shade-plugin</artifactId>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.twilio;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import spark.Filter;
6+
import spark.Request;
7+
import spark.Response;
8+
9+
import java.util.Collection;
10+
import java.util.function.Function;
11+
import java.util.stream.Collectors;
12+
13+
public class LoggingFilter implements Filter {
14+
15+
private static final String TEMPLATE_WITH_BODY =
16+
"\nRequest {} {} {} HEADERS:[{}] BODY: {}\nResponse {} HEADERS:[{}] BODY: {} ";
17+
private static final String TEMPLATE_WITH_NO_BODY =
18+
"\nRequest {} {} {} HEADERS:[{}] \nResponse {} HEADERS:[{}]";
19+
private static Logger logger = LoggerFactory.getLogger(LoggingFilter.class);
20+
21+
@Override
22+
public void handle(Request request, Response response) throws Exception {
23+
String requestHeaderString = buildHeadersString(request.headers(),
24+
h -> request.headers(h));
25+
String responseHeaderString = buildHeadersString(response.raw().getHeaderNames(),
26+
h -> response.raw().getHeader(h));
27+
String template;
28+
Object[] params;
29+
if(logger.isDebugEnabled()) {
30+
template = TEMPLATE_WITH_BODY;
31+
params = new Object[] {
32+
request.requestMethod(),
33+
request.pathInfo(),
34+
request.protocol(),
35+
requestHeaderString,
36+
request.body(),
37+
response.raw().getStatus(),
38+
responseHeaderString,
39+
response.body()
40+
};
41+
} else {
42+
template = TEMPLATE_WITH_NO_BODY;
43+
params = new Object[] {
44+
request.requestMethod(),
45+
request.pathInfo(),
46+
request.protocol(),
47+
requestHeaderString,
48+
response.raw().getStatus(),
49+
responseHeaderString,
50+
};
51+
}
52+
logger.info(template, params);
53+
}
54+
55+
private String buildHeadersString(Collection<String> headers, Function<String, String> getHeader) {
56+
return headers
57+
.stream()
58+
.map(h -> h + ":" + getHeader.apply(h))
59+
.collect(Collectors.joining(", "));
60+
}
61+
}

src/main/java/com/twilio/Webapp.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.HashMap;
1111
import java.util.Properties;
1212

13+
import static spark.Spark.afterAfter;
1314
import static spark.Spark.get;
1415
import static spark.Spark.staticFileLocation;
1516

@@ -25,6 +26,9 @@ public static void main(String[] args) throws Exception {
2526
// Create a Faker instance to generate a random username for the connecting user
2627
Faker faker = new Faker();
2728

29+
// Log all requests and responses
30+
afterAfter(new LoggingFilter());
31+
2832
// Create an access token using our Twilio credentials
2933
get("/token", "application/json", (request, response) -> {
3034
// Generate a random username for the connecting client
@@ -50,6 +54,8 @@ public static void main(String[] args) throws Exception {
5054
Gson gson = new Gson();
5155
return gson.toJson(json);
5256
});
57+
58+
5359
}
5460

5561
private static void dotenv() throws Exception {

src/main/resources/logback.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration scan="true" scanPeriod="30 seconds">
3+
<!-- address performance concern with jul-to-slf4j -->
4+
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
5+
<resetJUL>true</resetJUL>
6+
</contextListener>
7+
8+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
9+
<encoder>
10+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
11+
</pattern>
12+
</encoder>
13+
</appender>
14+
15+
<root level="info">
16+
<appender-ref ref="STDOUT"/>
17+
</root>
18+
</configuration>

0 commit comments

Comments
 (0)