You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to log the error when my server returns a 400. This has turned out to be difficult because JsonError doesn't have a human readable toString() function. Instead, it just returns the message and ignores all the embedded errors, which is what I actually care about. I didn't even expect to have to deal with a response object here in RequestFilter, I expected to be able get the response body as a string. I'm not sure having a response object is bad, it just makes it hard to log the response. I could serialize it, but then I'm double serializing, which is inefficient.
The text was updated successfully, but these errors were encountered:
Here's my example, I'm using a @RequestFilter to log all requests. The bug happens on any 400, where res.body.toString() will only print the top level message like "Bad Request", and won't show what actually caused the request to fail. To work around this, I'm using objectMapper to serialize the body to a string before logging.
@ServerFilter(ServerFilter.MATCH_ALL_PATTERN)
classLoggingServerFilter @Inject constructor(privatevalobjectMapper:ObjectMapper) : Ordered {
privatevalLOG=LoggerFactory.getLogger("HTTP")
privateval nextTraceId =AtomicLong(1)
overridefungetOrder(): Int {
returnOrdered.HIGHEST_PRECEDENCE
}
@RequestFilter
// IMPORTANT: Execute on BLOCKING executor to avoid blocking the main Netty event loop and stalling the server.
@ExecuteOn(TaskExecutors.BLOCKING)
funlogRequest(req:HttpRequest<*>, continuation:FilterContinuation<MutableHttpResponse<*>>) {
// Log the requestval traceId = nextTraceId.getAndIncrement()
LOG.info("REQ ${req.methodName}${req.uri} traceId=$traceId")
// Run the request and log the responsevar res:MutableHttpResponse<*>
val duration = measureTime {
res = continuation.proceed()
}
// For 400 Bad Requests, log the response so we can see why it failed. This doesn't appear to significantly affect// performance; local testing times appear identical. The body is JsonError, which unfortunately// doesn't have a human-readable "toString" method, so we're using objectMapper to convert it back to JSON.val errorBody =if (res.status ==HttpStatus.BAD_REQUEST) {
val body = res.body.getOrNull()
val bodyString = objectMapper.writeValueAsString(body)
" rsp='${bodyString}'"
} else""LOG.info("RSP ${res.status.code}${req.methodName}${req.uri} time=${duration.inWholeMilliseconds}ms traceId=${traceId}${errorBody}")
}
}
Feature description
I'm trying to log the error when my server returns a 400. This has turned out to be difficult because
JsonError
doesn't have a human readabletoString()
function. Instead, it just returns themessage
and ignores all the embedded errors, which is what I actually care about. I didn't even expect to have to deal with a response object here inRequestFilter
, I expected to be able get the response body as a string. I'm not sure having a response object is bad, it just makes it hard to log the response. I could serialize it, but then I'm double serializing, which is inefficient.The text was updated successfully, but these errors were encountered: