Skip to content

Commit e69210c

Browse files
author
Igor Polevoy
committed
#339 Logger in RequestDispatcher generates two separate log lines on exception (5XX response)
1 parent 4774e8e commit e69210c

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

activeweb/src/main/java/org/javalite/activeweb/RequestDispatcher.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,6 @@ private void renderSystemError(String template, String layout, int status, Throw
284284

285285
logDone(e);
286286

287-
if(status >= 500){
288-
logger.error("ERROR!", e);
289-
}
290-
291-
292287
HttpServletRequest req = RequestContext.getHttpRequest();
293288
String requestedWith = req.getHeader("x-requested-with") == null ?
294289
req.getHeader("X-Requested-With") : req.getHeader("x-requested-with");
@@ -337,10 +332,15 @@ private void logDone(Throwable throwable) {
337332
+ "\",\"action\":\"" + action
338333
+ "\",\"duration_millis\":" + millis
339334
+ ",\"method\":\"" + method
340-
+ ",\"url\":\"" + url
335+
+ "\",\"url\":\"" + url
341336
+ (throwable != null ? "\",\"error\":\"" + JsonHelper.sanitize(throwable.getMessage() != null ? throwable.getMessage() : throwable.toString()) : "")
342337
+ "\",\"status\":" + status + "}";
343-
logger.info(log);
338+
339+
if(throwable != null && status >= 500){
340+
logger.error(log, throwable);
341+
}else {
342+
logger.info(log);
343+
}
344344
}
345345

346346
public void destroy() {

activeweb/src/test/java/org/javalite/activeweb/controller_filters/JSONLogSpec.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public void shouldPrintControllerException() throws IOException, ServletExceptio
7878
Map log0 = JsonHelper.toMap(logs[0]);
7979
the(log0.get("level")).shouldBeEqual("INFO");
8080
the(log0.get("logger")).shouldBeEqual("org.javalite.activeweb.RequestDispatcher");
81-
8281
Map message = (Map) log0.get("message");
8382
the(message.get("info")).shouldBeEqual("executing controller");
8483
the(message.get("controller")).shouldBeEqual("app.controllers.LoggingController");
@@ -87,33 +86,24 @@ public void shouldPrintControllerException() throws IOException, ServletExceptio
8786

8887
//Line 1
8988
Map log1 = JsonHelper.toMap(logs[1]);
90-
the(log1.get("level")).shouldBeEqual("INFO");
89+
the(log1.get("level")).shouldBeEqual("ERROR");
9190
the(log1.get("timestamp")).shouldNotBeNull();
9291
the(log1.get("logger")).shouldBeEqual("org.javalite.activeweb.RequestDispatcher");
93-
9492
message = (Map) log1.get("message");
9593
the(message.get("controller")).shouldBeEqual("app.controllers.LoggingController");
9694
the(message.get("duration_millis")).shouldNotBeNull();
9795
the(message.get("method")).shouldBeEqual("GET");
9896
the(message.get("status")).shouldBeEqual(500);
97+
Map exception = (Map) log1.get("exception");
98+
the(exception.get("message")).shouldBeEqual("blah!");
99+
the(exception.get("stacktrace")).shouldContain("java.lang.RuntimeException: blah!\njava.lang.RuntimeException: blah!\n\tat app.controllers.LoggingController.error");
99100

100101
//Line 2
101102
Map log2 = JsonHelper.toMap(logs[2]);
102-
the(log2.get("level")).shouldBeEqual("ERROR");
103+
the(log2.get("level")).shouldBeEqual("INFO");
103104
the(log2.get("timestamp")).shouldNotBeNull();
104-
the(log2.get("logger")).shouldBeEqual("org.javalite.activeweb.RequestDispatcher");
105-
the(log2.get("message")).shouldBeEqual("ERROR!");
106-
107-
Map exception = (Map) log2.get("exception");
108-
the(exception.get("message")).shouldBeEqual("blah!");
109-
the(exception.get("stacktrace")).shouldContain("java.lang.RuntimeException: blah!\njava.lang.RuntimeException: blah!\n\tat app.controllers.LoggingController.error");
110-
111-
//Line 3
112-
Map log3 = JsonHelper.toMap(logs[3]);
113-
the(log3.get("level")).shouldBeEqual("INFO");
114-
the(log3.get("timestamp")).shouldNotBeNull();
115-
the(log3.get("logger")).shouldBeEqual("org.javalite.activeweb.freemarker.FreeMarkerTemplateManager");
116-
the(log3.get("message")).shouldBeEqual("rendering template: '/system/error' with layout: '/layouts/default_layout");
105+
the(log2.get("logger")).shouldBeEqual("org.javalite.activeweb.freemarker.FreeMarkerTemplateManager");
106+
the(log2.get("message")).shouldBeEqual("rendering template: '/system/error' with layout: '/layouts/default_layout");
117107
}
118108

119109
@Test

0 commit comments

Comments
 (0)