From 239bd7f8b41a32a465914ea66758c397994d6f3e Mon Sep 17 00:00:00 2001 From: badayvedat Date: Mon, 14 Oct 2024 18:25:25 +0300 Subject: [PATCH 1/3] feat(logging): log user name too --- projects/fal/src/fal/logging/__init__.py | 4 ++-- projects/fal/src/fal/logging/user.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/projects/fal/src/fal/logging/__init__.py b/projects/fal/src/fal/logging/__init__.py index c3788691..5b565153 100644 --- a/projects/fal/src/fal/logging/__init__.py +++ b/projects/fal/src/fal/logging/__init__.py @@ -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 @@ -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, diff --git a/projects/fal/src/fal/logging/user.py b/projects/fal/src/fal/logging/user.py index 4a76f8bf..39779a7f 100644 --- a/projects/fal/src/fal/logging/user.py +++ b/projects/fal/src/fal/logging/user.py @@ -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 From a3ad89d1d9f2ab534d3b5557c3dfb85da08bb8cc Mon Sep 17 00:00:00 2001 From: badayvedat Date: Mon, 14 Oct 2024 18:27:21 +0300 Subject: [PATCH 2/3] style: some spacing --- projects/fal/src/fal/logging/user.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/projects/fal/src/fal/logging/user.py b/projects/fal/src/fal/logging/user.py index 39779a7f..44472548 100644 --- a/projects/fal/src/fal/logging/user.py +++ b/projects/fal/src/fal/logging/user.py @@ -18,6 +18,8 @@ def add_user_info( # 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 From 6cae046f70827e9f9328cdcc8c50a038036f7c35 Mon Sep 17 00:00:00 2001 From: badayvedat Date: Mon, 14 Oct 2024 18:29:20 +0300 Subject: [PATCH 3/3] fix: --- projects/fal/src/fal/logging/user.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/projects/fal/src/fal/logging/user.py b/projects/fal/src/fal/logging/user.py index 44472548..88070cd8 100644 --- a/projects/fal/src/fal/logging/user.py +++ b/projects/fal/src/fal/logging/user.py @@ -5,21 +5,22 @@ from fal.auth import USER -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 +def get_key_from_user_info(key: str) -> str | None: try: - user_id = USER.info.get("sub") - user_name = USER.info.get("nickname") + 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 + 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"] = user_id - event_dict["usr.name"] = user_name + event_dict["usr.id"] = get_key_from_user_info("sub") + event_dict["usr.name"] = get_key_from_user_info("nickname") return event_dict