How to configure jetty to put logs into an external file

20,489

Updated Instructions (June 2016)

For Jetty 9+, you'll be using a split ${jetty.home} and ${jetty.base} directory.

Note: do not edit/modify/delete/add/remove any content in ${jetty.home}. All of your configuration will reside in ${jetty.base} from now on.

Instructions as command line:

$ mkdir /path/to/mybase
$ cd /path/to/mybase

# Prepare a basic jetty.base directory
$ java -jar /path/to/jetty-dist/start.jar --add-to-start=http,deploy,resources,ext
INFO: ext             initialised in ${jetty.base}/start.ini
INFO: resources       initialised in ${jetty.base}/start.ini
INFO: server          initialised (transitively) in ${jetty.base}/start.ini
INFO: http            initialised in ${jetty.base}/start.ini
INFO: deploy          initialised in ${jetty.base}/start.ini
MKDIR: ${jetty.base}/lib
MKDIR: ${jetty.base}/lib/ext
MKDIR: ${jetty.base}/resources
MKDIR: ${jetty.base}/webapps
INFO: Base directory was modified

# Download the required jar files
$ cd /path/to/mybase/lib/ext
$ curl -O http://central.maven.org/maven2/org/slf4j/slf4j-api/1.7.21/slf4j-api-1.7.21.jar
$ curl -O http://central.maven.org/maven2/org/slf4j/slf4j-log4j12/1.7.21/slf4j-log4j12-1.7.21.jar
$ curl -O http://central.maven.org/maven2/log4j/log4j/1.2.17/log4j-1.2.17.jar

# Prepare the Jetty side logging to use slf4j
$ cd /path/to/mybase/resources
$ echo "org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog" > jetty-logging.properties

# Grab a copy of a log4j.xml to initialize things
$ cd /path/to/mybase/resources
$ curl -o log4j.xml https://gist.githubusercontent.com/joakime/13e31db59b83079be3fdc1a877de7060/raw/5c275a2a2f29445d6cdde7fcae3820da99e7234b/log4j.xml

# Start Jetty
$ cd /path/to/mybase
$ java -jar /path/to/jetty-dist/start.jar

Note: do not enable the logging module as that is strictly for Jetty's StdErrLog implementation. That logging module will capture any System.err and System.out and redirect it to a rolling log file. This capture and redirect will be in direct conflict with your log4j ConsoleAppender!

Original Instructions - Only valid on Jetty 8 (now EOL) and older

Follow these steps:

  1. create a logging directory in $JETTY_HOME/lib: $JETTY_HOME/lib/logging (this is just best practice)
  2. put log4j, slf4j-log4j and slf4j-api in that directory: e.g.: log4j-1.2.16.jar slf4j-api-1.6.1.jar slf4j-log4j12-1.6.1.jar
  3. make sure to have that new directory in jetty's classpath by adding "logging" in your $JETTY_HOME/start.ini OPTIONS line: e.g.: OPTIONS=Server,websocket,resources,ext,jsp,jdbc,logging
  4. place your log4j.properties in $JETTY_HOME/resources directory
  5. start jetty

If your log4j.properties is properly setup this should work for you. I'll take care to have such a step by step guide in the documentation.

Share:
20,489
madcyree
Author by

madcyree

Updated on July 09, 2022

Comments

  • madcyree
    madcyree almost 2 years

    How to configure jetty to put its logs into an external file?

    Manual says that I have to put slf4j into the lib directory.

    What I did was:

    • download slf4j and put slf4j-log4j12-1.7.3.jar into $JETTY_HOME$/lib .
    • download log4j and put log4j-1.2.17.jar into $JETTY_HOME$/lib
    • create a log4j configuration file. it's available from below:

    <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
      <param name="Threshold" value="DEBUG" />    
      <param name="File" value="c:/app/jetty/logs/server.log" />
      <layout class="org.apache.log4j.PatternLayout">   <param name="ConversionPattern" value="%d  %-5p  [%c{1}] %m %n" />
      </layout>
    </appender>
    <root>
      <priority value="debug" />
      <appender-ref ref="fileAppender" />
    </root> 
    
    • commented line

      "#org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog" in $JETTY_HOME$/resources/jetty-logging.properties

    Although I did everything what's described above, I am not able to see any logs in the target destinations. All log entries are still available from the console

  • Joakim Erdfelt
    Joakim Erdfelt over 11 years
    If you are using Jetty 9, you have 1 more step. exit the $JETTY_HOME/resources/jetty-logging.properties to have this 1 line ... org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.‌​Slf4jLog
  • Joakim Erdfelt
    Joakim Erdfelt over 11 years
    Documentation for this has been updated ... Example: Logging with Apache Log4j
  • Tom
    Tom about 10 years
    I am using Jetty9,followed your steps,but get java.lang.ClassNotFoundException: org.slf4j.LoggerFactory exception.