From 927e04ab6d01c46bc8c1c5b734c22528c48edcc5 Mon Sep 17 00:00:00 2001 From: benoitc Date: Tue, 25 Dec 2012 23:05:40 +0100 Subject: [PATCH] add syslog facility name setting --- gunicorn/config.py | 12 ++++++++++++ gunicorn/glogging.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/gunicorn/config.py b/gunicorn/config.py index 6c0023fb..5e2d80ae 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -1277,3 +1277,15 @@ class SyslogPrefix(Setting): All entries will be prefixed by gunicorn.. By default the program name is the name of the process. """ + + +class SyslogFacility(Setting): + name = "syslog_facility" + section = "Logging" + cli = ["--log-syslog-facility"] + meta = "SYSLOG_FACILITY" + validator = validate_string + default = "user" + desc = """\ + Syslog facility name + """ diff --git a/gunicorn/glogging.py b/gunicorn/glogging.py index c3271cdb..ddb8eea7 100644 --- a/gunicorn/glogging.py +++ b/gunicorn/glogging.py @@ -17,6 +17,33 @@ import threading from gunicorn import util from gunicorn.six import string_types + +# syslog facility codes +SYSLOG_FACILITIES = { + "auth": 4, + "authpriv": 10, + "cron": 9, + "daemon": 3, + "ftp": 11, + "kern": 0, + "lpr": 6, + "mail": 2, + "news": 7, + "security": 4, # DEPRECATED + "syslog": 5, + "user": 1, + "uucp": 8, + "local0": 16, + "local1": 17, + "local2": 18, + "local3": 19, + "local4": 20, + "local5": 21, + "local6": 22, + "local7": 23 + } + + CONFIG_DEFAULTS = dict( version=1, disable_existing_loggers=False, @@ -347,13 +374,22 @@ class Logger(object): prefix = cfg.syslog_prefix prefix = "gunicorn.%s" % prefix + + # set format fmt = logging.Formatter(r"%s: %s" % (prefix, fmt)) + # syslog facility + try: + facility = SYSLOG_FACILITIES[cfg.syslog_facility.lower()] + except KeyError: + raise RuntimeError("unknown facility name") + # parse syslog address socktype, addr = parse_syslog_address(cfg.syslog_addr) # finally setup the syslog handler - h = logging.handlers.SysLogHandler(address=addr, socktype=socktype) + h = logging.handlers.SysLogHandler(address=addr, facility=facility, + socktype=socktype) h.setFormatter(fmt) h._gunicorn = True log.addHandler(h)