初始化ngx_log,設置日誌級別ngx_log.log_level,並獲得日誌的文件句柄ngx_log_file.fd。nginx
static ngx_log_t ngx_log; struct ngx_log_s { ngx_uint_t log_level; ngx_open_file_t *file; ngx_atomic_uint_t connection; ngx_log_handler_pt handler; void *data; /* * 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; ngx_log_t *next; }; typedef struct ngx_log_s ngx_log_t;
static ngx_open_file_t ngx_log_file; struct ngx_open_file_s { ngx_fd_t fd; ngx_str_t name; void (*flush)(ngx_open_file_t *file, ngx_log_t *log); void *data; }; typedef struct ngx_open_file_s ngx_open_file_t;
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; #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; }
當編譯完 Nginx 之後,能夠在 objs 目錄下面看到一個ngx_auto_config.h文件ide
#ifndef NGX_PREFIX #define NGX_PREFIX "/usr/local/" #endif #ifndef NGX_CONF_PREFIX #define NGX_CONF_PREFIX "conf/" #endif #ifndef NGX_SBIN_PATH #define NGX_SBIN_PATH "" #endif #ifndef NGX_CONF_PATH #define NGX_CONF_PATH "conf/nginx.conf" #endif #ifndef NGX_PID_PATH #define NGX_PID_PATH "logs/nginx.pid" #endif #ifndef NGX_LOCK_PATH #define NGX_LOCK_PATH "logs/nginx.lock" #endif #ifndef NGX_ERROR_LOG_PATH #define NGX_ERROR_LOG_PATH "logs/error.log" #endif #ifndef NGX_HTTP_LOG_PATH #define NGX_HTTP_LOG_PATH "logs/access.log" #endif
所以日誌文件路徑爲:ui
/usr/local/logs/error.logatom