-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `loguru` integration Actually, this is the solution in comments under #653 adapted to codebase and tested as well. #653 (comment) I also changed `logging` integration to use methods instead of functions in handlers, as in that way we can easily overwrite parts that are different in `loguru` integration. It shouldn't be a problem, as those methods are private and used only in that file. --------- Co-authored-by: Anton Pirker <anton.pirker@sentry.io>
- Loading branch information
1 parent
4b6a381
commit 8a2b74f
Showing
8 changed files
with
326 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
name: Test loguru | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- release/** | ||
|
||
pull_request: | ||
|
||
# Cancel in progress workflows on pull_requests. | ||
# https://docs.github.com/en/actions/using-jobs/using-concurrency#example-using-a-fallback-value | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
cancel-in-progress: true | ||
|
||
permissions: | ||
contents: read | ||
|
||
env: | ||
BUILD_CACHE_KEY: ${{ github.sha }} | ||
CACHED_BUILD_PATHS: | | ||
${{ github.workspace }}/dist-serverless | ||
jobs: | ||
test: | ||
name: loguru, python ${{ matrix.python-version }}, ${{ matrix.os }} | ||
runs-on: ${{ matrix.os }} | ||
timeout-minutes: 45 | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
python-version: ["3.5","3.6","3.7","3.8","3.9","3.10","3.11"] | ||
# python3.6 reached EOL and is no longer being supported on | ||
# new versions of hosted runners on Github Actions | ||
# ubuntu-20.04 is the last version that supported python3.6 | ||
# see https://github.com/actions/setup-python/issues/544#issuecomment-1332535877 | ||
os: [ubuntu-20.04] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Setup Test Env | ||
run: | | ||
pip install coverage "tox>=3,<4" | ||
- name: Test loguru | ||
timeout-minutes: 45 | ||
shell: bash | ||
run: | | ||
set -x # print commands that are executed | ||
coverage erase | ||
# Run tests | ||
./scripts/runtox.sh "py${{ matrix.python-version }}-loguru" --cov=tests --cov=sentry_sdk --cov-report= --cov-branch | ||
coverage combine .coverage* | ||
coverage xml -i | ||
- uses: codecov/codecov-action@v3 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
files: coverage.xml | ||
|
||
check_required_tests: | ||
name: All loguru tests passed or skipped | ||
needs: test | ||
# Always run this, even if a dependent job failed | ||
if: always() | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: Check for failures | ||
if: contains(needs.test.result, 'failure') | ||
run: | | ||
echo "One of the dependent jobs have failed. You may need to re-run it." && exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from __future__ import absolute_import | ||
|
||
import enum | ||
|
||
from sentry_sdk._types import TYPE_CHECKING | ||
from sentry_sdk.integrations import Integration, DidNotEnable | ||
from sentry_sdk.integrations.logging import ( | ||
BreadcrumbHandler, | ||
EventHandler, | ||
_BaseHandler, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from logging import LogRecord | ||
from typing import Optional, Tuple | ||
|
||
try: | ||
from loguru import logger | ||
except ImportError: | ||
raise DidNotEnable("LOGURU is not installed") | ||
|
||
|
||
class LoggingLevels(enum.IntEnum): | ||
TRACE = 5 | ||
DEBUG = 10 | ||
INFO = 20 | ||
SUCCESS = 25 | ||
WARNING = 30 | ||
ERROR = 40 | ||
CRITICAL = 50 | ||
|
||
|
||
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" | ||
|
||
def __init__(self, level=DEFAULT_LEVEL, event_level=DEFAULT_EVENT_LEVEL): | ||
# type: (Optional[int], Optional[int]) -> 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 | ||
) | ||
|
||
if event_level is not None: | ||
event_handler = logger.add( | ||
LoguruEventHandler(level=event_level), level=event_level | ||
) | ||
|
||
_ADDED_HANDLERS = (breadcrumb_handler, event_handler) | ||
|
||
@staticmethod | ||
def setup_once(): | ||
# type: () -> None | ||
pass # we do everything in __init__ | ||
|
||
|
||
class _LoguruBaseHandler(_BaseHandler): | ||
def _logging_to_event_level(self, record): | ||
# type: (LogRecord) -> str | ||
try: | ||
return LoggingLevels(record.levelno).name.lower() | ||
except ValueError: | ||
return record.levelname.lower() if record.levelname else "" | ||
|
||
|
||
class LoguruEventHandler(_LoguruBaseHandler, EventHandler): | ||
"""Modified version of :class:`sentry_sdk.integrations.logging.EventHandler` to use loguru's level names.""" | ||
|
||
|
||
class LoguruBreadcrumbHandler(_LoguruBaseHandler, BreadcrumbHandler): | ||
"""Modified version of :class:`sentry_sdk.integrations.logging.BreadcrumbHandler` to use loguru's level names.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import pytest | ||
|
||
pytest.importorskip("loguru") |
Oops, something went wrong.