对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,编译即可。
乐铺活动验证
要把请求时间从字符串改成秒为单位的数字串,以及把连接时间长度从浮点格式的秒转成毫秒
参考网上的资料,修改如下:
第一个要修改的文件:
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的日志格式
- 实例说明:源码包生成RPM格式
- Linux下nginx生成日志自动切割
- nginx(2) 日志文件的格式和存储路径
- nginx日志切割,日志格式,静态文件不记录日志,配置缓存
- nginx服务aceess.log日志参数切割格式(咋个办呢 zgbn)
- nginx 日志格式
- leveldb研究2- 存储分析,数据库日志文件格式,数据文件的格式和生成
- 实例说明:源码包生成RPM格式
- Nginx 日志格式剖析
- nginx 用log_format设置日志格式
- elk系列3之通过json格式采集Nginx日志
- nginx之日志格式
- Nginx 日志改成 JSON 格式
- ELK 之三:Kibana 使用与Tomcat、Nginx 日志格式处理
- nginx日志格式说明
- nginx自定义日志格式
- nginx 【logformat】日志格式
- [nginx] 巧用NGINX日志格式来分析网站访问速度与瓶颈
- Nginx 日志改成 JSON 格式的方法