Skip Navigation
Resources Blog Access Logging in Nexus 1.3.6

Access Logging in Nexus 1.3.6

Since I've done much of the work on developing custom jetty.xml files for Nexus, it's probably natural that I was the one to research and answer the following question:

How can I turn on access logging for a Nexus instance?

It turns out this is a fairly easy thing to do. My solution below draws on the Jetty documentation for version 6.x, here.

First, copy the basic sample jetty.xml file from $basedir/conf/example/jetty.xml to $basedir/conf/jetty.xml. This file provides feature parity with the default configuration built into Nexus, so restarting Nexus with this file in place (and unchanged) should not produce any difference in behavior. This is the starting point for customizing the Jetty configuration within Nexus.

Now that we have our starting point, we need to modify the Jetty configuration to provide access logging, called request logging in Jetty. Find the XML section that begins:

<Set name="handler">

In place of this section, paste the following modification:

<Set name="handler">
    <New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">
      <Set name="handlers">
        <Array type="org.mortbay.jetty.Handler">
          <Item>
            <New id="Contexts"
                    class="org.mortbay.jetty.handler.ContextHandlerCollection">
              <Call name="addLifeCycleListener">
                <Arg>
                  &lt;New class="org.sonatype.plexus.jetty.custom.&nbsp;<b>&gt;&gt;</b>
                            InjectExistingPlexusListener"/&gt;
                &lt;/Arg&gt;
              &lt;/Call&gt;
              &lt;Call name="addLifeCycleListener"&gt;
                &lt;Arg&gt;
                  &lt;New class="org.sonatype.plexus.jetty.custom.&nbsp;<b>&gt;&gt;</b>
                            DisableTagLibsListener"/&gt;
                &lt;/Arg&gt;
              &lt;/Call&gt;
            &lt;/New&gt;
          &lt;/Item&gt;
          &lt;Item&gt;
            &lt;New id="DefaultHandler"
                    class="org.mortbay.jetty.handler.DefaultHandler"/&gt;
          &lt;/Item&gt;
          &lt;Item&gt;
            &lt;New id="RequestLog"
                    class="org.mortbay.jetty.handler.RequestLogHandler"/&gt;
          &lt;/Item&gt;
        &lt;/Array&gt;
      &lt;/Set&gt;
    &lt;/New&gt;
  &lt;/Set&gt;
  &lt;Ref id="RequestLog"&gt;
    &lt;Set name="requestLog"&gt;
      &lt;New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog"&gt;
        &lt;Arg&gt;${nexus-work}/logs/yyyy_mm_dd.access.log&lt;/Arg&gt;
        &lt;Set name="retainDays"&gt;90&lt;/Set&gt;
        &lt;Set name="append"&gt;true&lt;/Set&gt;
        &lt;Set name="extended"&gt;false&lt;/Set&gt;
        &lt;Set name="LogTimeZone"&gt;GMT&lt;/Set&gt;
      &lt;/New&gt;
    &lt;/Set&gt;
  &lt;/Ref&gt;

This replaces the single Handler instance created in the original jetty.xml example with one that uses a chain of Handler instances, the last of which is the request logger. Notice how the contents that used to reside within the <Set name="handler"> section are now included in the embedded section that starts with:

&lt;Set name="handlers"&gt;
    &lt;Array type="org.mortbay.jetty.Handler"&gt;
      &lt;Item&gt;

Also notice, later in the modification we added, that the request log will be kept in $nexus-work/logs/ with a filename consisting of the current year, month, and day, with the suffix .access.log. (Remember, $nexus-work actually points to $basedir/../sonatype-work by default.) This places our access log in the directory already used by Nexus for other types of logging, for consistency and simplified maintenance.

Picture of John Casey

Written by John Casey

John is a former Engineer at Sonatype and is a software engineering expert specializing in build process / automation (particularly for Java software). His experience emphasizes engineering, not just software development; he interested in the process of making software reliable and supportable in production environments.