PATCH: $time_custom supports a custom log timestamp

Wayne Davison wayne at opencoder.net
Mon Aug 24 22:02:49 MSD 2009


Hi there.  The company I work for, SourceForge, wanted the ability to
have a custom timestamp in a log file, so I created a patch for this. I
have been authorized to release it back to the nginx community.  See
what you think of my design and let me know if you have any ideas for
improvements.

The patch implements the following:

For every named log format there may be defined a custom timestamp format
that will be passed to strftime() for expansion.  This string is accessed
via $time_custom instead of $time_local.  (It defaults to expanding to an
empty string unless the user specifies a time_custom_format value for the
named log format.)  Inside the custom format one extra variable expansion
is supported:  the use of "$ms" will substitute a 3-digit milliseconds
value at that point.

For example:

    log_format  main  '$remote_addr - $remote_user [$time_custom] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    time_custom_format main '%d/%b/%Y:%H:%M:%S.$ms %z';

That defines a log format that is like $time_local except that it gets
millisecond logging resolution.  Obviously, the string doesn't need to
be quite so similar to the default, but you get the idea.

Here's the patch:

    http://opencoder.net/nginx-time_custom.patch

The code caches the strftime() expansion so that all loggers using the
named log format cause strftime() to be called at most once per second.

Please let me know what you think.

..wayne..





More information about the nginx mailing list