diff --git a/doc/configuration.txt b/doc/configuration.txt index daec87958..699b1c4ee 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 faf9bb28d..b6c60dd0a 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 a67b34838..28eb1cab7 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 9ba08201a..78e10290a 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 d52727b6f..333cbc69d 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.