From 48936af9a2c7e3b49d8546e5b01332a10bdefc6b Mon Sep 17 00:00:00 2001 From: Kevinm Date: Wed, 22 Dec 2010 16:08:21 +0000 Subject: [PATCH] [MINOR] log: ability to override the syslog tag 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 | 6 ++++++ include/types/global.h | 2 +- src/cfgparse.c | 9 +++++++++ src/haproxy.c | 6 +++++- src/log.c | 4 ++-- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/doc/configuration.txt b/doc/configuration.txt index daec87958b..699b1c4ee0 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -535,6 +535,12 @@ log-send-hostname [] intermediate syslog server or for simply customizing the hostname printed in the logs. +log-tag + 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 Creates processes when going daemon. This requires the "daemon" mode. By default, only one process is created, which is the recommended mode diff --git a/include/types/global.h b/include/types/global.h index faf9bb28d0..b6c60dd0ac 100644 --- a/include/types/global.h +++ b/include/types/global.h @@ -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 */ diff --git a/src/cfgparse.c b/src/cfgparse.c index a67b348384..28eb1cab7f 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -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); diff --git a/src/haproxy.c b/src/haproxy.c index 9ba08201a8..78e10290a0 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -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; diff --git a/src/log.c b/src/log.c index d52727b6f6..333cbc69df 100644 --- 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. -- 2.39.5