Introduction
By default, Jetty server emits out the logs to console. The console output rolls over very fast and does not help you if you run into some issue and want to go through the console output. This calls for directing the console output to some log files. Jetty has its own logging mechanism and does not use the existing logging frameworks like Log4J etc. However, these frameworks can be configured to work with Jetty. I tried to configure Log4J, going thru various resources avaialble on the internet, but somehow, it did not work for me. I struggled a bit before I could make it work for my windows and linux installation. This trivia quikly outlines the steps to configure Log4J for your jetty server which I learnt that would work.
Steps
Assuming your application would be deployed to appbase. This is a new feature in jetty called jetty.base mechanism, wherein you can separate out the jetty instances, configurations specific to your applications, it’s logs and your application from the jetty’s distributed libraries and configurations. In our case, appbase becomes your jetty.base. It would be helpful to note that jetty.home denotes the path where jetty is installed i.e. path to the folder where lib, start.ini etc. exist.
To configure log4j logging for the jetty logs, perform the following steps:
- Need to add the logging module in your appbase. Run the following command from within appbase directory.
appbase> java –jar $jetty.home/start.jar –add-to-start=logging
This would perform the following tasks:
- logs folder is created in $appbase
- resources folder is created in $appbase
- Adds the command “—module=logging” in $appbase/start.ini.
- Download the required log4j and sl4lj libraries - slf4j-api, slf4j-log4j, and log4j into the folder $jetty.base/lib/logging.
I found that it performed only first two steps. I had to download the log4j & sl4lj libraries manually. If any of the above is not done, you may do it manually.
- Put log4j.properties or log4j.xml file in $appbase/resources.
- Uncomment the following line from $appbase/start.ini to enable logging thru slf4j
-Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog
- Now run the jetty server from within your appbase, it would create the logs as per the log4.properties or log4j.xml file configurations in $appbase/resources.