perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.63k stars 1.56k forks source link

Easy way to enable request logging? #1091

Open mpdude opened 5 years ago

mpdude commented 5 years ago

Is there an easy way to enable request logging, say by just flipping a configuration switch?

What I‘m after is something like the „Common Log Format“, that is, a lot entry for every request with the HTTP response code, User Agent, requested URI and such.

I guess this must be possible somehow, without assembling all this information manually in a filter?

tipsy commented 5 years ago

Unfortunately there is no such switch currently. Your best bet is to use a filter.

toyg commented 5 years ago

It looks like Jetty has a facility for this. From https://www.eclipse.org/jetty/documentation/9.4.x/configuring-jetty-request-logs.html

NCSARequestLog requestLog = new NCSARequestLog("/var/logs/jetty/jetty-yyyy_mm_dd.request.log");
requestLog.setAppend(true);
requestLog.setExtended(false);
requestLog.setLogTimeZone("GMT");
requestLog.setLogLatency(true);
requestLog.setRetainDays("90");

server.setRequestLog(requestLog);

You would have to add this in a custom JettyServerFactory.

skyghis commented 5 years ago

There is a sample to add a NCSA logger on Spark 2.7.2 in this repo : https://github.com/ygaller/spark-with-request-logger

ivoanjo commented 2 years ago

Would be really nice to have this as a default method that one can call. It's kinda weird to need to add this much code to turn on request logging :/