Reputation: 3076
I'm looking to return per request debug information in a JSON HTTP response body. This debug information would be collected throughout my application, including data such as DB queries and how long they took, external calls made, whether certain conditions were met, general messages showing application flow etc.
I've looked into Java logging frameworks and the Play Framework, which I am using, has a built in logger that works great for logging information to files. But I can't seem to find any that handle request level logging i.e. store debug messages for this particular HTTP request to be returned with this request and then destroyed.
I could of course create a Debug class, instantiate that and pass that around throughout my application for each request, but this doesn't seem like a nice way to handle this as I would need to be passing this into a lot of classes in my application.
Are there any better ways/design patterns/libraries out there that can do what I'm looking for without having to pass a Debug object round my entire application?
Upvotes: 1
Views: 2291
Reputation: 2476
You don't need to use a logging framework for it. The best approach to return your debug info in the json response is to use the same method you are using to return the rest of the json.
This way, you can setup it to work in debug mode via -Dsomevar=debug or via an HTTP request parameter like debug=true
Upvotes: 1
Reputation: 4463
Instead of passing Debug object from layer to layer, why don't you use Http.Context? It is defined at Controller class:
public abstract class Controller extends Results implements Status, HeaderNames {
...
public static Context ctx() {
return Http.Context.current();
}
}
Usage:
ctx().args.put(key, value)
ctx().args.get(key);
Upvotes: 1
Reputation: 3076
I found a solution, the Play Framework provides request level storage for arbitrary objects using Http.Context.current().args
I'm using this HashMap to store my custom debug storage class so that I can access it throughout a request anywhere in my application.
Upvotes: 1
Reputation: 148870
It is not a common usage, so I do not think that you will find a product implementing that. You have basically 2 possibilities :
Either way, the backend part should :
And all servlets of controllers should be modified to add the logging content to the json response body.
Upvotes: 1