Nginx logging: Is there a way to log request time in milliseconds since Unix epoch

log-filesloggingnginx

I'm aware of the $msec logging parameter which gives the request time as seconds since Unix epoch with millisecond granularity. However, the format of this includes a period '.' e.g. '1407233265.472' and this is a problem for the import function that I'm using to parse the log files.

Is there a way (log parameter or plugin) that will allow the request time to be recorded in total milliseconds? e.g. '1407233265472'. Am happy to compile nginx as required.

Best Answer

There are 2 ways to achieve this. The first one is dirty and I wont recommend it - it's the fastest way though:

if ($msec ~ "(.*)\.(.*)") {
    set $epoch_millis = "$1$2"
}

This is much cleaner but requires lua:

set_by_lua_block $epoch_millis { return string.gsub(ngx.var.msec, "%.", "") }

then just log $epoch_millis