Fail save json dump for `structlog_convert_to_json_for_new_relic`
This commit is contained in:
parent
2c4ae02c4d
commit
a7f7d0b184
|
|
@ -8,7 +8,10 @@ import structlog
|
|||
from environs import Env
|
||||
|
||||
from vbv_lernwelt.core.constants import DEFAULT_RICH_TEXT_FEATURES
|
||||
from vbv_lernwelt.core.utils import structlog_add_app_info, structlog_add_to_message
|
||||
from vbv_lernwelt.core.utils import (
|
||||
structlog_add_app_info,
|
||||
structlog_convert_to_json_for_new_relic,
|
||||
)
|
||||
|
||||
SERVER_ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
|
||||
APPS_DIR = SERVER_ROOT_DIR / "vbv_lernwelt"
|
||||
|
|
@ -474,7 +477,7 @@ else:
|
|||
structlog.processors.StackInfoRenderer(),
|
||||
structlog.processors.format_exc_info,
|
||||
structlog.processors.UnicodeDecoder(),
|
||||
structlog_add_to_message,
|
||||
structlog_convert_to_json_for_new_relic,
|
||||
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
|
||||
],
|
||||
context_class=dict,
|
||||
|
|
|
|||
|
|
@ -18,13 +18,26 @@ def structlog_add_app_info(
|
|||
return event_dict
|
||||
|
||||
|
||||
def structlog_add_to_message(_: logging.Logger, __: str, event_dict: EventDict) -> str:
|
||||
"""
|
||||
The *event_dict* is added as dict ``message``.
|
||||
class FailSafeJSONEncoder(json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
try:
|
||||
return super(FailSafeJSONEncoder, self).default(obj)
|
||||
except Exception:
|
||||
return str(obj)
|
||||
|
||||
This allows you to defer formatting to `logging`.
|
||||
|
||||
def safe_json_dumps(data, **kwargs):
|
||||
return json.dumps(data, cls=FailSafeJSONEncoder, **kwargs)
|
||||
|
||||
|
||||
def structlog_convert_to_json_for_new_relic(
|
||||
_: logging.Logger, __: str, event_dict: EventDict
|
||||
) -> str:
|
||||
"""
|
||||
return json.dumps(event_dict)
|
||||
The *event_dict* is serialized to a json string, so that in New Relic logs
|
||||
the nested keys will show up as attributes.
|
||||
"""
|
||||
return safe_json_dumps(event_dict)
|
||||
|
||||
|
||||
def structlog_inject_context_dict(test, level, event_dict):
|
||||
|
|
|
|||
Loading…
Reference in New Issue