From 8e80328114dd37691499dc758241e630946ac7db Mon Sep 17 00:00:00 2001 From: benoitc Date: Sun, 12 Dec 2010 16:10:58 +0100 Subject: [PATCH] add support for logging configuration using a ini file. It uses the standard Python logging's module Configuration file format (http://docs.python.org/library/logging.html#configuration-file-format) and allows anyone to use his custom file handler. Fix issues #117 and #111. --- examples/logging.conf | 33 +++++++++++++++++++++++++++++++++ gunicorn/app/base.py | 33 ++++++++++++++++++++------------- gunicorn/config.py | 12 ++++++++++++ 3 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 examples/logging.conf diff --git a/examples/logging.conf b/examples/logging.conf new file mode 100644 index 00000000..286f4a39 --- /dev/null +++ b/examples/logging.conf @@ -0,0 +1,33 @@ +[loggers] +keys=root, gunicorn + +[handlers] +keys=console, file + +[formatters] +keys=generic + +[logger_root] +level=INFO +handlers=console + +[logger_gunicorn] +level=DEBUG +handlers=file +propagate=1 +qualname=gunicorn + +[handler_console] +class=StreamHandler +formatter=generic +args=(sys.stdout, ) + +[handler_file] +class=logging.FileHandler +formatter=generic +args=('/tmp/test.log',) + +[formatter_generic] +format="%(asctime)s [%(process)d] [%(levelname)s] %(message)s" +datefmt="%Y-%m-%d %H:%M:%S" +class=logging.Formatter diff --git a/gunicorn/app/base.py b/gunicorn/app/base.py index f0aa34c2..638a21cb 100644 --- a/gunicorn/app/base.py +++ b/gunicorn/app/base.py @@ -5,6 +5,7 @@ import errno import logging +import logging.config import os import sys import traceback @@ -136,19 +137,25 @@ class Application(object): """ self.logger = logging.getLogger('gunicorn') - handlers = [] - if self.cfg.logfile != "-": - handlers.append(logging.FileHandler(self.cfg.logfile)) - else: - handlers.append(logging.StreamHandler()) - - loglevel = self.LOG_LEVELS.get(self.cfg.loglevel.lower(), logging.INFO) - self.logger.setLevel(loglevel) - - format = r"%(asctime)s [%(process)d] [%(levelname)s] %(message)s" + fmt = r"%(asctime)s [%(process)d] [%(levelname)s] %(message)s" datefmt = r"%Y-%m-%d %H:%M:%S" - for h in handlers: - h.setFormatter(logging.Formatter(format, datefmt)) - self.logger.addHandler(h) + if not self.cfg.logconfig: + handlers = [] + if self.cfg.logfile != "-": + handlers.append(logging.FileHandler(self.cfg.logfile)) + else: + handlers.append(logging.StreamHandler()) + + loglevel = self.LOG_LEVELS.get(self.cfg.loglevel.lower(), logging.INFO) + self.logger.setLevel(loglevel) + for h in handlers: + h.setFormatter(logging.Formatter(fmt, datefmt)) + self.logger.addHandler(h) + else: + if os.path.exists(self.cfg.logconfig): + logging.config.fileConfig(self.cfg.logconfig) + else: + raise RuntimeError("Error: logfile '%s' not found." % + self.cfg.logconfig) diff --git a/gunicorn/config.py b/gunicorn/config.py index 642b32dc..44c73469 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -548,6 +548,18 @@ class Loglevel(Setting): * error * critical """ +class LogConfig(Setting): + name = "logconfig" + section = "Logging" + cli = ["--log-config"] + meta = "FILE" + validator = validate_string + default = None + desc = """\ + The log config file to use. Gunicorn uses the standard Python \ +logging module's Configuration file format. + """ + class Procname(Setting): name = "proc_name"