Search code examples
javaloggingjettyjetty-9

How to set Jetty Request Log to default Timezone using XML


I am not able to make Jetty Request-log log requests with the local time-zone time using the following code,

   <Set name="LogTimeZone" type="java.lang.String">
       <Get class="java.util.TimeZone" name="default">
           <Get name="ID"/>
        </Get>
    </Set>

it defaults to GMT. The setLogTimeZone() (http://download.eclipse.org/jetty/9.3.9.v20160517/apidocs/org/eclipse/jetty/server/AbstractNCSARequestLog.html#setLogTimeZone-java.lang.String-) accepts a string argument and therefore should ideally work. But, it doesn't! Although, when providing the required String directly it works just fine,

  <Set name="LogTimeZone">Europe/London</Set>

the jetty log(std error) seems to be working fine. Is this a known bug?

Jetty Version used : 9.1.1


Solution

  • Take the result of getID() out of the TimeZone.getDefault() into an xml id attribute, then reference it later in the <Set> call using a <Ref> element.

    Example of how this works in Jetty XML:

    foo.xml

    <?xml version="1.0"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" 
                               "http://www.eclipse.org/jetty/configure_9_3.dtd">
    <Configure id="foo" class="java.lang.Object">
      <Get class="java.util.TimeZone" name="default">
        <Get id="defaultTimeZone" name="ID"/>
      </Get>
      <Get id="sysout" class="java.lang.System" name="out">
        <Call name="println">
          <Arg>
            <Ref refid="defaultTimeZone"/>
          </Arg>
        </Call>
      </Get>
    </Configure>
    

    Testing it on the command line ...

    $ cd /path/to/jetty-dist-9.3.9.v20160517/
    $ java -cp lib/jetty-util-9.3.9.v20160517.jar:lib/jetty-xml-9.3.9.v20160517.jar \
      org.eclipse.jetty.xml.XmlConfiguration foo.xml
    2016-07-13 17:16:25.447:INFO::main: Logging initialized @121ms
    America/Phoenix