JAX-RS and CXF: disable stacktraces on errors
Using CXF as JAX-RS implementation you will get this kind of output when an erroneous request happens:
[21:35:56.095][WARN ][io-38802-exec-1][interceptor.JAXRSInInterceptor] No root resource matching request path /api/missing has been found, Relative Path: /missing. Please enable FINE/TRACE log level for more details.
[21:35:56.108][WARN ][io-38802-exec-1][.WebApplicationExceptionMapper] javax.ws.rs.NotFoundException: HTTP 404 Not Found
at org.apache.cxf.jaxrs.utils.SpecExceptions.toNotFoundException(SpecExceptions.java:89)
at org.apache.cxf.jaxrs.utils.ExceptionUtils.toNotFoundException(ExceptionUtils.java:129)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:169)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
[...]
This is great and implied by JAX-RS specification but 4xx are actually not "errors" for the server, just one conditional path and even 500 will not be really helpful compared a deeper logging integration like a CDI interceptor for instance which can enrich the message from some more context.
Long story short: you will often desire to disable this output.
This log is coming from org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper. Luckily this exception mapper has some configuration you can use to make it more silent. Concretely preventing him to log the full stack will already make the output more usable:
[21:39:17.505][WARN ][io-36857-exec-1][interceptor.JAXRSInInterceptor] No root resource matching request path /api/missing has been found, Relative Path: /missing. Please enable FINE/TRACE log level for more details.
If you dig a little bit you will also realize this log is coming from the CXF resource resolution runtime and not the exception mapper so mission solved if this is the whole output.
How to achieve it? Simply disabling WARNING level for exception caught by the default JAX-RS exception mapper and log them at FINE level.
To do it just set printStackTrace to false in the exception mapper and register it.
For TomEE or Meecrowave here is a sample:
import org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper;
import javax.enterprise.context.Dependent;
import javax.ws.rs.ext.Provider;
@Provider
// scope is not that important but used to mark it as scanned in annotated mode
@Dependent
public class DefaultExceptionMapper extends WebApplicationExceptionMapper {
public DefaultExceptionMapper() {
setPrintStackTrace(false);
}
}
And here it is, the CDI/EE stack will scan and install this mapper instead of the default one. Now the exception will no more be logged. In Tomcat* servers (including TomEE and Meecrowave) you are more than likely using the access log to catch the 40x and 50x errors so this avoids to pollute your container logs for no real gain.
From the same author:
In the same category: