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(loguru): add message format configuration arguments #2208

Merged
merged 9 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 14 additions & 3 deletions sentry_sdk/integrations/loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

Expand All @@ -42,7 +43,13 @@ class LoggingLevels(enum.IntEnum):
class LoguruIntegration(Integration):
identifier = "loguru"

def __init__(self, level=DEFAULT_LEVEL, event_level=DEFAULT_EVENT_LEVEL):
def __init__(
self,
level=DEFAULT_LEVEL,
event_level=DEFAULT_EVENT_LEVEL,
breadcrumb_format=DEFAULT_FORMAT,
event_format=DEFAULT_FORMAT,
):
# type: (Optional[int], Optional[int]) -> None
sentrivana marked this conversation as resolved.
Show resolved Hide resolved
global _ADDED_HANDLERS
breadcrumb_handler, event_handler = _ADDED_HANDLERS
Expand All @@ -56,12 +63,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