Skip to content

Commit

Permalink
added capability to take JSON as log's config
Browse files Browse the repository at this point in the history
  • Loading branch information
kingkuong committed Nov 18, 2018
1 parent c66957b commit e98f6a0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
21 changes: 16 additions & 5 deletions docs/source/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ The log config file to use.
Gunicorn uses the standard Python logging module's Configuration
file format.

.. _logconfig-json:

logiconfig_json
~~~~~~~~~

* ``--log-config-json FILE``
* ``None``

The log config file written in JSON.

.. _logconfig-dict:

logconfig_dict
Expand All @@ -274,8 +284,9 @@ logconfig_dict

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.
takes precedence over the :ref:`logconfig` and :ref:`logConfigJson` options, which uses the
older file configuration format and JSON respectively.


Format: https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig

Expand Down Expand Up @@ -1173,11 +1184,11 @@ libraries may be installed using setuptools' ``extra_require`` feature.
A string referring to one of the following bundled classes:

* ``sync``
* ``eventlet`` - Requires eventlet >= 0.9.7 (or install it via
* ``eventlet`` - Requires eventlet >= 0.9.7 (or install it via
``pip install gunicorn[eventlet]``)
* ``gevent`` - Requires gevent >= 0.13 (or install it via
* ``gevent`` - Requires gevent >= 0.13 (or install it via
``pip install gunicorn[gevent]``)
* ``tornado`` - Requires tornado >= 0.2 (or install it via
* ``tornado`` - Requires tornado >= 0.2 (or install it via
``pip install gunicorn[tornado]``)
* ``gthread`` - Python 2 requires the futures package to be installed
(or install it via ``pip install gunicorn[gthread]``)
Expand Down
28 changes: 22 additions & 6 deletions gunicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,11 @@ class WorkerClass(Setting):
A string referring to one of the following bundled classes:
* ``sync``
* ``eventlet`` - Requires eventlet >= 0.9.7 (or install it via
* ``eventlet`` - Requires eventlet >= 0.9.7 (or install it via
``pip install gunicorn[eventlet]``)
* ``gevent`` - Requires gevent >= 0.13 (or install it via
* ``gevent`` - Requires gevent >= 0.13 (or install it via
``pip install gunicorn[gevent]``)
* ``tornado`` - Requires tornado >= 0.2 (or install it via
* ``tornado`` - Requires tornado >= 0.2 (or install it via
``pip install gunicorn[tornado]``)
* ``gthread`` - Python 2 requires the futures package to be installed
(or install it via ``pip install gunicorn[gthread]``)
Expand Down Expand Up @@ -668,7 +668,7 @@ class WorkerThreads(Setting):
If it is not defined, the default is ``1``.
This setting only affects the Gthread worker type.
.. note::
If you try to use the ``sync`` worker type and set the ``threads``
setting to more than 1, the ``gthread`` worker type will be used
Expand Down Expand Up @@ -1368,15 +1368,31 @@ class LogConfigDict(Setting):
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.
takes precedence over the :ref:`logconfig` and :ref:`logConfigJson` options, which uses the
older file configuration format and JSON respectively.
Format: https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig
.. versionadded:: 19.8
"""


class LogConfigJson(Setting):
name = "logconfig_json"
section = "Logging"
cli = ["--log-config-json"]
meta = "FILE"
validator = validate_string
default = None
desc = """\
The log config JSON reads config from a JSON file
Format: https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig
.. versionadded:: 19.9
"""


class SyslogTo(Setting):
name = "syslog_addr"
section = "Logging"
Expand Down
18 changes: 17 additions & 1 deletion gunicorn/glogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import base64
import binascii
import json
import time
import logging
logging.Logger.manager.emittedNoHandlerWarning = 1
Expand Down Expand Up @@ -238,6 +239,21 @@ def setup(self, cfg):
TypeError
) as exc:
raise RuntimeError(str(exc))
elif cfg.logconfig_json:
config = CONFIG_DEFAULTS.copy()
if os.path.exists(cfg.logconfig_json):
try:
config_json = json.load(open(cfg.logconfig_json))
config.update(config_json)
dictConfig(config)
except(
json.JSONDecodeError,
AttributeError,
ImportError,
ValueError,
TypeError
) as exc:
raise RuntimeError(str(exc))
elif cfg.logconfig:
if os.path.exists(cfg.logconfig):
defaults = CONFIG_DEFAULTS.copy()
Expand Down Expand Up @@ -330,7 +346,7 @@ def access(self, resp, req, environ, request_time):
"""

if not (self.cfg.accesslog or self.cfg.logconfig or
self.cfg.logconfig_dict or
self.cfg.logconfig_dict or self.cfg.logconfig_json or
(self.cfg.syslog and not self.cfg.disable_redirect_access_to_syslog)):
return

Expand Down

0 comments on commit e98f6a0

Please sign in to comment.