Skip to content

feat(logs): Forward extra from logger as attributes #4374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2025
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
10 changes: 4 additions & 6 deletions sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,15 @@ 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: Any
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
74 changes: 73 additions & 1 deletion tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _convert_attr(attr):
return attr["value"]
if attr["value"].startswith("{"):
try:
return json.loads(attr["stringValue"])
return json.loads(attr["value"])
except ValueError:
pass
return str(attr["value"])
Expand Down Expand Up @@ -393,6 +393,78 @@ def test_log_strips_project_root(sentry_init, capture_envelopes):
assert attrs["code.file.path"] == "blah/path.py"


def test_logger_with_all_attributes(sentry_init, capture_envelopes):
"""
The python logger should be able to log all attributes, including 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)

attributes = logs[0]["attributes"]

assert "process.pid" in attributes
assert isinstance(attributes["process.pid"], int)
del attributes["process.pid"]

assert "sentry.release" in attributes
assert isinstance(attributes["sentry.release"], str)
del attributes["sentry.release"]

assert "server.address" in attributes
assert isinstance(attributes["server.address"], str)
del attributes["server.address"]

assert "thread.id" in attributes
assert isinstance(attributes["thread.id"], int)
del attributes["thread.id"]

assert "code.file.path" in attributes
assert isinstance(attributes["code.file.path"], str)
del attributes["code.file.path"]

assert "code.function.name" in attributes
assert isinstance(attributes["code.function.name"], str)
del attributes["code.function.name"]

assert "code.line.number" in attributes
assert isinstance(attributes["code.line.number"], int)
del attributes["code.line.number"]

assert "process.executable.name" in attributes
assert isinstance(attributes["process.executable.name"], str)
del attributes["process.executable.name"]

assert "thread.name" in attributes
assert isinstance(attributes["thread.name"], str)
del attributes["thread.name"]

# Assert on the remaining non-dynamic attributes.
assert attributes == {
"foo": "bar",
"numeric": 42,
"more_complex": "{'nested': 'data'}",
"logger.name": "test-logger",
"sentry.origin": "auto.logger.log",
"sentry.message.template": "log #%d",
"sentry.message.parameters.0": 1,
"sentry.environment": "production",
"sentry.sdk.name": "sentry.python",
"sentry.sdk.version": VERSION,
"sentry.severity_number": 13,
"sentry.severity_text": "warn",
}


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