javadatetomcataccess-log

Tomcat access log records future date


Tomcat access log is printing timestamp as “30/Dec/2020” instead of “30/12/2019” from yesterday. We are using the Timestamp format as “[%{dd/MMM/YYYY HH:mm:ss.SSS}t]” in AccessLogValve.

This has been the format for our project and working seamlessly till 29th Dec and all of sudden it started printing year as 2020 from 29th. We are using openjdk version "1.8.0_202" and Apache Tomcat/8.5.50

Any help is highly appreciated!


Solution

  • Calendar year versus Week-based year

    YYYY means week-based year. For calendar year, use yyyy.

    Read the documentation carefully when doing such work.

    ISO 8601

    I suggest doing all your logging using standard ISO 8601 formats. For a date-only value, that would be yyyy-MM-dd pattern.

    UTC

    For any given moment, the date varies around the globe by time zone. For example, the new year 2020 arrived hours earlier in Tokyo Japan than in Kolkata India, and several hours even later in Toledo Ohio US.

    So you should specify the time zone by which you want to perceive the date. If omitted, your JVM’s current default time zone is implicitly applied. Better to explicitly specified the desired/expected time zone. I don’t recall how to do this in Tomcat, and don’t have the docs available now, so you’ll have to look that up.

    Generally best to use UTC (an offset of zero hours-minutes-seconds) for logging.