Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spark web framework logging requests and responses

I'm using the Spark web framework to develop a REST API. Is there a way to automatically log all incoming requests and outgoing responses (query params, headers, status codes, etc) or do I need to manually add logging for each one of the handlers?

The Spark documentation has nothing on this subject.

Thanks.

like image 305
Henrique Avatar asked May 04 '15 20:05

Henrique


1 Answers

Here's was my workaround.

private static String requestInfoToString(Request request) {
    StringBuilder sb = new StringBuilder();
    sb.append(request.requestMethod());
    sb.append(" " + request.url());
    sb.append(" " + request.body());
    return sb.toString();
}

public static void main(String[] args) {
    // Bunch of handlers

    before((request, response) -> {
        log.info(requestInfoToString(request));
    });
}
like image 132
Joe Avatar answered Sep 26 '22 07:09

Joe