Skip to content

Commit 3f53d58

Browse files
Added catch and ignore methods to log21.CrashReporter.Reporter.
1 parent 49adde2 commit 3f53d58

6 files changed

Lines changed: 45 additions & 15 deletions

File tree

CHANGES-LOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Help this project by [Donation](DONATE.md)
66
Changes log
77
-----------
88

9+
### 2.3.8
10+
11+
Added `catch` and `ignore` methods to `log21.CrashReporter.Reporter`.
12+
913
### 2.3.7
1014

1115
Added `exceptions_to_catch` and `exceptions_to_ignore` arguments to `log21.CrashReporter.Reporter` class.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ python setup.py install
5353
Changes
5454
-------
5555

56-
### 2.3.7
56+
### 2.3.8
5757

58-
Added `exceptions_to_catch` and `exceptions_to_ignore` arguments to `log21.CrashReporter.Reporter` class.
58+
Added `catch` and `ignore` methods to `log21.CrashReporter.Reporter`.
5959

6060
[Full Changes Log](https://github.com/MPCodeWriter21/log21/blob/master/CHANGES-LOG.md)
6161

log21/CrashReporter/Formatters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self, format_: str, style: str = '%', datefmt: str = '%Y-%m-%d %H:%
1919

2020
self.datefmt = datefmt
2121

22-
def format(self, exception: Exception):
22+
def format(self, exception: BaseException):
2323
exception_dict = {
2424
'__name__': __name__,
2525
'type': type(exception),

log21/CrashReporter/Reporters.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class Reporter:
6666
raise_after_report: bool
6767

6868
def __init__(self, report_function: _Callable[[BaseException], _Any], raise_after_report: bool = False,
69-
formatter: '_log21.CrashReporter.Formatter' = None, exceptions_to_catch: _Iterable[BaseException] = None,
69+
formatter: '_log21.CrashReporter.Formatter' = None,
70+
exceptions_to_catch: _Iterable[BaseException] = None,
7071
exceptions_to_ignore: _Iterable[BaseException] = None):
7172
"""
7273
:param report_function: Function to call when an exception is raised.
@@ -137,6 +138,26 @@ def wrap(*args, **kwargs):
137138

138139
return wrap
139140

141+
def catch(self, exception: BaseException):
142+
"""
143+
Add an exception to the list of exceptions to catch.
144+
145+
:param exception: Exception to catch.
146+
"""
147+
if self._exceptions_to_catch is None:
148+
self._exceptions_to_catch = set()
149+
self._exceptions_to_catch.add(exception)
150+
151+
def ignore(self, exception: BaseException):
152+
"""
153+
Add an exception to the list of exceptions to ignore.
154+
155+
:param exception: Exception to ignore.
156+
"""
157+
if self._exceptions_to_ignore is None:
158+
self._exceptions_to_ignore = set()
159+
self._exceptions_to_ignore.add(exception)
160+
140161

141162
class ConsoleReporter(Reporter):
142163
"""
@@ -186,12 +207,13 @@ class ConsoleReporter(Reporter):
186207
"""
187208

188209
def __init__(self, raise_after_report: bool = False, formatter: '_log21.CrashReporter.Formatter' = None,
189-
print_function: _Callable = print):
210+
print_function: _Callable = print, exceptions_to_catch: _Iterable[BaseException] = None,
211+
exceptions_to_ignore: _Iterable[BaseException] = None):
190212
"""
191213
:param raise_after_report: If True, the exception will be raised after the report_function is called.
192214
:param print_function: Function to use to print the message.
193215
"""
194-
super().__init__(self._report, raise_after_report)
216+
super().__init__(self._report, raise_after_report, formatter, exceptions_to_catch, exceptions_to_ignore)
195217

196218
if formatter:
197219
if isinstance(formatter, _log21.CrashReporter.Formatter):
@@ -203,7 +225,7 @@ def __init__(self, raise_after_report: bool = False, formatter: '_log21.CrashRep
203225

204226
self.print = print_function
205227

206-
def _report(self, exception: Exception):
228+
def _report(self, exception: BaseException):
207229
"""
208230
Prints the exception to the console.
209231
@@ -220,8 +242,10 @@ class FileReporter(Reporter):
220242
"""
221243

222244
def __init__(self, file: _Union[str, _PathLike, _IO], raise_after_report: bool = True,
223-
formatter: '_log21.CrashReporter.Formatter' = None):
224-
super().__init__(self._report, raise_after_report)
245+
formatter: '_log21.CrashReporter.Formatter' = None,
246+
exceptions_to_catch: _Iterable[BaseException] = None,
247+
exceptions_to_ignore: _Iterable[BaseException] = None):
248+
super().__init__(self._report, raise_after_report, formatter, exceptions_to_catch, exceptions_to_ignore)
225249
if isinstance(file, str):
226250
self.file = open(file, 'a')
227251
elif isinstance(file, _PathLike):
@@ -242,7 +266,7 @@ def __init__(self, file: _Union[str, _PathLike, _IO], raise_after_report: bool =
242266
else:
243267
self.formatter = _log21.CrashReporter.Formatters.Formatter(**_FILE_REPORTER_FORMAT)
244268

245-
def _report(self, exception: Exception):
269+
def _report(self, exception: BaseException):
246270
"""
247271
Writes the exception to the file.
248272
@@ -292,8 +316,10 @@ class EmailReporter(Reporter):
292316
"""
293317

294318
def __init__(self, mail_host: str, port: int, from_address: str, to_address: str, password: str, username: str = '',
295-
tls: bool = True, raise_after_report: bool = True, formatter: '_log21.CrashReporter.Formatter' = None):
296-
super().__init__(self._report, raise_after_report)
319+
tls: bool = True, raise_after_report: bool = True, formatter: '_log21.CrashReporter.Formatter' = None,
320+
exceptions_to_catch: _Iterable[BaseException] = None,
321+
exceptions_to_ignore: _Iterable[BaseException] = None):
322+
super().__init__(self._report, raise_after_report, formatter, exceptions_to_catch, exceptions_to_ignore)
297323
self.mail_host = mail_host
298324
self.port = port
299325
self.from_address = from_address
@@ -328,7 +354,7 @@ def __init__(self, mail_host: str, port: int, from_address: str, to_address: str
328354
else:
329355
self.formatter = _log21.CrashReporter.Formatters.Formatter(**_EMAIL_REPORTER_FORMAT)
330356

331-
def _report(self, exception: Exception):
357+
def _report(self, exception: BaseException):
332358
"""
333359
Sends an email with the exception.
334360

log21/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from log21.Formatters import ColorizingFormatter, DecolorizingFormatter
2222
from log21.Colors import Colors, get_color, get_colors, ansi_escape, get_color_name, closest_color
2323

24-
__version__ = "2.3.7"
24+
__version__ = "2.3.8"
2525
__author__ = "CodeWriter21 (Mehrad Pooryoussof)"
2626
__github__ = "Https://GitHub.com/MPCodeWriter21/log21"
2727
__all__ = ['ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter', 'DecolorizingFormatter',

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
long_description = file.read()
88

99
DESCRIPTION = 'A simple logging package that helps you log colorized messages in Windows console.'
10-
VERSION = '2.3.7'
10+
VERSION = '2.3.8'
1111

1212
setup(
1313
name='log21',

0 commit comments

Comments
 (0)