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.
This commit is contained in:
benoitc 2010-12-12 16:10:58 +01:00
parent a8e34ac16c
commit 8e80328114
3 changed files with 65 additions and 13 deletions

33
examples/logging.conf Normal file
View File

@ -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

View File

@ -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)

View File

@ -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"