]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] log: ability to override the syslog tag
authorKevinm <kevinmusker@gmail.com>
Wed, 22 Dec 2010 16:08:21 +0000 (16:08 +0000)
committerWilly Tarreau <w@1wt.eu>
Thu, 30 Dec 2010 10:43:36 +0000 (11:43 +0100)
One of the requirements we have is to run multiple instances of haproxy on a
single host; this is so that we can split the responsibilities (and change
permissions) between product teams. An issue we ran up against is how we
would distinguish between the logs generated by each instance. The solution
we came up with (please let me know if there is a better way) is to override
the application tag written to syslog. We can then configure syslog to write
these to different files.

I have attached a patch adding a global option 'log-tag' to override the
default syslog tag 'haproxy' (actually defaults to argv[0]).

doc/configuration.txt
include/types/global.h
src/cfgparse.c
src/haproxy.c
src/log.c

index daec87958bbde976234f348578270598da908055..699b1c4ee00ed7acbc41b899a9476b34e365ba54 100644 (file)
@@ -535,6 +535,12 @@ log-send-hostname [<string>]
   intermediate syslog server or for simply customizing the hostname printed in
   the logs.
 
+log-tag <string>
+  Sets the tag field in the syslog header to this string. It defaults to the
+  program name as launched from the command line, which usually is "haproxy".
+  Sometimes it can be useful to differentiate between multiple processes
+  running on the same host.
+
 nbproc <number>
   Creates <number> processes when going daemon. This requires the "daemon"
   mode. By default, only one process is created, which is the recommended mode
index faf9bb28d060071e1fcb59b1c26e3ec44fd3f3da..b6c60dd0ac2f90fd011f249b62711cc5e0b4363c 100644 (file)
@@ -76,6 +76,7 @@ struct global {
        char *chroot;
        char *pidfile;
        char *node, *desc;              /* node name & description */
+       char *log_tag;                  /* name for syslog */
        int logfac1, logfac2;
        int loglev1, loglev2;
        int minlvl1, minlvl2;
@@ -108,7 +109,6 @@ struct global {
 };
 
 extern struct global global;
-extern char *progname;          /* program name */
 extern int  pid;                /* current process id */
 extern int  relative_pid;       /* process id starting at 1 */
 extern int  actconn;            /* # of active sessions */
index a67b348384bd1ad83aa4a336da415d17a173648b..28eb1cab7f8c6a622d400b6f3338dbe057d75b1d 100644 (file)
@@ -988,6 +988,15 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
                global.log_send_hostname = malloc(len + 2);
                snprintf(global.log_send_hostname, len + 2, "%s ", name);
        }
+       else if (!strcmp(args[0], "log-tag")) {  /* tag to report to syslog */
+               if (*(args[1]) == 0) {
+                       Alert("parsing [%s:%d] : '%s' expects a tag for use in syslog.\n", file, linenum, args[0]);
+                       err_code |= ERR_ALERT | ERR_FATAL;
+                       goto out;
+               }
+               free(global.log_tag);
+               global.log_tag = strdup(args[1]);
+       }
        else if (!strcmp(args[0], "spread-checks")) {  /* random time between checks (0-50) */
                if (global.spread_checks != 0) {
                        Alert("parsing [%s:%d]: spread-checks already specified. Continuing.\n", file, linenum);
index 9ba08201a811052a0e4ef38afb01afdd233b5b0f..78e10290a0e23b88a4f07cf85f53bb9cb4b9f852 100644 (file)
@@ -94,7 +94,6 @@
 
 /* list of config files */
 static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
-char *progname = NULL;         /* program name */
 int  pid;                      /* current process id */
 int  relative_pid = 1;         /* process id starting at 1 */
 
@@ -352,6 +351,7 @@ void init(int argc, char **argv)
        char *cfg_pidfile = NULL;
        int err_code = 0;
        struct wordlist *wl;
+       char *progname;
 
        /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
         * the string in case of truncation, and at least FreeBSD appears not to do
@@ -405,6 +405,9 @@ void init(int argc, char **argv)
        while ((tmp = strchr(progname, '/')) != NULL)
                progname = tmp + 1;
 
+       /* the process name is used for the logs only */
+       global.log_tag = strdup(progname);
+
        argc--; argv++;
        while (argc > 0) {
                char *flag;
@@ -868,6 +871,7 @@ void deinit(void)
        protocol_unbind_all();
 
        free(global.log_send_hostname); global.log_send_hostname = NULL;
+       free(global.log_tag); global.log_tag = NULL;
        free(global.chroot);  global.chroot = NULL;
        free(global.pidfile); global.pidfile = NULL;
        free(global.node);    global.node = NULL;
index d52727b6f6fdaba07a333b12ccd34a1ee15e0eed..333cbc69df02a6ea8d14548696b97d437fcf88af 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -178,7 +178,7 @@ void send_log(struct proxy *p, int level, const char *message, ...)
        int nbloggers = 0;
        char *log_ptr;
 
-       if (level < 0 || progname == NULL || message == NULL)
+       if (level < 0 || message == NULL)
                return;
 
        if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
@@ -193,7 +193,7 @@ void send_log(struct proxy *p, int level, const char *message, ...)
                                   monthname[tm.tm_mon],
                                   tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
                                   global.log_send_hostname ? global.log_send_hostname : "",
-                                  progname, pid);
+                                  global.log_tag, pid);
                /* WARNING: depending upon implementations, snprintf may return
                 * either -1 or the number of bytes that would be needed to store
                 * the total message. In both cases, we must adjust it.