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

因工作需要,要对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,编译即可。

 
 
 乐铺活动验证
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值