Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Workaround logging.LogRecord.msg type of str #133

Merged
merged 2 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace_packages = true
disallow_any_unimported = true
disallow_any_expr = false
disallow_any_decorated = true
disallow_any_explicit = true
disallow_any_explicit = false
disallow_any_generics = false
disallow_subclassing_any = true

Expand Down
17 changes: 10 additions & 7 deletions src/pythonjsonlogger/jsonlogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import traceback
import importlib

from typing import Dict, Union, List, Tuple
from typing import Any, Dict, Union, List, Tuple

from inspect import istraceback

Expand Down Expand Up @@ -163,7 +163,7 @@ def parse(self) -> List[str]:
else:
return []

def add_fields(self, log_record, record, message_dict):
def add_fields(self, log_record: Dict[str, Any], record: logging.LogRecord, message_dict: Dict[str, Any]) -> None:
"""
Override this method to implement custom logic for adding fields.
"""
Expand Down Expand Up @@ -195,15 +195,17 @@ def jsonify_log_record(self, log_record):
indent=self.json_indent,
ensure_ascii=self.json_ensure_ascii)

def serialize_log_record(self, log_record):
def serialize_log_record(self, log_record: Dict[str, Any]) -> str:
"""Returns the final representation of the log record."""
return "%s%s" % (self.prefix, self.jsonify_log_record(log_record))

def format(self, record):
def format(self, record: logging.LogRecord) -> str:
"""Formats a log record and serializes to json"""
message_dict = {}
if isinstance(record.msg, dict):
message_dict = record.msg
message_dict: Dict[str, Any] = {}
# FIXME: logging.LogRecord.msg and logging.LogRecord.message in typeshed
# are always type of str. We shouldn't need to override that.
if isinstance(record.msg, dict): # type: ignore
message_dict = record.msg # type: ignore
record.message = None
else:
record.message = record.getMessage()
Expand All @@ -226,6 +228,7 @@ def format(self, record):
# Python2.7 doesn't have stack_info.
pass

log_record: Dict[str, Any]
log_record = OrderedDict()
self.add_fields(log_record, record, message_dict)
log_record = self.process_log_record(log_record)
Expand Down