zoukankan      html  css  js  c++  java
  • nginx开发_调试日志

    接口列表

    核心文件ngx_log.h

    主要接口如下:

    ngx_log_error(level, log, err, fmt, ...)
    ngx_log_debug(level, log, err, fmt, ...)
    ngx_log_debug0(level, log, err, fmt)
    ngx_log_debug1(level, log, err, fmt, arg1)
    ngx_log_debug2(level, log, err, fmt, arg1, arg2)
    ngx_log_debug3(level, log, err, fmt, arg1, arg2, arg3)
    ngx_log_debug4(level, log, err, fmt, arg1, arg2, arg3, arg4)
    ngx_log_debug5(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5)
    ngx_log_debug6(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5, arg6)
    ngx_log_debug7(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
    ngx_log_debug8(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)

    可以简单分成三组

    ngx_log_error(level, log, err, fmt, ...)

    ngx_log_debug(level, log, err, fmt, ...)

    ngx_log_debugX(level, log, err, fmt, ...)

    ngx_log_error接口

    ngx_log_error(level, log, err, fmt, ...) 标准的分级打印接口。

    第1个参数level可选宏如下:

    #define NGX_LOG_STDERR            0
    #define NGX_LOG_EMERG             1
    #define NGX_LOG_ALERT             2
    #define NGX_LOG_CRIT              3
    #define NGX_LOG_ERR               4
    #define NGX_LOG_WARN              5
    #define NGX_LOG_NOTICE            6
    #define NGX_LOG_INFO              7
    #define NGX_LOG_DEBUG             8
    

    打印等级通过nginx.conf中的error_log指令可以配置,其中WARN以上级别打印会直接打印到stderr上,对于某些临时调试场景有意义。

    第2个参数log,用于部分回调功能。(还未深入研究)【个人认为此参数应该以全局变量方式存在,不应该设置为参数,影响在算法模块中使用日志】

    常见获取方式包括

    [ngx_conf_t].log
    [ngx_http_request_t].connection.log
    [ngx_connection_t].log
    

    第3个参数err,用于记录errno,非系统调用错误,一般使用0即可。【个人认为此参数过设计】

    后续参数为可变长的字符串参数,其中针对ngx常用的几个类型有特殊定义,可以参考下文中的表格。

    使用示例:

    static void* 
    ngx_http_mytest_create_srv_conf(ngx_conf_t *cf)
    {
      	...
        ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "ngx_http_mytest_create_srv_conf");
    	...
    }
    static ngx_int_t 
    ngx_http_ws_cache_info_filter(ngx_http_request_t *r)
    {
        ...
        ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, "ws_cache_info context already exist!");
        ...
    }
    

    ngx_log_debug接口

    第一个参数,实际上为模块类型标识,会与log->log_level进行比对,如果比对失败将不进行打印。

    进行打印时功能等价于ngx_log_error的NGX_LOG_DEBUG级别,后续参数含义也一致。

    #define NGX_LOG_DEBUG_CORE        0x010
    #define NGX_LOG_DEBUG_ALLOC       0x020
    #define NGX_LOG_DEBUG_MUTEX       0x040
    #define NGX_LOG_DEBUG_EVENT       0x080
    #define NGX_LOG_DEBUG_HTTP        0x100
    #define NGX_LOG_DEBUG_MAIL        0x200
    #define NGX_LOG_DEBUG_STREAM      0x400
    

    注意:ngx_log_debug接口并不在NGX_DEBUG宏限制范围内,也就是说即使编译时没有添加--debug参数,ngx_log_debug接口依然可以使用。

    【个人认为:level参数没有意义应该删除,接口应该放在NGX_DEBUG宏限制范围内,与ngx_log_debugX语义保持一致】

    使用示例:

    static ngx_int_t 
    ngx_http_ws_cache_info_filter(ngx_http_request_t *r)
    {
        ...
        ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "enter ngx_http_ws_cache_info_filter");
        ...
    }
    

    ngx_log_debugX接口

    用于固定数量参数的打印接口,参数与ngx_log_debug一致,接口定义在NGX_DEBUG宏限制范围内,即必须在编译时开启--debug参数,这组接口才能使用。

    【个人认为:ngx_log_debugX接口本意应该为了不支持动态数量参数的C编译器设计的,一般通途不大,但它时唯一一组可以通过编译参数去除的调试接口,所以还有广泛使用。根因还是ngx_log_debug接口设计不合理】

    使用示例:

    static ngx_int_t
    ngx_http_ws_cache_info_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
    {
      	...
    	ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "arg_name=%V", &arg_name);
      	...
    }
    

    ngx特殊格式化

    标识符 对应类型
    %O off_t
    %T time_t
    %z ssize_t
    %i ngx_int_t
    %p void *
    %V ngx_str_t *
    %s u_char * (null-terminated)
    %*s size_t + u_char *

    更多完成的标识符可以参考《深入理解Nginx(第2版)》P148 表4-8

  • 相关阅读:
    那些离不开的 Chrome 扩展插件
    Spring Boot 实战 —— 入门
    Maven 学习笔记
    Linux lvm 分区知识笔记
    Linux 双向 SSH 免密登录
    CentOS Yum 源搭建
    Ubuntu 系统学习
    iOS 测试三方 KIF 的那些事
    Swift 网络请求数据与解析
    iOS Plist 文件的 增 删 改
  • 原文地址:https://www.cnblogs.com/atskyline/p/7852459.html
Copyright © 2011-2022 走看看