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

feat(logging): log user name too #330

Closed
wants to merge 3 commits 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
4 changes: 2 additions & 2 deletions projects/fal/src/fal/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from structlog.typing import EventDict, WrappedLogger

from .style import LEVEL_STYLES
from .user import add_user_id
from .user import add_user_info

# Unfortunately structlog console processor does not support
# more general theming as a public API. Consider a PR on the
Expand Down Expand Up @@ -43,7 +43,7 @@ def set_debug_logging(debug: bool):
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S"),
structlog.processors.StackInfoRenderer(),
add_user_id,
add_user_info,
_console_log_output,
],
wrapper_class=structlog.stdlib.BoundLogger,
Expand Down
21 changes: 13 additions & 8 deletions projects/fal/src/fal/logging/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
from fal.auth import USER


def add_user_id(
logger: WrappedLogger, method_name: str, event_dict: EventDict
) -> EventDict:
"""The structlog processor that sends the logged user id on every log"""
user_id: str | None = None
def get_key_from_user_info(key: str) -> str | None:
try:
user_id = USER.info.get("sub")
return USER.info.get(key)
except Exception:
# logs are fail-safe, so any exception is safe to ignore
# this is expected to happen only when user is logged out
# or there's no internet connection
pass
event_dict["usr.id"] = user_id
return None


def add_user_info(
logger: WrappedLogger, method_name: str, event_dict: EventDict
) -> EventDict:
"""The structlog processor that sends the logged user id on every log"""

event_dict["usr.id"] = get_key_from_user_info("sub")
event_dict["usr.name"] = get_key_from_user_info("nickname")

return event_dict
Loading