Merge pull request #1917 from cuongtranx/1775-support-log-config-json

added capability to take JSON as log's config
This commit is contained in:
Benoit Chesneau 2023-05-07 16:21:32 +02:00 committed by GitHub
commit 400d84733a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 5 deletions

View File

@ -305,6 +305,16 @@ The log config file to use.
Gunicorn uses the standard Python logging module's Configuration Gunicorn uses the standard Python logging module's Configuration
file format. file format.
.. _logconfig-json:
logiconfig_json
~~~~~~~~~
* ``--log-config-json FILE``
* ``None``
The log config file written in JSON.
.. _logconfig-dict: .. _logconfig-dict:
``logconfig_dict`` ``logconfig_dict``
@ -316,8 +326,9 @@ file format.
The log config dictionary to use, using the standard Python The log config dictionary to use, using the standard Python
logging module's dictionary configuration format. This option logging module's dictionary configuration format. This option
takes precedence over the :ref:`logconfig` option, which uses the takes precedence over the :ref:`logconfig` and :ref:`logConfigJson` options, which uses the
older file configuration format. older file configuration format and JSON respectively.
Format: https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig Format: https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig

View File

@ -1507,8 +1507,9 @@ class LogConfigDict(Setting):
desc = """\ desc = """\
The log config dictionary to use, using the standard Python The log config dictionary to use, using the standard Python
logging module's dictionary configuration format. This option logging module's dictionary configuration format. This option
takes precedence over the :ref:`logconfig` option, which uses the takes precedence over the :ref:`logconfig` and :ref:`logConfigJson` options,
older file configuration format. which uses the older file configuration format and JSON
respectively.
Format: https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig Format: https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig
@ -1516,6 +1517,22 @@ class LogConfigDict(Setting):
""" """
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): class SyslogTo(Setting):
name = "syslog_addr" name = "syslog_addr"
section = "Logging" section = "Logging"

View File

@ -5,6 +5,7 @@
import base64 import base64
import binascii import binascii
import json
import time import time
import logging import logging
logging.Logger.manager.emittedNoHandlerWarning = 1 # noqa logging.Logger.manager.emittedNoHandlerWarning = 1 # noqa
@ -239,6 +240,21 @@ class Logger(object):
TypeError TypeError
) as exc: ) as exc:
raise RuntimeError(str(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: elif cfg.logconfig:
if os.path.exists(cfg.logconfig): if os.path.exists(cfg.logconfig):
defaults = CONFIG_DEFAULTS.copy() defaults = CONFIG_DEFAULTS.copy()
@ -333,7 +349,7 @@ class Logger(object):
""" """
if not (self.cfg.accesslog or self.cfg.logconfig or 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)): (self.cfg.syslog and not self.cfg.disable_redirect_access_to_syslog)):
return return