[nginx] Added syslog support for error_log and access_log direct...

Homutov Vladimir vl at nginx.com
Wed May 21 13:26:29 UTC 2014


details:   http://hg.nginx.org/nginx/rev/777202558122
branches:  
changeset: 5702:777202558122
user:      Vladimir Homutov <vl at nginx.com>
date:      Mon May 12 16:34:15 2014 +0400
description:
Added syslog support for error_log and access_log directives.

diffstat:

 auto/sources                           |    6 +-
 src/core/ngx_core.h                    |    1 +
 src/core/ngx_log.c                     |   86 +++++++-
 src/core/ngx_log.h                     |    6 +
 src/core/ngx_syslog.c                  |  332 +++++++++++++++++++++++++++++++++
 src/core/ngx_syslog.h                  |   30 ++
 src/core/ngx_times.c                   |   21 +-
 src/core/ngx_times.h                   |    1 +
 src/http/modules/ngx_http_log_module.c |   71 ++++++-
 src/http/ngx_http_request.h            |    2 +
 src/os/unix/ngx_process_cycle.c        |   11 +-
 11 files changed, 544 insertions(+), 23 deletions(-)

diffs (truncated from 858 to 300 lines):

diff -r 1209b8a7b077 -r 777202558122 auto/sources
--- a/auto/sources	Tue May 20 16:10:07 2014 +0400
+++ b/auto/sources	Mon May 12 16:34:15 2014 +0400
@@ -37,7 +37,8 @@ CORE_DEPS="src/core/nginx.h \
            src/core/ngx_resolver.h \
            src/core/ngx_open_file_cache.h \
            src/core/ngx_crypt.h \
-           src/core/ngx_proxy_protocol.h"
+           src/core/ngx_proxy_protocol.h \
+           src/core/ngx_syslog.h"
 
 
 CORE_SRCS="src/core/nginx.c \
@@ -69,7 +70,8 @@ CORE_SRCS="src/core/nginx.c \
            src/core/ngx_resolver.c \
            src/core/ngx_open_file_cache.c \
            src/core/ngx_crypt.c \
-           src/core/ngx_proxy_protocol.c"
+           src/core/ngx_proxy_protocol.c \
+           src/core/ngx_syslog.c"
 
 
 REGEX_MODULE=ngx_regex_module
