Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
55 changes: 23 additions & 32 deletions sentry_sdk/integrations/loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

if TYPE_CHECKING:
from logging import LogRecord
from typing import Optional, Tuple, Any
from typing import Optional, Any

try:
import loguru
Expand Down Expand Up @@ -43,16 +43,16 @@ class LoggingLevels(enum.IntEnum):

DEFAULT_LEVEL = LoggingLevels.INFO.value
DEFAULT_EVENT_LEVEL = LoggingLevels.ERROR.value
# We need to save the handlers to be able to remove them later
# in tests (they call `LoguruIntegration.__init__` multiple times,
# and we can't use `setup_once` because it's called before
# than we get configuration).
_ADDED_HANDLERS = (None, None) # type: Tuple[Optional[int], Optional[int]]


class LoguruIntegration(Integration):
identifier = "loguru"

level = DEFAULT_LEVEL # type: Optional[int]
event_level = DEFAULT_EVENT_LEVEL # type: Optional[int]
breadcrumb_format = DEFAULT_FORMAT
event_format = DEFAULT_FORMAT

def __init__(
self,
level=DEFAULT_LEVEL,
Expand All @@ -61,36 +61,27 @@ def __init__(
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

if breadcrumb_handler is not None:
logger.remove(breadcrumb_handler)
breadcrumb_handler = None
if event_handler is not None:
logger.remove(event_handler)
event_handler = None

if level is not None:
breadcrumb_handler = logger.add(
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,
format=event_format,
)

_ADDED_HANDLERS = (breadcrumb_handler, event_handler)
LoguruIntegration.level = level
LoguruIntegration.event_level = event_level
LoguruIntegration.breadcrumb_format = breadcrumb_format
LoguruIntegration.event_format = event_format

@staticmethod
def setup_once():
# type: () -> None
pass # we do everything in __init__
if LoguruIntegration.level is not None:
logger.add(
LoguruBreadcrumbHandler(level=LoguruIntegration.level),
level=LoguruIntegration.level,
format=LoguruIntegration.breadcrumb_format,
)

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


class _LoguruBaseHandler(_BaseHandler):
Expand Down
17 changes: 14 additions & 3 deletions tests/integrations/loguru/test_loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ def test_just_log(
expected_sentry_level,
disable_breadcrumbs,
disable_events,
uninstall_integration,
request,
):
uninstall_integration("loguru")
request.addfinalizer(logger.remove)

sentry_init(
integrations=[
LoguruIntegration(
Expand All @@ -49,7 +54,7 @@ def test_just_log(
formatted_message = (
" | "
+ "{:9}".format(level.name.upper())
+ "| tests.integrations.loguru.test_loguru:test_just_log:47 - test"
+ "| tests.integrations.loguru.test_loguru:test_just_log:52 - test"
)

if not created_event:
Expand Down Expand Up @@ -78,7 +83,10 @@ def test_just_log(
assert event["logentry"]["message"][23:] == formatted_message


def test_breadcrumb_format(sentry_init, capture_events):
def test_breadcrumb_format(sentry_init, capture_events, uninstall_integration, request):
uninstall_integration("loguru")
request.addfinalizer(logger.remove)

sentry_init(
integrations=[
LoguruIntegration(
Expand All @@ -98,7 +106,10 @@ def test_breadcrumb_format(sentry_init, capture_events):
assert breadcrumb["message"] == formatted_message


def test_event_format(sentry_init, capture_events):
def test_event_format(sentry_init, capture_events, uninstall_integration, request):
uninstall_integration("loguru")
request.addfinalizer(logger.remove)

sentry_init(
integrations=[
LoguruIntegration(
Expand Down
Loading