Skip to content

Commit

Permalink
Merge pull request benoitc#1602 from benoitc/feature/1087-dict-config
Browse files Browse the repository at this point in the history
Support Dictionary Config setting.
  • Loading branch information
tilgovi authored Oct 30, 2017
2 parents ad23ddd + f1af184 commit 70fa58d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
18 changes: 18 additions & 0 deletions gunicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,24 @@ class LogConfig(Setting):
"""


class LogConfigDict(Setting):
name = "logconfig_dict"
section = "Logging"
cli = ["--log-config-dict"]
validator = validate_dict
default = {}
desc = """\
The log config dictionary to use, using the standard Python
logging module's dictionary configuration format. This option
takes precedence over the :ref:`logconfig` option, which uses the
older file configuration format.
Format: https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig
.. versionadded:: 19.8
"""


class SyslogTo(Setting):
name = "syslog_addr"
section = "Logging"
Expand Down
23 changes: 22 additions & 1 deletion gunicorn/glogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import logging
logging.Logger.manager.emittedNoHandlerWarning = 1
from logging.config import fileConfig
try:
from logging.config import dictConfig
except ImportError:
# python 2.6
dictConfig = None
import os
import socket
import sys
Expand Down Expand Up @@ -226,7 +231,23 @@ def setup(self, cfg):
self.access_log, cfg, self.syslog_fmt, "access"
)

if cfg.logconfig:
if dictConfig is None and cfg.logconfig_dict:
util.warn("Dictionary-based log configuration requires "
"Python 2.7 or above.")

if dictConfig and cfg.logconfig_dict:
config = CONFIG_DEFAULTS.copy()
config.update(cfg.logconfig_dict)
try:
dictConfig(config)
except (
AttributeError,
ImportError,
ValueError,
TypeError
) as exc:
raise RuntimeError(str(exc))
elif cfg.logconfig:
if os.path.exists(cfg.logconfig):
defaults = CONFIG_DEFAULTS.copy()
defaults['__file__'] = cfg.logconfig
Expand Down

0 comments on commit 70fa58d

Please sign in to comment.