Access Logging in Nexus 1.3.6


September 23, 2009 By John Casey

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>
                  <New class="org.sonatype.plexus.jetty.custom. >>
                            InjectExistingPlexusListener"/>
                </Arg>
              </Call>
              <Call name="addLifeCycleListener">
                <Arg>
                  <New class="org.sonatype.plexus.jetty.custom. >>
                            DisableTagLibsListener"/>
                </Arg>
              </Call>
            </New>
          </Item>
          <Item>
            <New id="DefaultHandler"
                    class="org.mortbay.jetty.handler.DefaultHandler"/>
          </Item>
          <Item>
            <New id="RequestLog"
                    class="org.mortbay.jetty.handler.RequestLogHandler"/>
          </Item>
        </Array>
      </Set>
    </New>
  </Set>
  <Ref id="RequestLog">
    <Set name="requestLog">
      <New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog">
        <Arg>${nexus-work}/logs/yyyy_mm_dd.access.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>

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:

<Set name="handlers">
    <Array type="org.mortbay.jetty.Handler">
      <Item>

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.