Skip to content
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
12 changes: 11 additions & 1 deletion lite_bootstrap/instruments/logging_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
if import_checker.is_structlog_installed:
import orjson
import structlog
from structlog.processors import ExceptionRenderer


ScopeType = typing.MutableMapping[str, typing.Any]
Expand Down Expand Up @@ -96,6 +97,15 @@ class LoggingConfig(BaseConfig):
)


class CustomExceptionRenderer(ExceptionRenderer):
def __call__(self, logger: "WrappedLogger", name: str, event_dict: "EventDict") -> "EventDict":
exc_info = event_dict.get("exc_info")
event_dict = super().__call__(logger=logger, name=name, event_dict=event_dict)
if exc_info:
event_dict["exc_info"] = exc_info
return event_dict


@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
class LoggingInstrument(BaseInstrument):
bootstrap_config: LoggingConfig
Expand All @@ -111,7 +121,7 @@ def structlog_pre_chain_processors(self) -> list[typing.Any]:
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
CustomExceptionRenderer(),
structlog.processors.UnicodeDecoder(),
]

Expand Down
5 changes: 5 additions & 0 deletions tests/instruments/test_logging_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def test_logging_instrument_simple() -> None:
try:
logging_instrument.bootstrap()
logger.info("testing structlog", key="value")
try:
msg = "some error"
raise ValueError(msg) # noqa: TRY301
except ValueError:
logger.exception("logging error")
std_logger.info("testing std logger", extra={"key": "value"})
finally:
logging_instrument.teardown()
Expand Down
Loading