修改nginx源码,更改访问日志时间格式

说明:
修改nginx源码,实现访问日志输出的时间格式从[08/Mar/2013:09:30:58 +0800]变成了2013-03-08 12:21:03。
nginx log日志格式的话,无非是在src/core/nginx_times.c和src/http/modules/ngx_http_log_module.c 这两个文件中做修改。
nginx.conf 的默认日志格式中的时间格式使用$time_local去指定的,这是刷到acces.log中的时间格式是[08/Mar/2013:09:30:58 +0800]
此处,使用的是最新稳定版nginx-1.2.7

实现:
1.修改源码包中的src/http/modules/ngx_http_log_module.c
查找time_local,可以看到{ ngx_string(“time_local”), sizeof(“28/Sep/1970:12:00:00 +0600”) – 1, ngx_http_log_time }, { ngx_string(“time_iso8601”), sizeof(“1970-09-28T12:00:00+06:00”) – 1, 在1.2.7版本中有个time_iso8601的变量,和目标格式很接近

将{ ngx_string("time_iso8601"), sizeof("1970-09-28T12:00:00+06:00") - 1,
修改为{ ngx_string("time_iso8601"), sizeof("1970-09-28 12:00:00") - 1,

重编译后,启动,curl “localhost” 看刷log,还是不对。

2.修改src/http/modulesngx_times.c 查找iso8601
第一处:static u_char cached_http_log_iso8601[NGX_TIME_SLOTS] [sizeof(“1970-09-28T12:00:00+06:00”)];

将[sizeof("1970-09-28T12:00:00+06:00")];
修改为[sizeof("1970-09-28 12:00:00")];

 
第二处:ngx_cached_http_log_iso8601.len = sizeof(“1970-09-28T12:00:00+06:00”) – 1;

将ngx_cached_http_log_iso8601.len = sizeof("1970-09-28T12:00:00+06:00") - 1;
修改为gx_cached_http_log_iso8601.len = sizeof("1970-09-28 12:00:00") - 1;

 
第三处:p3 = &cached_http_log_iso8601[slot][0]; (void) ngx_sprintf(p3, “%4d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d”, tm.ngx_tm_year, tm.ngx_tm_mon, tm.ngx_tm_mday, tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec, tp->gmtoff < 0 ? '-' : '+', ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));

将p3 = &cached_http_log_iso8601[slot][0];  (void) ngx_sprintf(p3, "%4d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
修改为(void) ngx_sprintf(p3, "%4d-%02d-%02d %02d:%02d:%02d",

 
3.重新编译,启动,curl “localhost”,查看acees.log 就可以看到127.0.0.1 – – 2013-03-08 12:21:03 “GET / HTTP/1.1” 200 612

附录:
附录1.附了下原来的文章,怕以后找不到。比较乱,还没有整理。
第一个要修改的文件:
src/core/ngx_times.c

第一个地方:
static u_char cached_http_log_time[NGX_TIME_SLOTS]
[sizeof(“28/Sep/1970:12:00:00 +0600”)];
修改为
static u_char cached_http_log_time[NGX_TIME_SLOTS]
[sizeof(“1234567890”)]; //这里是换算成秒的位数,10位

第二个地方:
ngx_cached_http_log_time.len = sizeof(“28/Sep/1970:12:00:00 +0600”) – 1;
修改为
ngx_cached_http_log_time.len = sizeof(“1234567890”) – 1; //同上

第三个地方:函数 void ngx_time_update(time_t sec, ngx_uint_t msec) 里:
p2 = &cached_http_log_time[slot][0];

(void) ngx_sprintf(p2, “%02d/%s/%d:%02d:%02d:%02d %c%02d%02d”,
tm.ngx_tm_mday, months[tm.ngx_tm_mon – 1],
tm.ngx_tm_year, tm.ngx_tm_hour,
tm.ngx_tm_min, tm.ngx_tm_sec,
修改为
p2 = &cached_http_log_time[slot][0];
(void) ngx_sprintf(p2, “%l”,sec); //sec是函数传入的一个参数

以上都是修改请求时间按格式

第二个要修改的文件:
src/http/modules/ngx_http_log_module.c

第一步:
197行左右
修改请求时间
{ ngx_string(“time_local”), sizeof(“28/Sep/1970:12:00:00 +0600”) – 1,
修改为
{ ngx_string(“time_local”), sizeof(“1234567890”) – 1,

修改连接时间长度
{ ngx_string(“request_time”), NGX_TIME_T_LEN + 4,
修改为
{ ngx_string(“request_time”), sizeof(unsigned long long), //保证长度够用就行

第二步:static u_char * ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op) 里

return ngx_sprintf(buf,”%T.%03M”, ms / 1000, ms % 1000);
修改为
return ngx_sprintf(buf,”%T”,ms); //T是nginx自定义的一个格式串 相当于int64

OK,编译即可。

发表评论

邮箱地址不会被公开。 必填项已用*标注