大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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…

问题原文

原帖链接 · jamesc · CC BY-SA 3.0

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.

社区高分解答

原帖链接 · Sirk Johannsen · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-08-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。