[nginx] Syslog: fixed duplicate errors with access logging to sy...

Maxim Dounin mdounin at mdounin.ru
Wed Mar 13 15:52:32 UTC 2024


details:   http://freenginx.org/hg/nginx/rev/c7c8354f99fa
branches:  
changeset: 9224:c7c8354f99fa
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Fri Mar 01 05:42:09 2024 +0300
description:
Syslog: fixed duplicate errors with access logging to syslog.

The ngx_syslog_send() function logs errors itself, so there is no need
to additionally log errors in the caller, notably access log modules in
http and stream.

To ensure that incomplete and blocked writes are also logged, appropriate
logging added to ngx_syslog_send().

diffstat:

 src/core/ngx_syslog.c                  |   4 ++++
 src/http/modules/ngx_http_log_module.c |  17 ++---------------
 src/stream/ngx_stream_log_module.c     |  17 ++---------------
 3 files changed, 8 insertions(+), 30 deletions(-)

diffs (82 lines):

diff --git a/src/core/ngx_syslog.c b/src/core/ngx_syslog.c
--- a/src/core/ngx_syslog.c
+++ b/src/core/ngx_syslog.c
@@ -319,6 +319,10 @@ ngx_syslog_send(ngx_syslog_peer_t *peer,
         }
 
         peer->conn.fd = (ngx_socket_t) -1;
+
+    } else if ((size_t) n != len) {
+        ngx_log_error(NGX_LOG_CRIT, &peer->log, 0,
+                      "send() incomplete");
     }
 
     return n;
diff --git a/src/http/modules/ngx_http_log_module.c b/src/http/modules/ngx_http_log_module.c
--- a/src/http/modules/ngx_http_log_module.c
+++ b/src/http/modules/ngx_http_log_module.c
@@ -254,8 +254,7 @@ static ngx_int_t
 ngx_http_log_handler(ngx_http_request_t *r)
 {
     u_char                   *line, *p;
-    size_t                    len, size;
-    ssize_t                   n;
+    size_t                    len;
     ngx_str_t                 val;
     ngx_uint_t                i, l;
     ngx_http_log_t           *log;
@@ -376,19 +375,7 @@ ngx_http_log_handler(ngx_http_request_t 
 
         if (log[l].syslog_peer) {
 
-            size = p - line;
-
-            n = ngx_syslog_send(log[l].syslog_peer, line, size);
-
-            if (n < 0) {
-                ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
-                              "send() to syslog failed");
-
-            } else if ((size_t) n != size) {
-                ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
-                              "send() to syslog has written only %z of %uz",
-                              n, size);
-            }
+            (void) ngx_syslog_send(log[l].syslog_peer, line, p - line);
 
             continue;
         }
diff --git a/src/stream/ngx_stream_log_module.c b/src/stream/ngx_stream_log_module.c
--- a/src/stream/ngx_stream_log_module.c
+++ b/src/stream/ngx_stream_log_module.c
@@ -201,8 +201,7 @@ static ngx_int_t
 ngx_stream_log_handler(ngx_stream_session_t *s)
 {
     u_char                     *line, *p;
-    size_t                      len, size;
-    ssize_t                     n;
+    size_t                      len;
     ngx_str_t                   val;
     ngx_uint_t                  i, l;
     ngx_stream_log_t           *log;
@@ -324,19 +323,7 @@ ngx_stream_log_handler(ngx_stream_sessio
 
         if (log[l].syslog_peer) {
 
-            size = p - line;
-
-            n = ngx_syslog_send(log[l].syslog_peer, line, size);
-
-            if (n < 0) {
-                ngx_log_error(NGX_LOG_WARN, s->connection->log, 0,
-                              "send() to syslog failed");
-
-            } else if ((size_t) n != size) {
-                ngx_log_error(NGX_LOG_WARN, s->connection->log, 0,
-                              "send() to syslog has written only %z of %uz",
-                              n, size);
-            }
+            (void) ngx_syslog_send(log[l].syslog_peer, line, p - line);
 
             continue;
         }



More information about the nginx-devel mailing list