ngx_http_log_module.c文件

在nginx.conf中相关的指令为:

log_format                  proxyformat "$time_iso8601 $remote_addr:$remote_port $server_addr:$server_port $upstream_addr $request_time $upstream_response_time $status $upstream_status $request_length $body_bytes_sent \"$request_method $scheme://$http_host$request_uri $server_protocol\" \"$http_referer\" \"$http_user_agent\"";access_log                  syslog:facility=local3,severity=info,server=127.0.0.1:514,tag=tengine proxyformat;先关注log模块的初始化static ngx_http_module_t  ngx_http_log_module_ctx = {NULL,                                  /* preconfiguration */ngx_http_log_init,                     /* postconfiguration */ngx_http_log_create_main_conf,         /* create main configuration */NULL,                                  /* init main configuration */NULL,                                  /* create server configuration */NULL,                                  /* merge server configuration */ngx_http_log_create_loc_conf,          /* create location configuration */ngx_http_log_merge_loc_conf            /* merge location configuration */};static void *ngx_http_log_create_main_conf(ngx_conf_t *cf){ngx_http_log_main_conf_t  *conf;ngx_http_log_fmt_t  *fmt;conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));if (conf == NULL) {return NULL;}// 初始化formatif (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))!= NGX_OK){return NULL;}return conf;
}static ngx_int_tngx_http_log_init(ngx_conf_t *cf){// 这块有判断是否使用默认的combine的逻辑,在tengine中,自定义了一波,所以不care// 将handler加入log阶段cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);if (h == NULL) {return NGX_ERROR;}*h = ngx_http_log_handler;return NGX_OK;
}而在log_format指令的处理函数中,将这个fmt给保存下来
static char *
ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){// 获取loc_main_conf
ngx_http_log_main_conf_t *lmcf = conf;
// 初始化fmt
fmt = lmcf->formats.elts;
fmt = ngx_array_push(&lmcf->formats);if (fmt == NULL) {return NGX_CONF_ERROR;}// 赋值namefmt->name = value[1];
// 初始化flushes和opsfmt->flushes = ngx_array_create(cf->pool, 4, sizeof(ngx_int_t));if (fmt->flushes == NULL) {return NGX_CONF_ERROR;}fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));if (fmt->ops == NULL) {return NGX_CONF_ERROR;}// 最后把整个日志格式编译
return ngx_http_log_compile_format(cf, fmt->flushes, fmt->ops, cf->args, 2);}static char *
ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s){
// 遍历所有的args
for ( /* void */ ; s < args->nelts; s++) {// 在已有的ngx_http_log_vars中看有没有指定的日志格式
for (v = ngx_http_log_vars; v->name.len; v++) {if (v->name.len == var.len&& ngx_strncmp(v->name.data, var.data, var.len) == 0){
// 将对应的方法,长度赋值给对应op,等待写入op->len = v->len;op->getlen = NULL;op->run = v->run;op->data = 0;goto found;}}// 没有的话则调用该函数if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {return NGX_CONF_ERROR;}if (len) {op->len = len;op->getlen = NULL;// 如果长度小的话,就直接用data来记录if (len <= sizeof(uintptr_t)) {op->run = ngx_http_log_copy_short;op->data = 0;// 记录datawhile (len--) {op->data <<= 8;op->data |= data[len];}// 比较长的话,记录成指针来指向,函数也是获取指针来进行获取值} else {op->run = ngx_http_log_copy_long;p = ngx_pnalloc(cf->pool, len);if (p == NULL) {return NGX_CONF_ERROR;}ngx_memcpy(p, data, len);op->data = (uintptr_t) p;}}}static ngx_int_tngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,ngx_str_t *value){ngx_int_t  index;index = ngx_http_get_variable_index(cf, value);op->len = 0;op->getlen = ngx_http_log_variable_getlen;op->run = ngx_http_log_variable;op->data = index;}// 而这些可以进入日志中的参数,则是在core_preconfiguration函数中的ngx_http_variables_add_core_vars函数ngx_int_tngx_http_variables_add_core_vars(ngx_conf_t *cf){cmcf->variables_keys = ngx_pcalloc(cf->temp_pool,sizeof(ngx_hash_keys_arrays_t));for (cv = ngx_http_core_variables; cv->name.len; cv++) {v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t));if (v == NULL) {return NGX_ERROR;}*v = *cv;rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v,NGX_HASH_READONLY_KEY);if (rc == NGX_OK) {continue;}if (rc == NGX_BUSY) {ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,"conflicting variable name \"%V\"", &v->name);}return NGX_ERROR;}接下来可以看下整个的ngx_http_core_variablesstatic ngx_http_variable_t  ngx_http_core_variables[] = {{ ngx_string("http_host"), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.host), 0, 0 },{ ngx_string("http_user_agent"), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.user_agent), 0, 0 },{ ngx_string("http_referer"), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.referer), 0, 0 },#if (NGX_HTTP_GZIP){ ngx_string("http_via"), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.via), 0, 0 },#endif#if (NGX_HTTP_X_FORWARDED_FOR){ ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_headers,offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 },
#endif{ ngx_string("http_cookie"), NULL, ngx_http_variable_cookies,offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 },{ ngx_string("content_length"), NULL, ngx_http_variable_content_length,0, 0, 0 },{ ngx_string("content_type"), NULL, ngx_http_variable_header,offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 },{ ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 },{ ngx_string("binary_remote_addr"), NULL,ngx_http_variable_binary_remote_addr, 0, 0, 0 },{ ngx_string("remote_addr"), NULL, ngx_http_variable_remote_addr, 0, 0, 0 },{ ngx_string("remote_port"), NULL, ngx_http_variable_remote_port, 0, 0, 0 },{ ngx_string("proxy_protocol_addr"), NULL,ngx_http_variable_proxy_protocol_addr, 0, 0, 0 },{ ngx_string("server_addr"), NULL, ngx_http_variable_server_addr, 0, 0, 0 },{ ngx_string("server_port"), NULL, ngx_http_variable_server_port, 0, 0, 0 },{ ngx_string("server_protocol"), NULL, ngx_http_variable_request,offsetof(ngx_http_request_t, http_protocol), 0, 0 },{ ngx_string("scheme"), NULL, ngx_http_variable_scheme, 0, 0, 0 },{ ngx_string("https"), NULL, ngx_http_variable_https, 0, 0, 0 },{ ngx_string("full_request"), NULL, ngx_http_variable_full_request,0, 0, 0 },{ ngx_string("normalized_request"), NULL, ngx_http_variable_normalized_request,0, 0, 0 },{ ngx_string("request_uri"), NULL, ngx_http_variable_request,offsetof(ngx_http_request_t, unparsed_uri), 0, 0 },{ ngx_string("raw_uri"), NULL, ngx_http_variable_request,offsetof(ngx_http_request_t, raw_uri), 0, 0 },{ ngx_string("uri"), NULL, ngx_http_variable_request,offsetof(ngx_http_request_t, uri),NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("document_uri"), NULL, ngx_http_variable_request,offsetof(ngx_http_request_t, uri),NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("request"), NULL, ngx_http_variable_request_line, 0, 0, 0 },{ ngx_string("document_root"), NULL,ngx_http_variable_document_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("realpath_root"), NULL,ngx_http_variable_realpath_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("query_string"), NULL, ngx_http_variable_request,offsetof(ngx_http_request_t, args),NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("args"),ngx_http_variable_set_args,ngx_http_variable_request,offsetof(ngx_http_request_t, args),NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("is_args"), NULL, ngx_http_variable_is_args,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("request_filename"), NULL,ngx_http_variable_request_filename, 0,NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("server_name"), NULL, ngx_http_variable_server_name, 0, 0, 0 },{ ngx_string("request_method"), NULL,ngx_http_variable_request_method, 0,NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("remote_user"), NULL, ngx_http_variable_remote_user, 0, 0, 0 },{ ngx_string("bytes_sent"), NULL, ngx_http_variable_bytes_sent,0, 0, 0 },{ ngx_string("body_bytes_sent"), NULL, ngx_http_variable_body_bytes_sent,0, 0, 0 },{ ngx_string("pipe"), NULL, ngx_http_variable_pipe,0, 0, 0 },{ ngx_string("request_completion"), NULL,ngx_http_variable_request_completion,0, 0, 0 },{ ngx_string("request_body"), NULL,ngx_http_variable_request_body,0, 0, 0 },{ ngx_string("request_body_file"), NULL,ngx_http_variable_request_body_file,0, 0, 0 },{ ngx_string("request_length"), NULL, ngx_http_variable_request_length,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("request_time"), NULL, ngx_http_variable_request_time,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("status"), NULL,ngx_http_variable_status, 0,NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("sent_http_content_type"), NULL,ngx_http_variable_sent_content_type, 0, 0, 0 },{ ngx_string("sent_http_content_length"), NULL,ngx_http_variable_sent_content_length, 0, 0, 0 },{ ngx_string("sent_http_location"), NULL,ngx_http_variable_sent_location, 0, 0, 0 },{ ngx_string("sent_http_last_modified"), NULL,ngx_http_variable_sent_last_modified, 0, 0, 0 },{ ngx_string("sent_http_connection"), NULL,ngx_http_variable_sent_connection, 0, 0, 0 },{ ngx_string("sent_http_keep_alive"), NULL,ngx_http_variable_sent_keep_alive, 0, 0, 0 },{ ngx_string("sent_http_transfer_encoding"), NULL,ngx_http_variable_sent_transfer_encoding, 0, 0, 0 },{ ngx_string("sent_http_cache_control"), NULL, ngx_http_variable_headers,offsetof(ngx_http_request_t, headers_out.cache_control), 0, 0 },{ ngx_string("limit_rate"), ngx_http_variable_request_set_size,ngx_http_variable_request_get_size,offsetof(ngx_http_request_t, limit_rate),NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("connection"), NULL,ngx_http_variable_connection, 0, 0, 0 },{ ngx_string("connection_requests"), NULL,ngx_http_variable_connection_requests, 0, 0, 0 },{ ngx_string("nginx_version"), NULL, ngx_http_variable_nginx_version,0, 0, 0 },{ ngx_string("hostname"), NULL, ngx_http_variable_hostname,0, 0, 0 },{ ngx_string("pid"), NULL, ngx_http_variable_pid,0, 0, 0 },{ ngx_string("msec"), NULL, ngx_http_variable_msec,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("dollar"), NULL, ngx_http_variable_dollar,0, 0, 0 },{ ngx_string("host_comment"), NULL, ngx_http_variable_host_comment,0, 0, 0 },{ ngx_string("unix_time"), NULL, ngx_http_variable_unix_time,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("year"), NULL, ngx_http_variable_year,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("year2"), NULL, ngx_http_variable_year2,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("month"), NULL, ngx_http_variable_month,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("day"), NULL, ngx_http_variable_day,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("hour"), NULL, ngx_http_variable_hour,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("hour12"), NULL, ngx_http_variable_hour12,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("minute"), NULL, ngx_http_variable_minute,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("second"), NULL, ngx_http_variable_second,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("time_iso8601"), NULL, ngx_http_variable_time_iso8601,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("time_local"), NULL, ngx_http_variable_time_local,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("time_http"), NULL, ngx_http_variable_time_http,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },#if (NGX_HAVE_TCP_INFO){ ngx_string("tcpinfo_rtt"), NULL, ngx_http_variable_tcpinfo,0, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("tcpinfo_rttvar"), NULL, ngx_http_variable_tcpinfo,1, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("tcpinfo_snd_cwnd"), NULL, ngx_http_variable_tcpinfo,2, NGX_HTTP_VAR_NOCACHEABLE, 0 },{ ngx_string("tcpinfo_rcv_space"), NULL, ngx_http_variable_tcpinfo,3, NGX_HTTP_VAR_NOCACHEABLE, 0 },#endif{ ngx_null_string, NULL, NULL, 0, 0, 0 }};这块是在ngx_http_variables_init_vars中初始化ngx_int_tngx_http_variables_init_vars(ngx_conf_t *cf){cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);v = cmcf->variables.elts;key = cmcf->variables_keys->keys.elts;for (i = 0; i < cmcf->variables.nelts; i++) {for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {av = key[n].value;if (v[i].name.len == key[n].key.len&& ngx_strncmp(v[i].name.data, key[n].key.data, v[i].name.len)== 0){// 将get_handler和data赋值过来v[i].get_handler = av->get_handler;v[i].data = av->data;}

nginx日志模块ngx_http_log_module源码分析相关推荐

  1. java日志模块_Java源码初探_logging日志模块实现

    一.用途 程序中记录日志,打印到控制台.文件等方式,记录过程可根据日志级别做筛选,日志格式可以自定义. 大概结构如下所示: 简要说明各个模块: (1) LogManager:管理LoggerConte ...

  2. nginx关于域名解析的源码分析

    在nginx中,nginx需要频繁进行域名解析的过程做了自己的优化,使用了自己的一套域名解析过程,并做了缓存处理.我们可以设置DNS解析服务器的地址,即通过resolver指令来设置DNS服务器的地址 ...

  3. nginx关闭请求处理ngx_http_finalize_request源码分析

    ngx_http_finalize_request 各个http模块在执行完某个操作都需要调用这个函数,来把请求的引用计数减去1,当引用计数为0时才会真正释放一个请求.这个函数实现比较复杂,考虑了各种 ...

  4. nginx upstream完整交互源码分析

    nginx访问上游服务器的几个阶段: 启动upstream 连接上游服务器 向上游发送请求 接收上游响应包头和包体 结束请求 主要数据结构:ngx_http_upstream_t和ngx_http_u ...

  5. Spring的日志模块-spring-jcl源码解析以及Java的日志框架

    1. spring-jcl模块结构 可以看到,这个模块的包名是apache的commons.logging,因为这是spring团队对common.logging 进行了重写,所以包名还保留之前的ap ...

  6. nginx域名解析部分完整源码分析

    nginx域名解析流程 域名查询的函数接口介绍 在使用同步IO的情况下,调用gethostbyname()或者gethostbyname_r()就可以根据域名查询到对应的IP地址, 但因为可能会通过网 ...

  7. 自定义日志_Dubbo自定义日志拦截器源码分析

    需求场景 在使用Dubbo搭建的分布式项目中,服务层代码调用是这样的: @GetMapping(value = "/info")2 public BaseResult userIn ...

  8. Fabric源码分析-共识模块

    正好这些天要有一个需求要帮客户魔改Fabric-v0.6,把一些hyperchain的高级特性移植过去,借此机会把之前看过的源码在梳理一下. 下面就是对Fabric共识模块的源码分析和梳理,代码都是以 ...

  9. Spring AOP 源码分析 - 拦截器链的执行过程

    1.简介 本篇文章是 AOP 源码分析系列文章的最后一篇文章,在前面的两篇文章中,我分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...

最新文章

  1. 一起学WP7 XNA游戏开发(八. 让3d model动起来)
  2. AI视频行为分析系统项目复盘——技术篇2:视频流GPU硬解码
  3. WordPress实践:自定义theme(01)
  4. 鼠标滚动缩放图片效果
  5. 006_表的CRUD的操作
  6. 在SAP云平台的CloudFoundry环境下消费ABAP On-Premise OData服务
  7. 【转】用fo-dicom实现print scu的注意事项!!!!!!!!!
  8. Linux : shell基础(慕课网Linux达人养成计划课程笔记)
  9. JDK1.8源码下载及获取、导入IDEA阅读、配置JDK源码
  10. 【BZOJ1434】【codevs2348】染色游戏,博弈
  11. 银河麒麟4安装MySQL8_银河麒麟4.0.2安装教程-电脑系统安装手册
  12. 第一个 Shell脚本
  13. DevExpress控件TExtLookupComboBox实现多列模糊匹配输入的方法
  14. Maven的dependencies与dependencyManagement用法区别
  15. 日期转换和日历的使用方法
  16. 用Python爬取28010条《隐秘的角落》评论,我发现了这些...
  17. 高中数学关于计算机的知识,高中数学必修三-算法概念基础知识解读
  18. xsmax进入dfu模式_iPhone如何进入DFU模式
  19. 紧急:Spring框架被爆出存在0day级别远程命令执行漏洞。漏洞危害程度不亚于log4j漏洞根据目前掌握的信息,JDK版本在9及以上的Spring框架均受影响。该漏洞目前无官方修复补丁
  20. 快到假期了,还抢不到票?可以试试这两个工具

热门文章

  1. Dockerfile中的CMD和ENTRYPOINT有什么区别?
  2. AASM rule of scoring sleep stages using EEG signal
  3. 数学符号发音及英文表达
  4. WP模板常用调用函数
  5. 【MYSQL】ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  6. 从汉语与英语最常用字词,分析汉语与英语的异同(一)
  7. 全球与中国椎弓根螺钉固定市场深度研究分析报告
  8. python爬虫可视化题目北京空气质量监测数据获取与分析
  9. 论团队协作的一个小故事
  10. 关于“击败”团队目标的思考