log4j settings in Jetty

2019-09-06 09:16发布

问题:

I have a java webapp that uses log4j to log messages. This webapp is a wrapper around a jar file that I have written. When I run my webapp through the built-in Jetty from Eclipse, my JAR's logs (LOG.info(...) messages) are printed out just fine. Now, I have downloaded a standalone instance of Jetty and have run my web app from there. With the default log4j settings packaged with Jetty, I am not able to see ANY logs from my JAR. I only see the Jetty logs and the webapp's logs.

I have found the log4j.properties file under the resources folder, but am not sure what I need to change in it to get my app's logs to show. I had thought that Jetty worked out of the box with basic log4j logging.

回答1:

Jetty doesn't use log4j.

You'll need to configure your ${jetty.base} (Important Note: do not edit/modify/delete any content in ${jetty.home}) to use slf4j first, log4j second.

$ cd /path/to/myjettybase
$ java -jar /path/to/jetty-dist/start.jar --add-to-start=resources,ext

Next, go download slf4j-api.jar, slf4j-log4j##.jar (where ## is the support version of log4j you want to use), and log4j.jar and put them in the new ${jetty.base}/lib/ext directory.

Finally, put your log4j.xml in your ${jetty.base}/resources/ directory.

That's it, you've setup Jetty Distribution for log4j use.



标签: java log4j Jetty