Skip to content

Commit

Permalink
feat(logging): log user name too
Browse files Browse the repository at this point in the history
  • Loading branch information
badayvedat committed Oct 14, 2024
1 parent 4ba761b commit 239bd7f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
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
4 changes: 3 additions & 1 deletion projects/fal/src/fal/logging/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
from fal.auth import USER


def add_user_id(
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"""
user_id: str | None = None
try:
user_id = USER.info.get("sub")
user_name = USER.info.get("nickname")
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
event_dict["usr.name"] = user_name
return event_dict

0 comments on commit 239bd7f

Please sign in to comment.