Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Jetty/Howto/Configure Request Logs"

< Jetty‎ | Howto
(New page: {{Jetty Howto | introduction = Request logs are a record of the requests that the server has processed. There is one entry per request received, and commonly in the standard [http://hoohoo...)
 
(Fixing package of NCSARequestLog)
 
(18 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Jetty Howto
 
{{Jetty Howto
| introduction = Request logs are a record of the requests that the server has processed. There is one entry per request received, and commonly in the standard [http://hoohoo.ncsa.uiuc.edu/docs/setup/httpd/LogOptions.html NCSA format], so you can  conveniently analyze these logs using tools such as [http://www.mrunix.net/webalizer/ webalizer].
+
| introduction =  
 +
Request logs are a record of the requests that the server has processed. There is one entry per request received, in the standard [http://www.w3.org/Daemon/User/Config/Logging.html#common-logfile-format NCSA format], so you can  conveniently analyze these logs using a tool such as [http://www.mrunix.net/webalizer/ Webalizer].
  
A standard request log entry includes the client IP address, date, method, URL, result, size, referrer and user agent, for example:
+
A standard request log entry includes the client IP address, date, method, URL, result, size, referrer and user agent. For example:
 
+
<div class="preformatted panel" style="border-width: 1px"><div class="preformattedContent panelContent">
+
  
 +
<source lang = "text">
 
  123.4.5.6 - - [27/Aug/2004:10:16:17 +0000]
 
  123.4.5.6 - - [27/Aug/2004:10:16:17 +0000]
 
   "GET /jetty/tut/XmlConfiguration.html HTTP/1.1"
 
   "GET /jetty/tut/XmlConfiguration.html HTTP/1.1"
 
   200 76793 "http://localhost:8080/jetty/tut/logging.html"
 
   200 76793 "http://localhost:8080/jetty/tut/logging.html"
 
   "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040614 Firefox/0.8"
 
   "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040614 Firefox/0.8"
 +
</source>
  
</div></div>
+
* Jetty provides a request log implementation called NCSARequestLog which supports the NCSA format in files that can be rolled over on a daily basis.
  
Jetty provides an implementation called NCSARequestLog which supports the NCSA format in files that can be rolled over on a daily basis.
+
* The [http://logback.qos.ch Logback project] offers [http://logback.qos.ch/access.html another implementation] of the RequestLog interface, providing very rich and powerful HTTP-access log functionality.
  
The [http://logback.qos.ch logback project] offers [http://logback.qos.ch/access.html another implementation] of RequestLog interface providing very rich and powerful HTTP-access log functionality,
+
* If neither of these options suits you, you can create a custom request logger by implementing Jetty's [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/NCSARequestLog.html RequestLog.java] interface and plugging it in similar to the NCSARequestLog, as shown below.
 
+
If neither of these options suits you, you can implement a custom request logger by implementing Jetty's [http://jetty.mortbay.org/jetty/jetty-6/xref/org/mortbay/jetty/RequestLog.html RequestLog.java] interface and plugging it in in similar fashion to the NCSARequestLog, as shown below.
+
  
 
==Configuring a Request Log for a Jetty Server==
 
==Configuring a Request Log for a Jetty Server==
  
To configure a single request log for the entire Jetty Server instance:
+
The following example configures a single request log for the entire Jetty Server instance:
  
<div class="code panel" style="border-width: 1px"><div class="codeContent panelContent">
+
<source lang = "xml">
 
+
<Set name="handler">
<span class="code-tag"><Set name=<span class="code-quote">"handler"</span>></span>
+
  <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
      <span class="code-tag"><New id=<span class="code-quote">"Handlers"</span> class=<span class="code-quote">"org.mortbay.jetty.handler.HandlerCollection"</span>></span>
+
    <Set name="handlers">
        <span class="code-tag"><Set name=<span class="code-quote">"handlers"</span>></span>
+
        <Array type="org.eclipse.jetty.server.Handler">
          <span class="code-tag"><Array type=<span class="code-quote">"org.mortbay.jetty.Handler"</span>></span>
+
          <Item>
            <span class="code-tag"><Item></span>
+
            <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
              <span class="code-tag"><New id=<span class="code-quote">"Contexts"</span> class=<span class="code-quote">"org.mortbay.jetty.handler.ContextHandlerCollection"</span>/></span>
+
             </Item>
             <span class="code-tag"></Item></span>
+
             <Item>
             <span class="code-tag"><Item></span>
+
               <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
               <span class="code-tag"><New id=<span class="code-quote">"DefaultHandler"</span> class=<span class="code-quote">"org.mortbay.jetty.handler.DefaultHandler"</span>/></span>
+
             </Item>
             <span class="code-tag"></Item></span>
+
             <Item>
             <span class="code-tag"><Item></span>
+
               <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"/>
               <span class="code-tag"><New id=<span class="code-quote">"RequestLog"</span> class=<span class="code-quote">"org.mortbay.jetty.handler.RequestLogHandler"</span>/></span>
+
             </Item>
             <span class="code-tag"></Item></span>
+
        </Array>
          <span class="code-tag"></Array></span>
+
    </Set>
        <span class="code-tag"></Set></span>
+
  </New>
      <span class="code-tag"></New></span>
+
</Set>
    <span class="code-tag"></Set></span>
+
 
   
 
   
 
   
 
   
    <span class="code-tag"><Ref id=<span class="code-quote">"RequestLog"</span>></span>
+
<Ref id="RequestLog">
      <span class="code-tag"><Set name=<span class="code-quote">"requestLog"</span>></span>
+
  <Set name="requestLog">
        <span class="code-tag"><New id=<span class="code-quote">"RequestLogImpl"</span> class=<span class="code-quote">"org.mortbay.jetty.NCSARequestLog"</span>></span>
+
    <New id="RequestLogImpl" class="org.eclipse.jetty.NCSARequestLog">
          <span class="code-tag"><Arg></span><span class="code-tag"><SystemProperty name=<span class="code-quote">"jetty.logs"</span> default=<span class="code-quote">"./logs"</span>/></span>/yyyy_mm_dd.request.log<span class="code-tag"></Arg></span>
+
      <Arg><SystemProperty name="jetty.logs" default="./logs"/>/yyyy_mm_dd.request.log</Arg>
          <span class="code-tag"><Set name=<span class="code-quote">"retainDays"</span>></span>90<span class="code-tag"></Set></span>
+
      <Set name="retainDays">90</Set>
          <span class="code-tag"><Set name=<span class="code-quote">"append"</span>></span>true<span class="code-tag"></Set></span>
+
      <Set name="append">true</Set>
          <span class="code-tag"><Set name=<span class="code-quote">"extended"</span>></span>false<span class="code-tag"></Set></span>
+
      <Set name="extended">false</Set>
          <span class="code-tag"><Set name=<span class="code-quote">"LogTimeZone"</span>></span>GMT<span class="code-tag"></Set></span>
+
      <Set name="LogTimeZone">GMT</Set>
        <span class="code-tag"></New></span>
+
    </New>
      <span class="code-tag"></Set></span>
+
  </Set>
    <span class="code-tag"></Ref></span>
+
</Ref>
 
+
</source>
</div></div>
+
  
 
The equivalent code is:
 
The equivalent code is:
  
<div class="code panel" style="border-width: 1px"><div class="codeContent panelContent">
+
<source lang = "java">
 +
HandlerCollection handlers = new HandlerCollection();
 +
ContextHandlerCollection contexts = new ContextHandlerCollection();
 +
RequestLogHandler requestLogHandler = new RequestLogHandler();
 +
handlers.setHandlers(new Handler[]{contexts,new DefaultHandler(),requestLogHandler});
 +
server.setHandler(handlers);
  
HandlerCollection handlers = <span class="code-keyword">new</span> HandlerCollection();
+
NCSARequestLog requestLog = new NCSARequestLog("./logs/jetty-yyyy_mm_dd.request.log");
        ContextHandlerCollection contexts = <span class="code-keyword">new</span> ContextHandlerCollection();
+
requestLog.setRetainDays(90);
        RequestLogHandler requestLogHandler = <span class="code-keyword">new</span> RequestLogHandler();
+
requestLog.setAppend(true);
        handlers.setHandlers(<span class="code-keyword">new</span> Handler[]{contexts,<span class="code-keyword">new</span> DefaultHandler(),requestLogHandler});
+
requestLog.setExtended(false);
        server.setHandler(handlers);
+
requestLog.setLogTimeZone("GMT");
+
requestLogHandler.setRequestLog(requestLog);
        NCSARequestLog requestLog = <span class="code-keyword">new</span> NCSARequestLog(<span class="code-quote">"./logs/jetty-yyyy_mm_dd.request.log"</span>);
+
</source>
        requestLog.setRetainDays(90);
+
        requestLog.setAppend(<span class="code-keyword">true</span>);
+
        requestLog.setExtended(<span class="code-keyword">false</span>);
+
        requestLog.setLogTimeZone(<span class="code-quote">"GMT"</span>);
+
        requestLogHandler.setRequestLog(requestLog);
+
  
</div></div>
+
This configures a request log in $JETTY_HOME/logs with filenames including the date. Old log files are kept for 90 days before being deleted. New entries append to existing log files, and the extended NCSA format is used in the GMT timezone.
  
This configures a request log in $JETTY_HOME/logs with filenames including the date. Old log files are kept for 90 days before being deleted. Existing log files are appended to and the extended NCSA format is used in the GMT timezone.
+
Many more configuration options are available–see [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/NCSARequestLog.html RequestLog.java].
  
There are many more configuration options available - see http://jetty.mortbay.org/apidocs/org/mortbay/jetty/NCSARequestLog.html
+
==Configuring a Request Log per Webapp==
  
==Configuring a Request Log per webapp==
+
The next example configures a request log on a per webapp basis:
  
<div class="code panel" style="border-width: 1px"><div class="codeContent panelContent">
+
<source lang = "xml">
 
+
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<span class="code-tag"><Configure class=<span class="code-quote">"org.mortbay.jetty.webapp.WebAppContext"</span>></span>
+
 
   
 
   
 
   ...
 
   ...
 
   
 
   
  <span class="code-tag"><Call name=<span class="code-quote">"addHandler"</span>></span>
+
  <Call name="addHandler">
    <span class="code-tag"><Arg></span>
+
    <Arg>
    <span class="code-tag"><New class=<span class="code-quote">"org.mortbay.jetty.handler.RequestLogHandler"</span>></span>
+
      <New class="org.eclipse.jetty.server.handler.RequestLogHandler">
      <span class="code-tag"><Set name=<span class="code-quote">"requestLog"</span>></span>
+
        <Set name="requestLog">
        <span class="code-tag"><New id=<span class="code-quote">"RequestLogImpl"</span> class=<span class="code-quote">"org.mortbay.jetty.NCSARequestLog"</span>></span>
+
            <New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog">
          <span class="code-tag"><Arg></span><span class="code-tag"><SystemProperty name=<span class="code-quote">"jetty.logs"</span> default=<span class="code-quote">"./logs"</span>/></span>/yyyy_mm_dd.test.request.log<span class="code-tag"></Arg></span>
+
            <Arg><SystemProperty name="jetty.logs" default="./logs"/>/yyyy_mm_dd.test.request.log</Arg>
          <span class="code-tag"><Set name=<span class="code-quote">"retainDays"</span>></span>90<span class="code-tag"></Set></span>
+
            <Set name="retainDays">90</Set>
          <span class="code-tag"><Set name=<span class="code-quote">"append"</span>></span>true<span class="code-tag"></Set></span>
+
            <Set name="append">true</Set>
          <span class="code-tag"><Set name=<span class="code-quote">"extended"</span>></span>false<span class="code-tag"></Set></span>
+
            <Set name="extended">false</Set>
          <span class="code-tag"><Set name=<span class="code-quote">"LogTimeZone"</span>></span>GMT<span class="code-tag"></Set></span>
+
            <Set name="LogTimeZone">GMT</Set>
        <span class="code-tag"></New></span>
+
          </New>
      <span class="code-tag"></Set></span>
+
        </Set>
    <span class="code-tag"></New></span>
+
      </New>
    <span class="code-tag"></Arg></span>
+
    </Arg>
  <span class="code-tag"></Call></span>
+
  </Call>
 
   
 
   
 
   ...
 
   ...
 
   
 
   
  <span class="code-tag"></Configure></span>
+
  </Configure>
 
+
</source>
</div>
+
| more = (optional) - links, additional references
+
  
 
}}
 
}}

Latest revision as of 14:44, 20 March 2013



Introduction

Request logs are a record of the requests that the server has processed. There is one entry per request received, in the standard NCSA format, so you can conveniently analyze these logs using a tool such as Webalizer.

A standard request log entry includes the client IP address, date, method, URL, result, size, referrer and user agent. For example:

 123.4.5.6 - - [27/Aug/2004:10:16:17 +0000]
  "GET /jetty/tut/XmlConfiguration.html HTTP/1.1"
  200 76793 "http://localhost:8080/jetty/tut/logging.html"
  "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040614 Firefox/0.8"
  • Jetty provides a request log implementation called NCSARequestLog which supports the NCSA format in files that can be rolled over on a daily basis.
  • If neither of these options suits you, you can create a custom request logger by implementing Jetty's RequestLog.java interface and plugging it in similar to the NCSARequestLog, as shown below.

Configuring a Request Log for a Jetty Server

The following example configures a single request log for the entire Jetty Server instance:

<Set name="handler">
  <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
     <Set name="handlers">
        <Array type="org.eclipse.jetty.server.Handler">
           <Item>
             <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
            </Item>
            <Item>
              <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
            </Item>
            <Item>
              <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"/>
            </Item>
        </Array>
    </Set>
  </New>
</Set>
 
 
<Ref id="RequestLog">
  <Set name="requestLog">
    <New id="RequestLogImpl" class="org.eclipse.jetty.NCSARequestLog">
      <Arg><SystemProperty name="jetty.logs" default="./logs"/>/yyyy_mm_dd.request.log</Arg>
      <Set name="retainDays">90</Set>
      <Set name="append">true</Set>
      <Set name="extended">false</Set>
      <Set name="LogTimeZone">GMT</Set>
     </New>
  </Set>
</Ref>

The equivalent code is:

HandlerCollection handlers = new HandlerCollection();
ContextHandlerCollection contexts = new ContextHandlerCollection();
RequestLogHandler requestLogHandler = new RequestLogHandler();
handlers.setHandlers(new Handler[]{contexts,new DefaultHandler(),requestLogHandler});
server.setHandler(handlers);
 
NCSARequestLog requestLog = new NCSARequestLog("./logs/jetty-yyyy_mm_dd.request.log");
requestLog.setRetainDays(90);
requestLog.setAppend(true);
requestLog.setExtended(false);
requestLog.setLogTimeZone("GMT");
requestLogHandler.setRequestLog(requestLog);

This configures a request log in $JETTY_HOME/logs with filenames including the date. Old log files are kept for 90 days before being deleted. New entries append to existing log files, and the extended NCSA format is used in the GMT timezone.

Many more configuration options are available–see RequestLog.java.

Configuring a Request Log per Webapp

The next example configures a request log on a per webapp basis:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
 
   ...
 
  <Call name="addHandler">
    <Arg>
      <New class="org.eclipse.jetty.server.handler.RequestLogHandler">
         <Set name="requestLog">
            <New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog">
            <Arg><SystemProperty name="jetty.logs" default="./logs"/>/yyyy_mm_dd.test.request.log</Arg>
            <Set name="retainDays">90</Set>
            <Set name="append">true</Set>
            <Set name="extended">false</Set>
            <Set name="LogTimeZone">GMT</Set>
          </New>
        </Set>
      </New>
    </Arg>
  </Call>
 
   ...
 
 </Configure>

Back to the top