blob: 6e4e58928da0123ccfe5ab6c0680efc53d6f2ac0 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example: Logging with Logback</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="jetty, servlet-api, cometd, http, spdy, eclipse, maven, java, software"><link rel="home" href="index.html" title="Jetty : The Definitive Reference"><link rel="up" href="configuring-logging.html" title="Chapter&nbsp;9.&nbsp;Jetty Logging"><link rel="prev" href="example-logging-java-util-logging.html" title="Example: Logging with Java's java.util.logging"><link rel="next" href="example-slf4j-multiple-loggers.html" title="Example: Capturing Multiple Logging frameworks with Slf4j"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><script type="text/javascript" src="js/shCore.js"></script><script type="text/javascript" src="js/shBrushJava.js"></script><script type="text/javascript" src="js/shBrushXml.js"></script><script type="text/javascript" src="js/shBrushBash.js"></script><script type="text/javascript" src="js/shBrushJScript.js"></script><script type="text/javascript" src="js/shBrushSql.js"></script><script type="text/javascript" src="js/shBrushProperties.js"></script><script type="text/javascript" src="js/shBrushPlain.js"></script><link type="text/css" rel="stylesheet" href="css/shCore.css"><link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"><link type="text/css" rel="stylesheet" href="css/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small">
Version: 9.0.6.v20130930</span></td><td style="width: 50%"><script type="text/javascript"> (function() {
var cx = '016459005284625897022:obd4lsai2ds';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script><gcse:search></gcse:search></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Example: Logging with Logback</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="example-logging-java-util-logging.html"><i class="icon-chevron-left"></i> Previous</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;9.&nbsp;Jetty Logging<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="example-slf4j-multiple-loggers.html">Next <i class="icon-chevron-right"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/support.jsp">Contact the core Jetty developers at
<span class="website">www.webtide.com</span></a></h5><p>
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ...
scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-logging-logback"></a>Example: Logging with Logback</h2></div></div></div><p>This page describes how to Configure Jetty for Logging with
<code class="literal"><a class="link" href="http://logback.qos.ch/" target="_top">Logback</a></code>.</p><p>The basic configuration for Logging with Logback is to use <a class="link" href="http://slf4j.org/manual.html" target="_top">Slf4j</a> and the <a class="link" href="http://logback.qos.ch/" target="_top">Logback Implementation for
Slf4j</a>.</p><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="d0e6680"></a>The Required JARs</h3></div></div></div><p>There will be 3 JARs you will need to download</p><div class="variablelist"><dl><dt><span class="term"><code class="filename"><a class="link" href="http://repo1.maven.org/maven2/ch/qos/logback/logback-classic/1.0.7/logback-classic-1.0.7.jar" target="_top">logback-classic-1.0.7.jar</a></code></span></dt><dd><p>This is the underlying logging framework and adaption layer
for Slf4j</p></dd><dt><span class="term"><code class="filename"><a class="link" href="http://repo1.maven.org/maven2/ch/qos/logback/logback-core/1.0.7/logback-core-1.0.7.jar" target="_top">logback-core-1.0.7.jar</a></code></span></dt><dd><p>The common implementation classes for the Logback
project.</p></dd><dt><span class="term"><code class="filename"><a class="link" href="http://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.6.6/slf4j-api-1.6.6.jar" target="_top">slf4j-api-1.6.6.jar</a></code></span></dt><dd><p>This is the basic Slf4j API that Jetty's Slf4jLog
implementation uses.</p></dd></dl></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="d0e6711"></a>Configuring Server Classpath</h3></div></div></div><p>In order for the Server to use these new JARs you will need to put
them into place and tell Jetty to load them on each startup.</p><p>Create a ${jetty.home}/lib/logging/ directory and place the 3 files
you downloaded there.</p><div class="screenexample"><pre class="screen">[jetty-distribution-9.0.6.v20130930]$ ls -l <span class="bold"><strong>lib/logging/</strong></span>
total 632
-rw-rw-r-- 1 jetty jetty 251679 Mar 12 14:46 <span class="bold"><strong>logback-classic-1.0.7.jar</strong></span>
-rw-rw-r-- 1 jetty jetty 364399 Mar 12 14:46 <span class="bold"><strong>logback-core-1.0.7.jar</strong></span>
-rw-rw-r-- 1 jetty jetty 26176 Mar 12 14:46 <span class="bold"><strong>slf4j-api-1.6.6.jar</strong></span>
</pre></div><p>Next, we need to edit the ${jetty.home}/start.ini to get jetty to
load these logging files into the Server classpath.</p><p>Edit the start.ini and check for the following entries. (Add them to
the end if missing)</p><script type="syntaxhighlighter" class="brush: properties;toolbar: false; highlight: [2]">
<![CDATA[# Adding lib/logging to server classpath
OPTIONS=logging
]]>
</script><p>Now verify that the 3 jars will be loaded by server classpath</p><div class="screenexample"><pre class="screen">[jetty-distribution-9.0.6.v20130930]$ <span class="bold"><strong>java -jar start.jar --version</strong></span> | grep logging
Active Options: [Server, client, ext, jmx, jsp, <span class="bold"><strong>logging</strong></span>, resources, websocket]
18: 1.0.7 | ${jetty.home}/lib/logging/<span class="bold"><strong>logback-classic-1.0.7.jar</strong></span>
19: 1.0.7 | ${jetty.home}/lib/logging/<span class="bold"><strong>logback-core-1.0.7.jar
</strong></span>20: 1.6.6 | ${jetty.home}/lib/logging/<span class="bold"><strong>slf4j-api-1.6.6.jar</strong></span>
</pre></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="d0e6757"></a>Configuring Server Logging</h3></div></div></div><p>If you have a
<code class="filename">${jetty.home}/resources/jetty-logging.properties</code>,
make sure it contains just the following</p><p><script type="syntaxhighlighter" class="brush: properties;toolbar: false">
<![CDATA[# Configure Jetty for SLf4j Logging
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog]]>
</script></p><p>This will tell Jetty to use the Slf4jLog implementation for its
internal Logging.</p></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="d0e6770"></a>Configuring Logback</h3></div></div></div><p>You'll need a
<code class="filename">${jetty.home}/resources/logback.xml</code> file to define
what you want Logback to do. It will be automatically located and loaded
on Logging startup.</p><p>Example <code class="filename">${jetty.home}/resources/logback.xml</code>
file</p><p><script type="syntaxhighlighter" class="brush: xml;toolbar: false">
<![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<!--
Example LOGBACK Configuration File
http://logback.qos.ch/manual/configuration.html
-->
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>\${jetty.home}/logs/jetty.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>jetty_%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
]]>
</script></p></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="d0e6786"></a>Scripted Installation of Logback</h3></div></div></div><script type="syntaxhighlighter" class="brush: bash;toolbar: false">
<![CDATA[#!/bin/bash
# -------------------------------------------
# FOR DEMONSTRATION PURPOSES ONLY
# USE AT YOUR OWN RISK
# -------------------------------------------
if [ ! -f start.ini ] ; then
echo "ERROR: Not in \${jetty.home} directory"
exit -1
fi
if [ -d lib/logging ] ; then
echo "ERROR: Existing lib/logging directory exists."
echo " Remove any existing logging implementations"
echo " before running this script, as conflicting logging"
echo " implementations can cause problems."
echo " Then remove the lib/logging directory entirely."
exit -1
fi
mkdir lib/logging
pushd lib/logging
curl -O http://repo1.maven.org/maven2/ch/qos/logback/logback-classic/1.0.7/logback-classic-1.0.7.jar
curl -O http://repo1.maven.org/maven2/ch/qos/logback/logback-core/1.0.7/logback-core-1.0.7.jar
curl -O http://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.6.6/slf4j-api-1.6.6.jar
popd
if (grep -E "OPTIONS=.*logging.*" start.ini)
then
echo "Logging already present in start.ini"
else
echo "Adding logging to start.ini"
echo "" >> start.ini
echo "# Adding lib/logging to server classpath" >> start.ini
echo "OPTIONS=logging" >> start.ini
fi
cat << EOFP > resources/jetty-logging.properties
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog
EOFP
if [ -f resources/logback.xml ]
then
echo "Using existing resources/logback.xml"
else
echo "Creating new resources/logback.xml"
cat << EOFL > resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Example LOGBACK Configuration File
http://logback.qos.ch/manual/configuration.html
-->
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>\${jetty.home}/logs/jetty.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>jetty_%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
EOFL
fi
]]>
</script></div></div><script type="text/javascript">
SyntaxHighlighter.all()
</script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="example-logging-java-util-logging.html"><i class="icon-chevron-left"></i> Previous</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="configuring-logging.html"><i class="icon-chevron-up"></i> Top</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="example-slf4j-multiple-loggers.html">Next <i class="icon-chevron-right"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Example: Logging with Java's java.util.logging&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="icon-home"></i> Home</a></td><td width="40%" align="right" valign="top">&nbsp;Example: Capturing Multiple Logging frameworks with Slf4j</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout">
See an error or something missing?
<span class="callout"><a href="http://github.com/jetty-project/jetty-documentation">Contribute to this documentation at
<span class="website"><i class="icon-github"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2013-10-30T13:20:21-05:00)</i></span></div></p><script xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1149868-7']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script></body></html>