mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
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:
parent
a8e34ac16c
commit
8e80328114
33
examples/logging.conf
Normal file
33
examples/logging.conf
Normal 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
|
||||||
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
import errno
|
import errno
|
||||||
import logging
|
import logging
|
||||||
|
import logging.config
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
@ -136,19 +137,25 @@ class Application(object):
|
|||||||
"""
|
"""
|
||||||
self.logger = logging.getLogger('gunicorn')
|
self.logger = logging.getLogger('gunicorn')
|
||||||
|
|
||||||
handlers = []
|
fmt = r"%(asctime)s [%(process)d] [%(levelname)s] %(message)s"
|
||||||
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"
|
|
||||||
datefmt = r"%Y-%m-%d %H:%M:%S"
|
datefmt = r"%Y-%m-%d %H:%M:%S"
|
||||||
for h in handlers:
|
if not self.cfg.logconfig:
|
||||||
h.setFormatter(logging.Formatter(format, datefmt))
|
handlers = []
|
||||||
self.logger.addHandler(h)
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -548,6 +548,18 @@ class Loglevel(Setting):
|
|||||||
* error
|
* error
|
||||||
* critical
|
* 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):
|
class Procname(Setting):
|
||||||
name = "proc_name"
|
name = "proc_name"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user