diff src/core/ngx_log.c @ 5702:777202558122

Added syslog support for error_log and access_log directives.
author Vladimir Homutov <vl@nginx.com>
date Mon, 12 May 2014 16:34:15 +0400
parents e088695737c3
children c3b08217f2a2
line wrap: on
line diff
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -148,6 +148,12 @@ ngx_log_error_core(ngx_uint_t level, ngx
             break;
         }
 
+        if (log->writer) {
+            log->writer(log, level, errstr, p - errstr);
+            log = log->next;
+            continue;
+        }
+
         (void) ngx_write_fd(log->file->fd, errstr, p - errstr);
 
         if (log->file->fd == ngx_stderr) {
@@ -366,15 +372,33 @@ ngx_log_init(u_char *prefix)
 ngx_int_t
 ngx_log_open_default(ngx_cycle_t *cycle)
 {
-    static ngx_str_t  error_log = ngx_string(NGX_ERROR_LOG_PATH);
+    ngx_log_t         *log;
+    static ngx_str_t   error_log = ngx_string(NGX_ERROR_LOG_PATH);
 
-    if (cycle->new_log.file == NULL) {
-        cycle->new_log.file = ngx_conf_open_file(cycle, &error_log);
-        if (cycle->new_log.file == NULL) {
+    if (ngx_log_get_file_log(&cycle->new_log) != NULL) {
+        return NGX_OK;
+    }
+
+    if (cycle->new_log.log_level != 0) {
+        /* there are some error logs, but no files */
+
+        log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
+        if (log == NULL) {
             return NGX_ERROR;
         }
 
-        cycle->new_log.log_level = NGX_LOG_ERR;
+        log->log_level = NGX_LOG_ERR;
+        ngx_log_insert(&cycle->new_log, log);
+
+    } else {
+        /* no error logs at all */
+        log = &cycle->new_log;
+        log->log_level = NGX_LOG_ERR;
+    }
+
+    log->file = ngx_conf_open_file(cycle, &error_log);
+    if (log->file == NULL) {
+        return NGX_ERROR;
     }
 
     return NGX_OK;
@@ -390,7 +414,8 @@ ngx_log_redirect_stderr(ngx_cycle_t *cyc
         return NGX_OK;
     }
 
-    fd = cycle->log->file->fd;
+    /* file log always exists when we are called */
+    fd = ngx_log_get_file_log(cycle->log)->file->fd;
 
     if (fd != ngx_stderr) {
         if (ngx_set_stderr(fd) == NGX_FILE_ERROR) {
@@ -405,6 +430,21 @@ ngx_log_redirect_stderr(ngx_cycle_t *cyc
 }
 
 
+ngx_log_t *
+ngx_log_get_file_log(ngx_log_t *head)
+{
+    ngx_log_t  *log;
+
+    for (log = head; log; log = log->next) {
+        if (log->file != NULL) {
+            return log;
+        }
+    }
+
+    return NULL;
+}
+
+
 static char *
 ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)
 {
@@ -482,8 +522,9 @@ ngx_error_log(ngx_conf_t *cf, ngx_comman
 char *
 ngx_log_set_log(ngx_conf_t *cf, ngx_log_t **head)
 {
-    ngx_log_t  *new_log;
-    ngx_str_t  *value, name;
+    ngx_log_t          *new_log;
+    ngx_str_t          *value, name;
+    ngx_syslog_peer_t  *peer;
 
     if (*head != NULL && (*head)->log_level == 0) {
         new_log = *head;
@@ -506,13 +547,30 @@ ngx_log_set_log(ngx_conf_t *cf, ngx_log_
         ngx_str_null(&name);
         cf->cycle->log_use_stderr = 1;
 
-    } else {
-        name = value[1];
-    }
+        new_log->file = ngx_conf_open_file(cf->cycle, &name);
+        if (new_log->file == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+
+     } else if (ngx_strncmp(value[1].data, "syslog:", 7) == 0) {
+        peer = ngx_pcalloc(cf->pool, sizeof(ngx_syslog_peer_t));
+        if (peer == NULL) {
+            return NGX_CONF_ERROR;
+        }
 
-    new_log->file = ngx_conf_open_file(cf->cycle, &name);
-    if (new_log->file == NULL) {
-        return NGX_CONF_ERROR;
+        if (ngx_syslog_process_conf(cf, peer) != NGX_CONF_OK) {
+            return NGX_CONF_ERROR;
+        }
+
+        new_log->writer = ngx_syslog_writer;
+        new_log->wdata = peer;
+
+    } else {
+        new_log->file = ngx_conf_open_file(cf->cycle, &value[1]);
+        if (new_log->file == NULL) {
+            return NGX_CONF_ERROR;
+        }
     }
 
     if (ngx_log_set_levels(cf, new_log) != NGX_CONF_OK) {