diff -r 1209b8a7b077 -r 777202558122 src/core/ngx_core.h
--- a/src/core/ngx_core.h	Tue May 20 16:10:07 2014 +0400
+++ b/src/core/ngx_core.h	Mon May 12 16:34:15 2014 +0400
@@ -77,6 +77,7 @@ typedef void (*ngx_connection_handler_pt
 #include <ngx_open_file_cache.h>
 #include <ngx_os.h>
 #include <ngx_connection.h>
+#include <ngx_syslog.h>
 #include <ngx_proxy_protocol.h>
 
 
diff -r 1209b8a7b077 -r 777202558122 src/core/ngx_log.c
--- a/src/core/ngx_log.c	Tue May 20 16:10:07 2014 +0400
+++ b/src/core/ngx_log.c	Mon May 12 16:34:15 2014 +0400
@@ -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;
 
+        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;
+        }
+
+        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 {
-        name = value[1];
-    }
-
-    new_log->file = ngx_conf_open_file(cf->cycle, &name);
-    if (new_log->file == NULL) {
-        return NGX_CONF_ERROR;
+        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) {
diff -r 1209b8a7b077 -r 777202558122 src/core/ngx_log.h
--- a/src/core/ngx_log.h	Tue May 20 16:10:07 2014 +0400
+++ b/src/core/ngx_log.h	Mon May 12 16:34:15 2014 +0400
@@ -43,6 +43,8 @@
 
 
 typedef u_char *(*ngx_log_handler_pt) (ngx_log_t *log, u_char *buf, size_t len);
+typedef void (*ngx_log_writer_pt) (ngx_log_t *log, ngx_uint_t level,
+    u_char *buf, size_t len);
 
 
 struct ngx_log_s {
@@ -54,6 +56,9 @@ struct ngx_log_s {
     ngx_log_handler_pt   handler;
     void                *data;
 
+    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
@@ -227,6 +232,7 @@ void ngx_cdecl ngx_log_stderr(ngx_err_t 
 u_char *ngx_log_errno(u_char *buf, u_char *last, ngx_err_t err);
 ngx_int_t ngx_log_open_default(ngx_cycle_t *cycle);
 ngx_int_t ngx_log_redirect_stderr(ngx_cycle_t *cycle);
+ngx_log_t *ngx_log_get_file_log(ngx_log_t *head);
 char *ngx_log_set_log(ngx_conf_t *cf, ngx_log_t **head);
 
 
diff -r 1209b8a7b077 -r 777202558122 src/core/ngx_syslog.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/ngx_syslog.c	Mon May 12 16:34:15 2014 +0400
@@ -0,0 +1,332 @@
+
+/*
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_event.h>
+
+
+#define NGX_SYSLOG_MAX_STR                                                    \
+     NGX_MAX_ERROR_STR + sizeof("<255>Jan 01 00:00:00 ") - 1                  \
+     + (NGX_MAXHOSTNAMELEN - 1) + 1 /* space */                               \
+     + 32 /* tag */ + 2 /* colon, space */
+
+
+static char *ngx_syslog_parse_args(ngx_conf_t *cf, ngx_syslog_peer_t *peer);
+static ngx_int_t ngx_syslog_init_peer(ngx_syslog_peer_t *peer);
+static void ngx_syslog_cleanup(void *data);
+
+
+static char  *facilities[] = {
+    "kern", "user", "mail", "daemon", "auth", "intern", "lpr", "news", "uucp",
+    "clock", "authpriv", "ftp", "ntp", "audit", "alert", "cron", "local0",
+    "local1", "local2", "local3", "local4", "local5", "local6", "local7",
+    NULL
+};
+
+/* note 'error/warn' like in nginx.conf, not 'err/warning' */
+static char  *severities[] = {
+    "emerg", "alert", "crit", "error", "warn", "notice", "info", "debug", NULL
+};
+
+static ngx_log_t    ngx_syslog_dummy_log;
+static ngx_event_t  ngx_syslog_dummy_event;
+
+
+char *
+ngx_syslog_process_conf(ngx_conf_t *cf, ngx_syslog_peer_t *peer)
+{
+    peer->pool = cf->pool;
+    peer->facility = NGX_CONF_UNSET_UINT;
+    peer->severity = NGX_CONF_UNSET_UINT;
+
+    if (ngx_syslog_parse_args(cf, peer) != NGX_CONF_OK) {
+        return NGX_CONF_ERROR;
+    }
+
+    if (peer->server.sockaddr == NULL) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "no syslog server specified");
+        return NGX_CONF_ERROR;
+    }
+
+    if (peer->facility == NGX_CONF_UNSET_UINT) {
+        peer->facility = 23; /* local7 */
+    }
+
+    if (peer->severity == NGX_CONF_UNSET_UINT) {
+        peer->severity = 6; /* info */
+    }
+
+    if (peer->tag.data == NULL) {
+        ngx_str_set(&peer->tag, "nginx");
+    }
+
+    peer->conn.fd = (ngx_socket_t) -1;
+
+    return NGX_CONF_OK;
+}
+
+
+static char *
+ngx_syslog_parse_args(ngx_conf_t *cf, ngx_syslog_peer_t *peer)
+{
+    u_char      *p, *comma, c;
+    size_t       len;
+    ngx_str_t   *value;
+    ngx_url_t    u;
+    ngx_uint_t   i;
+
+    value = cf->args->elts;
+
+    p = value[1].data + sizeof("syslog:") - 1;
+
+    for ( ;; ) {
+        comma = (u_char *) ngx_strchr(p, ',');
+
+        if (comma != NULL) {
+            len = comma - p;
+            *comma = '\0';
+
+        } else {
+            len = value[1].data + value[1].len - p;
+        }



More information about the nginx-devel mailing list