syslog support for 0.7.27 (should work on other 0.7.x versions as well)

Xavier Grangier grangier at gmail.com
Tue Dec 16 20:02:52 MSK 2008


Hello,

Thanks for the patch.
I'd love to be able to syslog only errors base on the error state like  
[error,crit]
In order for exemple to syslog only gateway timeout or fastcgi  
connection errors
I tried to hack your patch without luck :p

Thanks for your work

xav

Le 16 déc. 08 à 17:38, Marlon de Boer a écrit :

> Hi,
>
> Attached you can find a patch to enable syslog for nginx 0.7.27. It's
> based on my original patch for the 0.6.x branch.
>
> Regards
> Marlon de Boer
> System Engineer www.hyves.nl
> diff -ruN nginx-0.7.27/auto/make nginx-0.7.27.patched/auto/make
> --- nginx-0.7.27/auto/make	2008-03-18 11:36:27.000000000 +0100
> +++ nginx-0.7.27.patched/auto/make	2008-12-16 16:45:21.000000000 +0100
> @@ -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 nginx-0.7.27/auto/options nginx-0.7.27.patched/auto/options
> --- nginx-0.7.27/auto/options	2008-11-10 16:22:33.000000000 +0100
> +++ nginx-0.7.27.patched/auto/options	2008-12-16 16:45:21.000000000  
> +0100
> @@ -106,6 +106,8 @@
> MD5_OPT=
> MD5_ASM=NO
>
> +USE_SYSLOG=NO
> +
> USE_SHA1=NO
> SHA1=NONE
> SHA1_OPT=
> @@ -237,6 +239,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               ;;
> @@ -358,6 +362,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 nginx-0.7.27/auto/summary nginx-0.7.27.patched/auto/summary
> --- nginx-0.7.27/auto/summary	2008-05-16 16:32:58.000000000 +0200
> +++ nginx-0.7.27.patched/auto/summary	2008-12-16 16:45:21.000000000  
> +0100
> @@ -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 nginx-0.7.27/src/core/nginx.c nginx-0.7.27.patched/src/ 
> core/nginx.c
> --- nginx-0.7.27/src/core/nginx.c	2008-11-11 17:17:45.000000000 +0100
> +++ nginx-0.7.27.patched/src/core/nginx.c	2008-12-16  
> 16:45:21.000000000 +0100
> @@ -8,6 +8,9 @@
> #include <ngx_core.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);
> @@ -222,6 +225,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));
> @@ -366,6 +374,10 @@
>         ngx_single_process_cycle(cycle);
>     }
>
> +#ifdef USE_SYSLOG
> +    closelog();
> +#endif
> +
>     return 0;
> }
>
> diff -ruN nginx-0.7.27/src/core/ngx_conf_file.c nginx-0.7.27.patched/ 
> src/core/ngx_conf_file.c
> --- nginx-0.7.27/src/core/ngx_conf_file.c	2008-11-25  
> 16:55:10.000000000 +0100
> +++ nginx-0.7.27.patched/src/core/ngx_conf_file.c	2008-12-16  
> 16:45:21.000000000 +0100
> @@ -851,6 +851,11 @@
>     full.data = NULL;
> #endif
>
> +#ifdef USE_SYSLOG
> +if (name) {
> +		name = NULL;
> +}
> +#endif
>     if (name) {
>         full = *name;
>
> diff -ruN nginx-0.7.27/src/core/ngx_log.c nginx-0.7.27.patched/src/ 
> core/ngx_log.c
> --- nginx-0.7.27/src/core/ngx_log.c	2008-12-09 18:41:17.000000000  
> +0100
> +++ nginx-0.7.27.patched/src/core/ngx_log.c	2008-12-16  
> 16:51:10.000000000 +0100
> @@ -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]);
>
> @@ -150,10 +159,24 @@
>
>     ngx_linefeed(p);
>
> -    (void) 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
> +       (void) ngx_write_fd(log->file->fd, errstr, p - errstr);
> +#endif
> }
>
> -
> #if !(NGX_HAVE_VARIADIC_MACROS)
>
> void ngx_cdecl
> @@ -234,6 +257,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];
> @@ -241,6 +267,7 @@
>     } else {
>         name = NULL;
>     }
> +#endif
>
>     log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
>     if (log == NULL) {
> diff -ruN nginx-0.7.27/src/http/modules/ngx_http_log_module.c  
> nginx-0.7.27.patched/src/http/modules/ngx_http_log_module.c
> --- nginx-0.7.27/src/http/modules/ngx_http_log_module.c	2008-10-16  
> 15:31:00.000000000 +0200
> +++ nginx-0.7.27.patched/src/http/modules/ngx_http_log_module.c	 
> 2008-12-16 17:05:33.000000000 +0100
> @@ -8,6 +8,9 @@
> #include <ngx_core.h>
> #include <ngx_http.h>
>
> +#ifdef USE_SYSLOG
> +#include <syslog.h>
> +#endif
>
> typedef struct ngx_http_log_op_s  ngx_http_log_op_t;
>
> @@ -178,8 +181,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 =
> @@ -310,6 +314,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
>     u_char     *name;
>     time_t      now;
>     ssize_t     n;
> @@ -354,6 +372,7 @@
>
>         log->error_log_time = now;
>     }
> +#endif
> }
>
>
> @@ -798,7 +817,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;
>     }
> @@ -857,7 +880,11 @@
>     n = ngx_http_script_variables_count(&value[1]);
>
>     if (n == 0) {
> +#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;
>         }






More information about the nginx mailing list