Skip to content
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

Add user event data from logging extra #457

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ def _extra_from_record(record):
}


def _user_from_record(record):
# type: (LogRecord) -> Optional[Dict[str, None]]
user = vars(record).get("user")
if not isinstance(user, dict):
return None
return user


class EventHandler(logging.Handler, object):
def emit(self, record):
# type: (LogRecord) -> Any
Expand Down Expand Up @@ -194,6 +202,7 @@ def _emit(self, record):
event["level"] = _logging_to_event_level(record.levelname)
event["logger"] = record.name
event["logentry"] = {"message": to_string(record.msg), "params": record.args}
event["user"] = _user_from_record(record)
event["extra"] = _extra_from_record(record)

hub.capture_event(event, hint=hint)
Expand Down
26 changes: 26 additions & 0 deletions tests/integrations/logging/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ def test_logging_extra_data_integer_keys(sentry_init, capture_events):
assert event["extra"] == {"1": 1}


def test_logging_user_data(sentry_init, capture_events):
sentry_init(integrations=[LoggingIntegration()], default_integrations=False)
events = capture_events()

logger.critical(
"User error", extra=dict(user=dict(id=123, email="user@example.org"))
)

event, = events

assert event["user"]["id"] == 123
assert event["user"]["email"] == "user@example.org"


def test_logging_invalid_user_data(sentry_init, capture_events):
sentry_init(integrations=[LoggingIntegration()], default_integrations=False)
events = capture_events()

logger.critical("User error", extra=dict(user=123))

event, = events

assert "user" not in event
assert event["extra"]["user"] == 123


@pytest.mark.xfail(sys.version_info[:2] == (3, 4), reason="buggy logging module")
def test_logging_stack(sentry_init, capture_events):
sentry_init(integrations=[LoggingIntegration()], default_integrations=False)
Expand Down