nginx/src/core/ngx_log.c

345 lines
7.6 KiB
C
Raw Normal View History

/*
* Copyright (C) Igor Sysoev
*/
#include <ngx_config.h>
2003-05-20 23:37:55 +08:00
#include <ngx_core.h>
2003-06-03 23:42:58 +08:00
static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_command_t ngx_errlog_commands[] = {
{ngx_string("error_log"),
2003-12-09 04:48:12 +08:00
NGX_MAIN_CONF|NGX_CONF_1MORE,
2003-06-03 23:42:58 +08:00
ngx_set_error_log,
0,
0,
NULL},
ngx_null_command
};
2004-04-13 00:38:09 +08:00
static ngx_core_module_t ngx_errlog_module_ctx = {
ngx_string("errlog"),
NULL,
2004-04-13 00:38:09 +08:00
NULL
};
2003-06-03 23:42:58 +08:00
ngx_module_t ngx_errlog_module = {
nginx-0.1.29-RELEASE import *) Feature: the ngx_http_ssi_module supports "include virtual" command. *) Feature: the ngx_http_ssi_module supports the condition command like 'if expr="$NAME"' and "else" and "endif" commands. Only one nested level is supported. *) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and DATE_GMT variables and "config timefmt" command. *) Feature: the "ssi_ignore_recycled_buffers" directive. *) Bugfix: the "echo" command did not show the default value for the empty QUERY_STRING variable. *) Change: the ngx_http_proxy_module was rewritten. *) Feature: the "proxy_redirect", "proxy_pass_request_headers", "proxy_pass_request_body", and "proxy_method" directives. *) Feature: the "proxy_set_header" directive. The "proxy_x_var" was canceled and must be replaced with the proxy_set_header directive. *) Change: the "proxy_preserve_host" is canceled and must be replaced with the "proxy_set_header Host $host" and the "proxy_redirect off" directives, the "proxy_set_header Host $host:$proxy_port" directive and the appropriate proxy_redirect directives. *) Change: the "proxy_set_x_real_ip" is canceled and must be replaced with the "proxy_set_header X-Real-IP $remote_addr" directive. *) Change: the "proxy_add_x_forwarded_for" is canceled and must be replaced with the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for" directive. *) Change: the "proxy_set_x_url" is canceled and must be replaced with the "proxy_set_header X-URL http://$host:$server_port$request_uri" directive. *) Feature: the "fastcgi_param" directive. *) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params" directive are canceled and must be replaced with the fastcgi_param directives. *) Feature: the "index" directive can use the variables. *) Feature: the "index" directive can be used at http and server levels. *) Change: the last index only in the "index" directive can be absolute. *) Feature: the "rewrite" directive can use the variables. *) Feature: the "internal" directive. *) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR, SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME, REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables. *) Change: nginx now passes the invalid lines in a client request headers or a backend response header. *) Bugfix: if the backend did not transfer response for a long time and the "send_timeout" was less than "proxy_read_timeout", then nginx returned the 408 response. *) Bugfix: the segmentation fault was occurred if the backend sent an invalid line in response header; the bug had appeared in 0.1.26. *) Bugfix: the segmentation fault may occurred in FastCGI fault tolerance configuration. *) Bugfix: the "expires" directive did not remove the previous "Expires" and "Cache-Control" headers. *) Bugfix: nginx did not take into account trailing dot in "Host" header line. *) Bugfix: the ngx_http_auth_module did not work under Linux. *) Bugfix: the rewrite directive worked incorrectly, if the arguments were in a request. *) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
NGX_MODULE_V1,
2004-04-13 00:38:09 +08:00
&ngx_errlog_module_ctx, /* module context */
2003-06-03 23:42:58 +08:00
ngx_errlog_commands, /* module directives */
NGX_CORE_MODULE, /* module type */
NULL, /* init master */
2003-07-11 12:50:59 +08:00
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
2003-06-03 23:42:58 +08:00
};
2003-06-26 23:35:36 +08:00
static ngx_log_t ngx_log;
static ngx_open_file_t ngx_stderr;
2003-06-03 23:42:58 +08:00
static const char *err_levels[] = {
2003-05-20 23:37:55 +08:00
"stderr", "emerg", "alert", "crit", "error",
"warn", "notice", "info", "debug"
};
2003-12-09 04:48:12 +08:00
static const char *debug_levels[] = {
"debug_core", "debug_alloc", "debug_mutex", "debug_event",
"debug_http", "debug_mail", "debug_mysql"
2003-12-09 04:48:12 +08:00
};
#if (NGX_HAVE_VARIADIC_MACROS)
void
ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, ...)
#else
void
ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, va_list args)
#endif
{
#if (NGX_HAVE_VARIADIC_MACROS)
va_list args;
#endif
u_char errstr[NGX_MAX_ERROR_STR], *p, *last;
2003-07-21 05:15:59 +08:00
if (log->file->fd == NGX_INVALID_FILE) {
return;
}
last = errstr + NGX_MAX_ERROR_STR;
2003-11-13 14:14:05 +08:00
ngx_memcpy(errstr, ngx_cached_err_log_time.data,
ngx_cached_err_log_time.len);
p = errstr + ngx_cached_err_log_time.len;
2003-11-13 14:14:05 +08:00
p = ngx_snprintf(p, last - p, " [%s] ", err_levels[level]);
2002-08-26 23:18:19 +08:00
2002-12-23 14:29:22 +08:00
/* pid#tid */
p = ngx_snprintf(p, last - p, "%P#" NGX_TID_T_FMT ": ",
ngx_log_pid, ngx_log_tid);
2002-08-26 23:18:19 +08:00
if (log->connection) {
p = ngx_snprintf(p, last - p, "*%uA ", log->connection);
}
#if (NGX_HAVE_VARIADIC_MACROS)
2003-11-20 00:26:41 +08:00
2002-08-26 23:18:19 +08:00
va_start(args, fmt);
p = ngx_vsnprintf(p, last - p, fmt, args);
2002-08-26 23:18:19 +08:00
va_end(args);
2003-11-20 00:26:41 +08:00
2002-08-26 23:18:19 +08:00
#else
2003-11-20 00:26:41 +08:00
p = ngx_vsnprintf(p, last - p, fmt, args);
2003-11-20 00:26:41 +08:00
2002-08-26 23:18:19 +08:00
#endif
if (err) {
2003-04-28 23:06:39 +08:00
if (p > last - 50) {
2004-01-22 00:38:54 +08:00
/* leave a space for an error code */
p = last - 50;
*p++ = '.';
*p++ = '.';
*p++ = '.';
2004-01-22 00:38:54 +08:00
}
#if (NGX_WIN32)
2003-04-28 23:06:39 +08:00
if ((unsigned) err >= 0x80000000) {
p = ngx_snprintf(p, last - p, " (%Xd: ", err);
} else {
p = ngx_snprintf(p, last - p, " (%d: ", err);
2004-01-22 00:38:54 +08:00
}
#else
2004-01-22 00:38:54 +08:00
p = ngx_snprintf(p, last - p, " (%d: ", err);
2004-01-22 00:38:54 +08:00
#endif
2004-01-22 00:38:54 +08:00
p = ngx_strerror_r(err, p, last - p);
2004-01-22 00:38:54 +08:00
if (p < last) {
*p++ = ')';
}
}
2002-12-23 14:29:22 +08:00
if (level != NGX_LOG_DEBUG && log->handler) {
p = log->handler(log, p, last - p);
2002-12-23 14:29:22 +08:00
}
if (p > last - NGX_LINEFEED_SIZE) {
p = last - NGX_LINEFEED_SIZE;
}
ngx_linefeed(p);
2004-01-22 00:38:54 +08:00
ngx_write_fd(log->file->fd, errstr, p - errstr);
2004-01-22 00:38:54 +08:00
}
#if !(NGX_HAVE_VARIADIC_MACROS)
void ngx_cdecl
ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, ...)
{
va_list args;
if (log->log_level >= level) {
va_start(args, fmt);
ngx_log_error_core(level, log, err, fmt, args);
va_end(args);
}
}
2003-05-20 23:37:55 +08:00
void ngx_cdecl
ngx_log_debug_core(ngx_log_t *log, ngx_err_t err, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
2003-12-01 04:03:18 +08:00
ngx_log_error_core(NGX_LOG_DEBUG, log, err, fmt, args);
va_end(args);
}
#endif
2003-05-20 23:37:55 +08:00
2006-11-20 16:51:45 +08:00
void
ngx_log_abort(ngx_err_t err, const char *text)
{
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err, text);
}
ngx_log_t *
ngx_log_init(void)
2003-06-03 23:42:58 +08:00
{
2003-06-26 23:35:36 +08:00
ngx_log.file = &ngx_stderr;
ngx_log.log_level = NGX_LOG_NOTICE;
2004-09-29 04:09:22 +08:00
#if (NGX_WIN32)
2004-10-01 23:53:53 +08:00
ngx_stderr_fileno = GetStdHandle(STD_ERROR_HANDLE);
ngx_stderr.fd = ngx_open_file(NGX_ERROR_LOG_PATH, NGX_FILE_RDWR,
NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND, 0);
2004-10-04 04:02:06 +08:00
if (ngx_stderr.fd == NGX_INVALID_FILE) {
ngx_message_box("nginx", MB_OK, ngx_errno,
"Could not open error log file: "
ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed");
return NULL;
2004-09-29 04:09:22 +08:00
}
if (ngx_file_append_mode(ngx_stderr.fd) == NGX_ERROR) {
ngx_message_box("nginx", MB_OK, ngx_errno,
"Could not open error log file: "
ngx_file_append_mode_n " \"" NGX_ERROR_LOG_PATH
"\" failed");
return NULL;
2004-09-29 04:09:22 +08:00
}
#else
ngx_stderr.fd = STDERR_FILENO;
2004-09-30 14:38:49 +08:00
2004-09-29 04:09:22 +08:00
#endif
2003-12-06 01:07:27 +08:00
return &ngx_log;
2003-06-03 23:42:58 +08:00
}
ngx_log_t *
ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_array_t *args)
2003-07-02 22:41:17 +08:00
{
ngx_log_t *log;
2003-12-09 04:48:12 +08:00
ngx_str_t *value, *name;
if (args) {
value = args->elts;
name = &value[1];
} else {
name = NULL;
}
2003-07-02 22:41:17 +08:00
log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
if (log == NULL) {
return NULL;
}
log->file = ngx_conf_open_file(cycle, name);
if (log->file == NULL) {
return NULL;
}
2003-07-21 05:15:59 +08:00
2003-07-02 22:41:17 +08:00
return log;
}
char *
ngx_set_error_log_levels(ngx_conf_t *cf, ngx_log_t *log)
2004-01-21 04:40:08 +08:00
{
2004-03-16 15:10:12 +08:00
ngx_uint_t i, n, d;
ngx_str_t *value;
2004-01-21 04:40:08 +08:00
value = cf->args->elts;
2003-12-09 04:48:12 +08:00
for (i = 2; i < cf->args->nelts; i++) {
2004-02-12 01:08:49 +08:00
for (n = 1; n <= NGX_LOG_DEBUG; n++) {
2003-12-09 04:48:12 +08:00
if (ngx_strcmp(value[i].data, err_levels[n]) == 0) {
2004-01-21 04:40:08 +08:00
if (log->log_level != 0) {
2003-12-09 04:48:12 +08:00
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2007-01-18 22:28:34 +08:00
"duplicate log level \"%s\"",
2003-12-09 04:48:12 +08:00
value[i].data);
return NGX_CONF_ERROR;
}
2004-01-21 04:40:08 +08:00
log->log_level = n;
2003-12-09 04:48:12 +08:00
continue;
2003-12-06 01:07:27 +08:00
}
}
2003-12-09 04:48:12 +08:00
2004-01-22 14:47:28 +08:00
for (n = 0, d = NGX_LOG_DEBUG_FIRST; d <= NGX_LOG_DEBUG_LAST; d <<= 1) {
if (ngx_strcmp(value[i].data, debug_levels[n++]) == 0) {
2004-01-21 04:40:08 +08:00
if (log->log_level & ~NGX_LOG_DEBUG_ALL) {
2003-12-09 04:48:12 +08:00
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid log level \"%s\"",
value[i].data);
return NGX_CONF_ERROR;
}
2004-01-21 04:40:08 +08:00
log->log_level |= d;
2003-12-09 04:48:12 +08:00
}
}
2004-01-21 04:40:08 +08:00
if (log->log_level == 0) {
2003-12-06 01:07:27 +08:00
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2003-12-09 04:48:12 +08:00
"invalid log level \"%s\"", value[i].data);
2003-12-06 01:07:27 +08:00
return NGX_CONF_ERROR;
}
2003-06-03 23:42:58 +08:00
}
2004-02-12 01:08:49 +08:00
if (log->log_level == NGX_LOG_DEBUG) {
log->log_level = NGX_LOG_DEBUG_ALL;
}
2003-06-03 23:42:58 +08:00
return NGX_CONF_OK;
}
2004-04-15 23:34:36 +08:00
static char *
ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2004-04-15 23:34:36 +08:00
{
ngx_str_t *value;
value = cf->args->elts;
2004-09-30 14:38:49 +08:00
if (value[1].len == 6 && ngx_strcmp(value[1].data, "stderr") == 0) {
cf->cycle->new_log->file->fd = ngx_stderr.fd;
cf->cycle->new_log->file->name.len = 0;
cf->cycle->new_log->file->name.data = NULL;
} else {
cf->cycle->new_log->file->name = value[1];
2004-09-28 00:03:21 +08:00
2007-07-30 02:05:45 +08:00
if (ngx_conf_full_name(cf->cycle, &cf->cycle->new_log->file->name, 0)
== NGX_ERROR)
2004-09-30 14:38:49 +08:00
{
return NGX_CONF_ERROR;
}
2004-04-15 23:34:36 +08:00
}
return ngx_set_error_log_levels(cf, cf->cycle->new_log);
}