無(wú)論在任何項(xiàng)目中,日志都是一個(gè)非常重要的模塊,無(wú)論是問(wèn)題定位還是日常信息的管理,都離不開他
在nginx中,ngx_errlog_module模塊專門用于處理nginx日志信息,是nginx的core模塊之一
在 main 函數(shù)中,時(shí)間初始化結(jié)束后馬上進(jìn)行的就是日志模塊的初始化
日志結(jié)構(gòu):
日志模塊的初始化主要做的事情就是初始化全局變量 ngx_log,并創(chuàng)建 errlog 文件
ngx_log_s 結(jié)構(gòu)
ngx_log 變量是一個(gè) ngx_log_s 結(jié)構(gòu)體,定義在 core/ngx_log.h 文件中,結(jié)構(gòu)如下:
// struct ngx_log_s// 日志結(jié)構(gòu) {{{struct ngx_log_s { ngx_uint_t log_level; // 日志等級(jí) ngx_open_file_t *file; // 日志文件信息 ngx_atomic_uint_t connection; // 引用該日志對(duì)象的連接數(shù) ngx_log_handler_pt handler; // 輸出日志時(shí)需要調(diào)用的回調(diào)函數(shù) void *data; // 回調(diào)函數(shù)所需要的參數(shù) ngx_log_writer_pt writer; void *wdata; /* * we declare "action" as "char *" because the actions are usually * the static strings and in the "u_char *" case we have to override * their types all the time */ char *action; // 日志記錄前,保存當(dāng)前正在進(jìn)行的動(dòng)作 ngx_log_t *next; // 指向下一日志對(duì)象}; // }}}日志級(jí)別
nginx 中日志共分為9個(gè)級(jí)別:
#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
ngx_open_file_s 結(jié)構(gòu)
其中,日志的文件描述結(jié)構(gòu)如下:
// struct ngx_open_file_s// 打開文件結(jié)構(gòu) {{{struct ngx_open_file_s { ngx_fd_t fd; ngx_str_t name; void (*flush)(ngx_open_file_t *file, ngx_log_t *log); // 文件刷新時(shí)調(diào)用的回調(diào) void *data; // 回調(diào)函數(shù)使用的參數(shù)}; // }}}所有文件相關(guān)的數(shù)據(jù)結(jié)構(gòu)和操作都在 core/ngx_conf_file.h 和 core/ngx_conf_file.c 文件中日志初始化過(guò)程
日志相關(guān)的初始化操作都在 core/ngx_log.c 文件中,現(xiàn)在我們調(diào)用的是其中的初始化函數(shù):
// ngx_log_t *ngx_log_init(u_char *prefix);// 初始化 ngx_log 結(jié)構(gòu),創(chuàng)建 errlog 文件 {{{ngx_log_t *ngx_log_init(u_char *prefix){ u_char *p, *name; size_t nlen, plen; ngx_log.file = &ngx_log_file; ngx_log.log_level = NGX_LOG_NOTICE; name = (u_char *) NGX_ERROR_LOG_PATH; /* * we use ngx_strlen() here since BCC warns about * condition is always false and unreachable code */ nlen = ngx_strlen(name); if (nlen == 0) { ngx_log_file.fd = ngx_stderr; return &ngx_log; } p = NULL; // 判斷是否是絕對(duì)路徑#if (NGX_WIN32) if (name[1] != ':') {#else if (name[0] != '/') {#endif if (prefix) { plen = ngx_strlen(prefix); } else {#ifdef NGX_PREFIX prefix = (u_char *) NGX_PREFIX; plen = ngx_strlen(prefix);#else plen = 0;#endif } if (plen) { name = malloc(plen + nlen + 2); if (name == NULL) { return NULL; } p = ngx_cpymem(name, prefix, plen); if (!ngx_path_separator(*(p - 1))) { *p++ = '/'; } ngx_cpystrn(p, (u_char *) NGX_ERROR_LOG_PATH, nlen + 1); p = name; } } ngx_log_file.fd = ngx_open_file(name, NGX_FILE_APPEND, NGX_FILE_CREATE_OR_OPEN, NGX_FILE_DEFAULT_ACCESS); if (ngx_log_file.fd == NGX_INVALID_FILE) { ngx_log_stderr(ngx_errno, "[alert] could not open error log file: " ngx_open_file_n " /"%s/" failed", name);#if (NGX_WIN32) ngx_event_log(ngx_errno, "could not open error log file: " ngx_open_file_n " /"%s/" failed", name);#endif ngx_log_file.fd = ngx_stderr; } if (p) { ngx_free(p); } return &ngx_log;}// }}}
新聞熱點(diǎn)
疑難解答
圖片精選