Skip to content

Commit

Permalink
feat(loguru): add message format configuration arguments (#2208)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gwill authored Jun 30, 2023
1 parent ac71829 commit 0919a95
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
20 changes: 16 additions & 4 deletions sentry_sdk/integrations/loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
from typing import Optional, Tuple

try:
import loguru
from loguru import logger
from loguru._defaults import LOGURU_FORMAT as DEFAULT_FORMAT
except ImportError:
raise DidNotEnable("LOGURU is not installed")

Expand All @@ -42,8 +44,14 @@ class LoggingLevels(enum.IntEnum):
class LoguruIntegration(Integration):
identifier = "loguru"

def __init__(self, level=DEFAULT_LEVEL, event_level=DEFAULT_EVENT_LEVEL):
# type: (Optional[int], Optional[int]) -> None
def __init__(
self,
level=DEFAULT_LEVEL,
event_level=DEFAULT_EVENT_LEVEL,
breadcrumb_format=DEFAULT_FORMAT,
event_format=DEFAULT_FORMAT,
):
# type: (Optional[int], Optional[int], str | loguru.FormatFunction, str | loguru.FormatFunction) -> None
global _ADDED_HANDLERS
breadcrumb_handler, event_handler = _ADDED_HANDLERS

Expand All @@ -56,12 +64,16 @@ def __init__(self, level=DEFAULT_LEVEL, event_level=DEFAULT_EVENT_LEVEL):

if level is not None:
breadcrumb_handler = logger.add(
LoguruBreadcrumbHandler(level=level), level=level
LoguruBreadcrumbHandler(level=level),
level=level,
format=breadcrumb_format,
)

if event_level is not None:
event_handler = logger.add(
LoguruEventHandler(level=event_level), level=event_level
LoguruEventHandler(level=event_level),
level=event_level,
format=event_format,
)

_ADDED_HANDLERS = (breadcrumb_handler, event_handler)
Expand Down
40 changes: 40 additions & 0 deletions tests/integrations/loguru/test_loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,43 @@ def test_just_log(
assert event["level"] == (level.name.lower())
assert event["logger"] == "tests.integrations.loguru.test_loguru"
assert event["logentry"]["message"][23:] == formatted_message


def test_breadcrumb_format(sentry_init, capture_events):
sentry_init(
integrations=[
LoguruIntegration(
level=LoggingLevels.INFO.value,
event_level=None,
breadcrumb_format="{message}",
)
],
default_integrations=False,
)

logger.info("test")
formatted_message = "test"

breadcrumbs = sentry_sdk.Hub.current.scope._breadcrumbs
(breadcrumb,) = breadcrumbs
assert breadcrumb["message"] == formatted_message


def test_event_format(sentry_init, capture_events):
sentry_init(
integrations=[
LoguruIntegration(
level=None,
event_level=LoggingLevels.ERROR.value,
event_format="{message}",
)
],
default_integrations=False,
)
events = capture_events()

logger.error("test")
formatted_message = "test"

(event,) = events
assert event["logentry"]["message"] == formatted_message

0 comments on commit 0919a95

Please sign in to comment.