Skip to content

Commit 9c54582

Browse files
committed
simplified log kwargs
1 parent f0c9481 commit 9c54582

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

packages/service-library/src/servicelib/aiohttp/rest_middlewares.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
from aiohttp.web_request import Request
1414
from aiohttp.web_response import StreamResponse
1515
from models_library.error_codes import create_error_code
16-
from models_library.errors_classes import OsparcErrorMixin
1716
from models_library.utils.json_serialization import json_dumps
1817

19-
from ..logging_errors import create_troubleshotting_log_message, get_log_record_extra
18+
from ..logging_errors import create_troubleshotting_log_kwargs
2019
from ..mimetype_constants import MIMETYPE_APPLICATION_JSON
2120
from ..utils import is_production_environ
2221
from .rest_models import ErrorItemType, ErrorType, LogMessageType
@@ -59,31 +58,22 @@ def _process_and_raise_unexpected_error(request: web.BaseRequest, err: Exception
5958
"request.method": f"{request.method}",
6059
"request.path": f"{request.path}",
6160
}
62-
if isinstance(err, OsparcErrorMixin):
63-
error_context.update(err.error_context())
6461

6562
frontend_msg = _FMSG_INTERNAL_ERROR_USER_FRIENDLY_WITH_OEC.format(
6663
error_code=error_code
6764
)
68-
log_msg = create_troubleshotting_log_message(
69-
message_to_user=frontend_msg,
70-
exception=err,
71-
error_code=error_code,
72-
error_context=error_context,
73-
)
74-
7565
http_error = create_http_error(
7666
err,
7767
frontend_msg,
7868
web.HTTPInternalServerError,
7969
skip_internal_error_details=_is_prod,
8070
)
8171
_logger.exception(
82-
log_msg,
83-
extra=get_log_record_extra(
84-
error_code=error_code,
85-
user_id=error_context.get("user_id"),
86-
),
72+
**create_troubleshotting_log_kwargs(
73+
message_to_user=frontend_msg,
74+
exception=err,
75+
error_context=error_context,
76+
)
8777
)
8878
raise http_error
8979

packages/service-library/tests/test_logging_errors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
create_troubleshotting_log_kwargs,
1010
create_troubleshotting_log_message,
1111
)
12-
from servicelib.logging_utils import get_log_record_extra
1312

1413

1514
def test_create_troubleshotting_log_message(caplog: pytest.LogCaptureFixture):
@@ -41,12 +40,13 @@ class MyError(OsparcErrorMixin, RuntimeError):
4140
)
4241

4342
assert log_kwargs["msg"] == log_msg
43+
assert (
44+
log_kwargs["extra"]["log_uid"] == "123"
45+
), "user_id is injected as extra from context"
4446

4547
with caplog.at_level(logging.WARNING):
4648
root_logger = logging.getLogger()
47-
root_logger.exception(
48-
log_msg, extra=get_log_record_extra(error_code=error_code)
49-
)
49+
root_logger.exception(**log_kwargs)
5050

5151
# ERROR root:test_logging_utils.py:417 Nice message to user [OEC:126055703573984].
5252
# {

0 commit comments

Comments
 (0)