Skip to content

Commit

Permalink
Merge pull request #1917 from cuongtranx/1775-support-log-config-json
Browse files Browse the repository at this point in the history
added capability to take JSON as log's config
  • Loading branch information
benoitc committed May 7, 2023
2 parents 96ab8ba + db9de01 commit 400d847
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
15 changes: 13 additions & 2 deletions docs/source/settings.rst
Expand Up @@ -305,6 +305,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 @@ -316,8 +326,9 @@ file format.

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
21 changes: 19 additions & 2 deletions gunicorn/config.py
Expand Up @@ -1507,15 +1507,32 @@ 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 to read config from a JSON file
Format: https://docs.python.org/3/library/logging.config.html#logging.config.jsonConfig
.. versionadded:: 20.0
"""


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

import base64
import binascii
import json
import time
import logging
logging.Logger.manager.emittedNoHandlerWarning = 1 # noqa
Expand Down Expand Up @@ -239,6 +240,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 @@ -333,7 +349,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 400d847

Please sign in to comment.