Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / Java
Tip/Trick

Log4J for Jetty

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
16 Jun 2015CPOL2 min read 11.7K  
Configuring Log4J for Jetty

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:

  1. logs folder is created in $appbase
  2. resources folder is created in $appbase
  3. Adds the command “—module=logging” in $appbase/start.ini.
  4. 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.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect HCL Technologies
India India
Shashi is working as Lead Solutions Architect at Uptick Entertainment Inc. His main area of interest is designing and distributed systems. He has worked in domains like Telecom applications, Gaming and Banking.

Comments and Discussions

 
-- There are no messages in this forum --