Skip to content

Commit 384b93f

Browse files
authored
Merge pull request #808 from DrorBuhnik/master
Add option to log url request including query string
2 parents aca3160 + bb1ca35 commit 384b93f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

jooby/src/main/java/org/jooby/RequestLogger.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ public class RequestLogger implements Route.Handler {
197197
private static final char BL = '[';
198198
private static final char BR = ']';
199199
private static final char Q = '\"';
200+
private static final char QUERY = '?';
200201

201202
private static Function<Request, String> ANNON = req -> DASH;
202203

@@ -211,6 +212,8 @@ public class RequestLogger implements Route.Handler {
211212

212213
private boolean latency;
213214

215+
private boolean queryString;
216+
214217
private boolean extended;
215218

216219
/**
@@ -247,6 +250,9 @@ public void handle(final Request req, final Response rsp) throws Throwable {
247250
sb.append(Q).append(req.method());
248251
sb.append(SP);
249252
sb.append(req.path());
253+
if (queryString) {
254+
req.queryString().ifPresent(s -> sb.append(QUERY).append(s));
255+
}
250256
sb.append(SP);
251257
sb.append(req.protocol());
252258
sb.append(Q).append(SP);
@@ -331,6 +337,16 @@ public RequestLogger latency() {
331337
return this;
332338
}
333339

340+
/**
341+
* Log full path of the request including query string.
342+
*
343+
* @return This instance.
344+
*/
345+
public RequestLogger queryString() {
346+
this.queryString = true;
347+
return this;
348+
}
349+
334350
/**
335351
* Append <code>Referer</code> and <code>User-Agent</code> entries to the NCSA line.
336352
*

jooby/src/test/java/org/jooby/RequestLoggerTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ public void latency() throws Exception {
7070
}, onComplete);
7171
}
7272

73+
@Test
74+
public void queryString() throws Exception {
75+
new MockUnit(Request.class, Response.class)
76+
.expect(capture)
77+
.expect(timestamp(7L))
78+
.expect(ip("127.0.0.1"))
79+
.expect(method("GET"))
80+
.expect(path("/path"))
81+
.expect(query("query=true"))
82+
.expect(protocol("HTTP/1.1"))
83+
.expect(status(Status.OK))
84+
.expect(len(345L))
85+
.run(unit -> {
86+
new RequestLogger()
87+
.dateFormatter(ZoneId.of("UTC"))
88+
.queryString()
89+
.log(line -> assertEquals(
90+
"127.0.0.1 - - [01/Jan/1970:00:00:00 +0000] \"GET /path?query=true HTTP/1.1\" 200 345", line))
91+
.handle(unit.get(Request.class), unit.get(Response.class));
92+
}, onComplete);
93+
}
94+
7395
@Test
7496
public void extended() throws Exception {
7597
new MockUnit(Request.class, Response.class)
@@ -147,6 +169,13 @@ private Block path(final String path) {
147169
};
148170
}
149171

172+
private Block query(final String query) {
173+
return unit -> {
174+
Request req = unit.get(Request.class);
175+
expect(req.queryString()).andReturn(Optional.of(query));
176+
};
177+
}
178+
150179
private Block status(final Status status) {
151180
return unit -> {
152181
Response rsp = unit.get(Response.class);

0 commit comments

Comments
 (0)