Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 6 additions & 6 deletions sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,17 @@ def emit(self, record):
if not client.options["_experiments"].get("enable_logs", False):
return

SentryLogsHandler._capture_log_from_record(client, record)
self._capture_log_from_record(client, record)

@staticmethod
def _capture_log_from_record(client, record):
def _capture_log_from_record(self, client, record):
# type: (BaseClient, LogRecord) -> None
scope = sentry_sdk.get_current_scope()
otel_severity_number, otel_severity_text = _python_level_to_otel(record.levelno)
project_root = client.options["project_root"]
attrs = {
"sentry.origin": "auto.logger.log",
} # type: dict[str, str | bool | float | int]
attrs = self._extra_from_record(
record
) # type: dict[str, str | bool | float | int]
attrs["sentry.origin"] = "auto.logger.log"
if isinstance(record.msg, str):
attrs["sentry.message.template"] = record.msg
if record.args is not None:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,27 @@ def test_log_strips_project_root(sentry_init, capture_envelopes):
assert attrs["code.file.path"] == "blah/path.py"


def test_extra_data(sentry_init, capture_envelopes):
"""
The python logger should be able to log extra data
"""
sentry_init(_experiments={"enable_logs": True})
envelopes = capture_envelopes()

python_logger = logging.Logger("test-logger")
python_logger.warning(
"log #%d",
1,
extra={"foo": "bar", "numeric": 42, "more_complex": {"nested": "data"}},
)
get_client().flush()

logs = envelopes_to_logs(envelopes)
assert logs[0]["attributes"]["foo"] == "bar"
assert logs[0]["attributes"]["numeric"] == 42
assert logs[0]["attributes"]["more_complex"] == '{"nested": "data"}'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you assert the attributes is equal to something, to make sure we aren't adding garbage default attributes from the record?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with 1b73d4c



def test_auto_flush_logs_after_100(sentry_init, capture_envelopes):
"""
If you log >100 logs, it should automatically trigger a flush.
Expand Down
Loading