I have a spring application serving web requests. Each request method looks like this
@RequestMapping(value = someUri, method = RequestMethod.POST)
Response someUriProcessor (SomeRequestModelMethod request) throws Exception {
try (JsonLogger logger = new JsonLogger(request, loggingTest1Uri)){
// get actual result
Response result = getSomeResultMethod(request);
// feed result to special logging object
logger.setResult (result);
// return result to client
return result;
}
}
JsonLogger class generate JSON object in close method and log it via standard slf4j logging framework which is uploaded to log analyzer tool (Splunk), it logs request, response, response time and so on.
During execution of getSomeResultMethod which does a lot of work lots of useful logs are generated using standard slf4j with logback binding in production. I want these logs to be included in JsonLogger without touching all the underlying classes (there are LOTS of them), say using method JsonLogger.appendToResultLog(String txt). My first thought is create my own wrapping slf4j binding, unwind stacktrace on each call to logger.info, logger.error and so on methods using reflection up to rest controller method and append log string JsonLogger in it.
I have strong feeling that I'm inventing a bicycle. Are there any more-or-less standard way to achieve results required?