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
4 changes: 1 addition & 3 deletions structlog_gcp/error_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def __call__(
event_dict[CLOUD_LOGGING_KEY]["@type"] = ERROR_EVENT_TYPE

# https://cloud.google.com/error-reporting/docs/formatting-error-messages
message = event_dict[CLOUD_LOGGING_KEY]["message"]
error_message = f"{message}\n{exception}"
event_dict[CLOUD_LOGGING_KEY]["stack_trace"] = error_message
event_dict[CLOUD_LOGGING_KEY]["stack_trace"] = exception

return event_dict

Expand Down
16 changes: 13 additions & 3 deletions tests/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from typing import Collection

from structlog.processors import CallsiteParameter
from structlog._frames import _format_exception
from structlog.processors import CallsiteParameter, _figure_out_exc_info
from structlog.typing import EventDict, WrappedLogger


Expand Down Expand Up @@ -34,7 +35,16 @@ def __call__(
def format_exc_info(
logger: WrappedLogger, method_name: str, event_dict: EventDict
) -> EventDict:
exc_info = event_dict.pop("exc_info", None)
exc_info = _figure_out_exc_info(event_dict.pop("exc_info", None))

if exc_info:
event_dict["exception"] = "Traceback blabla"
exception = _format_exception(exc_info)
# Format the exception, but only keep the "Traceback ..." and the
# actual exception line, and skip all the rest.
# We need to skip the middle lines as they contain the path to the
# files, which differs depending on which path the library is tested
# from.
head = exception.splitlines()[0]
tail = exception.splitlines()[-1]
event_dict["exception"] = f"{head}\n...\n{tail}"
return event_dict
6 changes: 3 additions & 3 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_exception(stdout: T_stdout, logger: WrappedLogger) -> None:
"foo": "bar",
"severity": "ERROR",
"message": "oh noes",
"stack_trace": "oh noes\nTraceback blabla",
"stack_trace": "Traceback (most recent call last):\n...\nZeroDivisionError: division by zero",
"time": "2023-04-01T08:00:00.000000Z",
}
assert msg == expected
Expand Down Expand Up @@ -191,7 +191,7 @@ def test_core_processors_only(
assert "" == output.err
msg = output.out.strip()

# No JSON formmating, no contextvars
# No JSON formatting, no contextvars
expected = "message='test' time='2023-04-01T08:00:00.000000Z' severity='INFO' logging.googleapis.com/sourceLocation={'file': '/app/test.py', 'line': '42', 'function': 'test:test123'}"

assert expected == msg
Expand Down Expand Up @@ -225,7 +225,7 @@ def test_exception_different_level(stdout: T_stdout, logger: WrappedLogger) -> N
},
"severity": "WARNING",
"message": "oh no; anyways",
"stack_trace": "oh no; anyways\ndivision by zero",
"stack_trace": "ZeroDivisionError('division by zero')",
"time": "2023-04-01T08:00:00.000000Z",
}
assert msg == expected
Expand Down