Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 44bbe40

Browse files
author
Sam Park
committed
Add Python 2.7 support
1 parent caaf391 commit 44bbe40

File tree

6 files changed

+26
-18
lines changed

6 files changed

+26
-18
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ repos:
99
rev: 3.7.7
1010
hooks:
1111
- id: flake8
12+
# Ignore missing 'typing' lib for Python 2.7
13+
args:
14+
- "--ignore=F821"
1215
language_version: python3.7
1316

1417
- repo: https://github.com/asottile/seed-isort-config

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ install: pip install -U pip tox
77
matrix:
88
fast_finish: true
99
include:
10+
- name: Python 2.7 tests
11+
python: '2.7'
12+
script: tox -e py27
13+
1014
- name: Python 3.5 tests
1115
python: '3.5'
1216
script: tox -e py35

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"Intended Audience :: Developers",
3030
"License :: OSI Approved :: MIT License",
3131
"Programming Language :: Python",
32+
"Programming Language :: Python :: 2.7",
3233
"Programming Language :: Python :: 3",
3334
"Programming Language :: Python :: 3.4",
3435
"Programming Language :: Python :: 3.5",

structlog_sentry/__init__.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
import logging
22
import sys
3-
from typing import List, Optional, Tuple, Union
43

54
from sentry_sdk import capture_event
65
from sentry_sdk.integrations.logging import ignore_logger
76
from sentry_sdk.utils import event_from_exception
87

98

10-
class SentryProcessor:
9+
class SentryProcessor(object):
1110
"""Sentry processor for structlog.
1211
1312
Uses Sentry SDK to capture events in Sentry.
1413
"""
1514

1615
def __init__(
17-
self,
18-
level: int = logging.WARNING,
19-
active: bool = True,
20-
as_extra: bool = True,
21-
tag_keys: Union[List[str], str] = None,
22-
) -> None:
16+
self, level=logging.WARNING, active=True, as_extra=True, tag_keys=None
17+
):
18+
# type: (int, bool, bool, typing.Union[typing.List[str], str]) -> None
2319
"""
2420
:param level: events of this or higher levels will be reported to Sentry.
2521
:param active: a flag to make this processor enabled/disabled.
@@ -34,7 +30,8 @@ def __init__(
3430
self._as_extra = as_extra
3531
self._original_event_dict = None
3632

37-
def _get_event_and_hint(self, event_dict: dict) -> Tuple[dict, Optional[str]]:
33+
def _get_event_and_hint(self, event_dict):
34+
# type: (dict) -> typing.Tuple[dict, typing.Optional[str]]
3835
"""Create a sentry event and hint from structlog `event_dict` and sys.exc_info.
3936
4037
:param event_dict: structlog event_dict
@@ -61,15 +58,17 @@ def _get_event_and_hint(self, event_dict: dict) -> Tuple[dict, Optional[str]]:
6158

6259
return event, hint
6360

64-
def _log(self, event_dict: dict) -> str:
61+
def _log(self, event_dict):
62+
# type: (dict) -> str
6563
"""Send an event to Sentry and return sentry event id.
6664
6765
:param event_dict: structlog event_dict
6866
"""
6967
event, hint = self._get_event_and_hint(event_dict)
7068
return capture_event(event, hint=hint)
7169

72-
def __call__(self, logger, method, event_dict) -> dict:
70+
def __call__(self, logger, method, event_dict):
71+
# type: (typing.Any, str, dict) -> dict
7372
"""A middleware to process structlog `event_dict` and send it to Sentry."""
7473
self._original_event_dict = event_dict.copy()
7574
sentry_skip = event_dict.pop("sentry_skip", False)
@@ -92,16 +91,18 @@ class SentryJsonProcessor(SentryProcessor):
9291
Uses Sentry SDK to capture events in Sentry.
9392
"""
9493

95-
def __init__(self, *args, **kwargs) -> None:
96-
super().__init__(*args, **kwargs)
94+
def __init__(self, *args, **kwargs):
95+
super(SentryJsonProcessor, self).__init__(*args, **kwargs)
9796
self._is_logger_ignored = False
9897

99-
def __call__(self, logger, method, event_dict) -> dict:
98+
def __call__(self, logger, method, event_dict):
99+
# type: (typing.Any, str, dict) -> dict
100100
if not self._is_logger_ignored:
101101
self._ignore_logger(logger, event_dict)
102-
return super().__call__(logger, method, event_dict)
102+
return super(SentryJsonProcessor, self).__call__(logger, method, event_dict)
103103

104-
def _ignore_logger(self, logger, event_dict: dict) -> None:
104+
def _ignore_logger(self, logger, event_dict):
105+
# type: (typing.Any, dict) -> None
105106
"""Tell Sentry to ignore logger.
106107
107108
This is temporary workaround to prevent duplication of a JSON event in Sentry.

test-requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
atomicwrites==1.3.0 # via pytest
88
attrs==19.1.0 # via pytest
99
coverage==4.5.3 # via pytest-cov
10-
more-itertools==7.0.0 # via pytest
1110
pluggy==0.9.0 # via pytest
1211
py==1.8.0 # via pytest
1312
pytest-cov==2.6.1

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tox]
22
envlist =
33
lint
4-
py{35,36,37}
4+
py{27,35,36,37}
55

66
[testenv]
77
deps =

0 commit comments

Comments
 (0)