[nginx] Syslog: introduced error log handler.
Roman Arutyunyan
arut at nginx.com
Mon Mar 13 12:59:21 UTC 2023
details: https://hg.nginx.org/nginx/rev/8771d35d55d0
branches:
changeset: 8150:8771d35d55d0
user: Maxim Dounin <mdounin at mdounin.ru>
date: Fri Mar 10 07:43:50 2023 +0300
description:
Syslog: introduced error log handler.
This ensures that errors which happen during logging to syslog are logged
with proper context, such as "while logging to syslog" and the server name.
Prodded by Safar Safarly.
diffstat:
src/core/ngx_syslog.c | 47 +++++++++++++++++++++++++++++++++++++++--------
src/core/ngx_syslog.h | 3 ++-
2 files changed, 41 insertions(+), 9 deletions(-)
diffs (127 lines):
diff -r 29adacffdefa -r 8771d35d55d0 src/core/ngx_syslog.c
--- a/src/core/ngx_syslog.c Fri Mar 10 07:43:40 2023 +0300
+++ b/src/core/ngx_syslog.c Fri Mar 10 07:43:50 2023 +0300
@@ -18,6 +18,7 @@
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 u_char *ngx_syslog_log_error(ngx_log_t *log, u_char *buf, size_t len);
static char *facilities[] = {
@@ -67,7 +68,7 @@ ngx_syslog_process_conf(ngx_conf_t *cf,
}
peer->hostname = &cf->cycle->hostname;
- peer->log = &cf->cycle->new_log;
+ peer->logp = &cf->cycle->new_log;
peer->conn.fd = (ngx_socket_t) -1;
@@ -289,6 +290,13 @@ ngx_syslog_send(ngx_syslog_peer_t *peer,
{
ssize_t n;
+ if (peer->log.handler == NULL) {
+ peer->log = *peer->logp;
+ peer->log.handler = ngx_syslog_log_error;
+ peer->log.data = peer;
+ peer->log.action = "logging to syslog";
+ }
+
if (peer->conn.fd == (ngx_socket_t) -1) {
if (ngx_syslog_init_peer(peer) != NGX_OK) {
return NGX_ERROR;
@@ -306,7 +314,7 @@ ngx_syslog_send(ngx_syslog_peer_t *peer,
if (n == NGX_ERROR) {
if (ngx_close_socket(peer->conn.fd) == -1) {
- ngx_log_error(NGX_LOG_ALERT, peer->log, ngx_socket_errno,
+ ngx_log_error(NGX_LOG_ALERT, &peer->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
@@ -324,25 +332,25 @@ ngx_syslog_init_peer(ngx_syslog_peer_t *
fd = ngx_socket(peer->server.sockaddr->sa_family, SOCK_DGRAM, 0);
if (fd == (ngx_socket_t) -1) {
- ngx_log_error(NGX_LOG_ALERT, peer->log, ngx_socket_errno,
+ ngx_log_error(NGX_LOG_ALERT, &peer->log, ngx_socket_errno,
ngx_socket_n " failed");
return NGX_ERROR;
}
if (ngx_nonblocking(fd) == -1) {
- ngx_log_error(NGX_LOG_ALERT, peer->log, ngx_socket_errno,
+ ngx_log_error(NGX_LOG_ALERT, &peer->log, ngx_socket_errno,
ngx_nonblocking_n " failed");
goto failed;
}
if (connect(fd, peer->server.sockaddr, peer->server.socklen) == -1) {
- ngx_log_error(NGX_LOG_ALERT, peer->log, ngx_socket_errno,
+ ngx_log_error(NGX_LOG_ALERT, &peer->log, ngx_socket_errno,
"connect() failed");
goto failed;
}
peer->conn.fd = fd;
- peer->conn.log = peer->log;
+ peer->conn.log = &peer->log;
/* UDP sockets are always ready to write */
peer->conn.write->ready = 1;
@@ -352,7 +360,7 @@ ngx_syslog_init_peer(ngx_syslog_peer_t *
failed:
if (ngx_close_socket(fd) == -1) {
- ngx_log_error(NGX_LOG_ALERT, peer->log, ngx_socket_errno,
+ ngx_log_error(NGX_LOG_ALERT, &peer->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
@@ -373,7 +381,30 @@ ngx_syslog_cleanup(void *data)
}
if (ngx_close_socket(peer->conn.fd) == -1) {
- ngx_log_error(NGX_LOG_ALERT, peer->log, ngx_socket_errno,
+ ngx_log_error(NGX_LOG_ALERT, &peer->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
}
+
+
+static u_char *
+ngx_syslog_log_error(ngx_log_t *log, u_char *buf, size_t len)
+{
+ u_char *p;
+ ngx_syslog_peer_t *peer;
+
+ p = buf;
+
+ if (log->action) {
+ p = ngx_snprintf(buf, len, " while %s", log->action);
+ len -= p - buf;
+ }
+
+ peer = log->data;
+
+ if (peer) {
+ p = ngx_snprintf(p, len, ", server: %V", &peer->server.name);
+ }
+
+ return p;
+}
diff -r 29adacffdefa -r 8771d35d55d0 src/core/ngx_syslog.h
--- a/src/core/ngx_syslog.h Fri Mar 10 07:43:40 2023 +0300
+++ b/src/core/ngx_syslog.h Fri Mar 10 07:43:50 2023 +0300
@@ -18,7 +18,8 @@ typedef struct {
ngx_addr_t server;
ngx_connection_t conn;
- ngx_log_t *log;
+ ngx_log_t log;
+ ngx_log_t *logp;
unsigned busy:1;
unsigned nohostname:1;
More information about the nginx-devel
mailing list