您的位置:首页 > 运维架构 > Nginx

对nginx生成的日志格式的源码改造

2010-05-27 17:12 274 查看
因工作需要,要对nginx生成的访问日志的格式进行修改,
要把请求时间从字符串改成秒为单位的数字串,以及把连接时间长度从浮点格式的秒转成毫秒

参考网上的资料,修改如下:

 

第一个要修改的文件:
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,编译即可。

 
 
 乐铺活动验证
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx string 活动 工作 c