Updated syslog patch
Øyvind Kolbu
oyvind.kolbu at usit.uio.no
Tue Aug 5 16:36:49 MSD 2008
Hi
I've cleaned up a few nits in Marlon's syslog patch, e.g to allow it to compile
with -Werror on gcc 4, and avoid adding the timestamp in
ngx_log_error_core().
We are using this patch in production where nginx is used as a
pop/imap-proxy, and it works nicely. Any chance of getting this in
a future release?
--
Øyvind Kolbu
Postmaster
Universitetet i Oslo
-------------- next part --------------
diff -ruN src-0.6.32-orig/auto/make src-0.6.32-syslog/auto/make
--- src-0.6.32-orig/auto/make 2008-08-04 17:18:28.000000000 +0200
+++ src-0.6.32-syslog/auto/make 2008-08-04 17:18:55.000000000 +0200
@@ -13,6 +13,10 @@
ngx_objs_dir=$NGX_OBJS$ngx_regex_dirsep
ngx_use_pch=`echo $NGX_USE_PCH | sed -e "s/\//$ngx_regex_dirsep/g"`
+#SYSLOG
+if [[ "${USE_SYSLOG}" == "YES" ]]; then
+ CFLAGS="$CFLAGS -DUSE_SYSLOG"
+fi
cat << END > $NGX_MAKEFILE
diff -ruN src-0.6.32-orig/auto/options src-0.6.32-syslog/auto/options
--- src-0.6.32-orig/auto/options 2008-08-04 17:18:28.000000000 +0200
+++ src-0.6.32-syslog/auto/options 2008-08-05 14:08:01.000000000 +0200
@@ -102,6 +102,8 @@
MD5_OPT=
MD5_ASM=NO
+USE_SYSLOG=NO
+
USE_SHA1=NO
SHA1=NONE
SHA1_OPT=
@@ -225,6 +227,8 @@
--with-md5-opt=*) MD5_OPT="$value" ;;
--with-md5-asm) MD5_ASM=YES ;;
+ --with-syslog) USE_SYSLOG=YES ;;
+
--with-sha1=*) SHA1="$value" ;;
--with-sha1-opt=*) SHA1_OPT="$value" ;;
--with-sha1-asm) SHA1_ASM=YES ;;
@@ -341,6 +345,8 @@
--with-md5-opt=OPTIONS set additional options for md5 building
--with-md5-asm use md5 assembler sources
+ --with-syslog use syslog instead of files to log messages
+
--with-sha1=DIR set path to sha1 library sources
--with-sha1-opt=OPTIONS set additional options for sha1 building
--with-sha1-asm use sha1 assembler sources
diff -ruN src-0.6.32-orig/auto/summary src-0.6.32-syslog/auto/summary
--- src-0.6.32-orig/auto/summary 2008-08-04 17:18:28.000000000 +0200
+++ src-0.6.32-syslog/auto/summary 2008-08-04 17:18:55.000000000 +0200
@@ -71,6 +71,11 @@
*) echo " + using zlib library: $ZLIB" ;;
esac
+case $USE_SYSLOG in
+ YES) echo " + using syslog" ;;
+ *) echo " + syslog is not used" ;;
+esac
+
echo
diff -ruN src-0.6.32-orig/src/core/nginx.c src-0.6.32-syslog/src/core/nginx.c
--- src-0.6.32-orig/src/core/nginx.c 2008-08-04 17:18:28.000000000 +0200
+++ src-0.6.32-syslog/src/core/nginx.c 2008-08-04 18:54:41.000000000 +0200
@@ -9,6 +9,9 @@
#include <ngx_event.h>
#include <nginx.h>
+#ifdef USE_SYSLOG
+#include <syslog.h>
+#endif
static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv);
@@ -221,6 +224,11 @@
ngx_ssl_init(log);
#endif
+ /* SYSLOG SUPPORT */
+#ifdef USE_SYSLOG
+ openlog("nginx", LOG_ODELAY, LOG_LOCAL5);
+#endif
+
/* init_cycle->log is required for signal handlers and ngx_getopt() */
ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
@@ -358,6 +366,10 @@
ngx_single_process_cycle(cycle);
}
+#ifdef USE_SYSLOG
+ closelog();
+#endif
+
return 0;
}
diff -ruN src-0.6.32-orig/src/core/ngx_conf_file.c src-0.6.32-syslog/src/core/ngx_conf_file.c
--- src-0.6.32-orig/src/core/ngx_conf_file.c 2008-08-04 17:18:28.000000000 +0200
+++ src-0.6.32-syslog/src/core/ngx_conf_file.c 2008-08-04 17:18:55.000000000 +0200
@@ -751,6 +751,11 @@
full.data = NULL;
#endif
+#ifdef USE_SYSLOG
+if (name) {
+ name = NULL;
+}
+#endif
if (name) {
full = *name;
diff -ruN src-0.6.32-orig/src/core/ngx_log.c src-0.6.32-syslog/src/core/ngx_log.c
--- src-0.6.32-orig/src/core/ngx_log.c 2008-08-04 17:18:28.000000000 +0200
+++ src-0.6.32-syslog/src/core/ngx_log.c 2008-08-05 14:04:52.000000000 +0200
@@ -7,6 +7,9 @@
#include <ngx_config.h>
#include <ngx_core.h>
+#ifdef USE_SYSLOG
+#include <syslog.h>
+#endif
static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
@@ -81,16 +84,22 @@
#endif
u_char errstr[NGX_MAX_ERROR_STR], *p, *last;
+#ifndef USE_SYSLOG
if (log->file->fd == NGX_INVALID_FILE) {
return;
}
+#endif
last = errstr + NGX_MAX_ERROR_STR;
+#ifdef USE_SYSLOG
+ p = errstr;
+#else
ngx_memcpy(errstr, ngx_cached_err_log_time.data,
ngx_cached_err_log_time.len);
p = errstr + ngx_cached_err_log_time.len;
+#endif
p = ngx_snprintf(p, last - p, " [%s] ", err_levels[level]);
@@ -158,7 +167,22 @@
ngx_linefeed(p);
- ngx_write_fd(log->file->fd, errstr, p - errstr);
+#ifdef USE_SYSLOG
+ /* allocate a string which can hold the error message */
+ char *syslogstr;
+
+ if ((syslogstr = calloc((p - errstr + 1), sizeof(char))) != NULL)
+ {
+ strncpy(syslogstr, (char *) errstr, p - errstr);
+
+ /* write to syslog */
+ syslog(LOG_CRIT, "%s", syslogstr);
+
+ free(syslogstr);
+ }
+#else
+ ngx_write_fd(log->file->fd, errstr, p - errstr);
+#endif
}
@@ -242,6 +266,9 @@
ngx_log_t *log;
ngx_str_t *value, *name;
+#ifdef USE_SYSLOG
+ name = value = NULL;
+#else
if (args) {
value = args->elts;
name = &value[1];
@@ -249,6 +276,7 @@
} else {
name = NULL;
}
+#endif
log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
if (log == NULL) {
diff -ruN src-0.6.32-orig/src/http/modules/ngx_http_log_module.c src-0.6.32-syslog/src/http/modules/ngx_http_log_module.c
--- src-0.6.32-orig/src/http/modules/ngx_http_log_module.c 2008-08-04 17:18:28.000000000 +0200
+++ src-0.6.32-syslog/src/http/modules/ngx_http_log_module.c 2008-08-04 18:06:19.000000000 +0200
@@ -9,6 +9,9 @@
#include <ngx_http.h>
#include <nginx.h>
+#ifdef USE_SYSLOG
+#include <syslog.h>
+#endif
typedef struct ngx_http_log_op_s ngx_http_log_op_t;
@@ -155,8 +158,9 @@
NGX_MODULE_V1_PADDING
};
-
+#ifndef USE_SYSLOG
static ngx_str_t ngx_http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
+#endif
static ngx_str_t ngx_http_combined_fmt =
@@ -285,6 +289,20 @@
ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log, u_char *buf,
size_t len)
{
+#ifdef USE_SYSLOG
+ /* allocate a string which can hold the error message */
+ char *syslogstr;
+
+ if ((syslogstr = calloc((len + 1), sizeof(char))) != NULL)
+ {
+ strncpy(syslogstr, (char *) buf, len);
+
+ /* write to syslog */
+ syslog(LOG_NOTICE, "%s", syslogstr);
+
+ free(syslogstr);
+ }
+#else
time_t now;
ssize_t n;
ngx_err_t err;
@@ -322,6 +340,7 @@
log->error_log_time = now;
}
+#endif
}
@@ -654,7 +673,11 @@
return NGX_CONF_ERROR;
}
+#ifdef USE_SYSLOG
+ log->file = ngx_conf_open_file(cf->cycle, NULL);
+#else
log->file = ngx_conf_open_file(cf->cycle, &ngx_http_access_log);
+#endif
if (log->file == NULL) {
return NGX_CONF_ERROR;
}
@@ -706,7 +729,11 @@
return NGX_CONF_ERROR;
}
+#ifdef USE_SYSLOG
+ log->file = ngx_conf_open_file(cf->cycle, NULL);
+#else
log->file = ngx_conf_open_file(cf->cycle, &value[1]);
+#endif
if (log->file == NULL) {
return NGX_CONF_ERROR;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <http://nginx.org/pipermail/nginx/attachments/20080805/2209fb91/attachment.pgp>
More information about the nginx
mailing list