Skip to content

Commit

Permalink
Added catch and ignore methods to log21.CrashReporter.Reporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
MPCodeWriter21 committed Aug 16, 2022
1 parent 49adde2 commit 3f53d58
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGES-LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Help this project by [Donation](DONATE.md)
Changes log
-----------

### 2.3.8

Added `catch` and `ignore` methods to `log21.CrashReporter.Reporter`.

### 2.3.7

Added `exceptions_to_catch` and `exceptions_to_ignore` arguments to `log21.CrashReporter.Reporter` class.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ python setup.py install
Changes
-------

### 2.3.7
### 2.3.8

Added `exceptions_to_catch` and `exceptions_to_ignore` arguments to `log21.CrashReporter.Reporter` class.
Added `catch` and `ignore` methods to `log21.CrashReporter.Reporter`.

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

Expand Down
2 changes: 1 addition & 1 deletion log21/CrashReporter/Formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, format_: str, style: str = '%', datefmt: str = '%Y-%m-%d %H:%

self.datefmt = datefmt

def format(self, exception: Exception):
def format(self, exception: BaseException):
exception_dict = {
'__name__': __name__,
'type': type(exception),
Expand Down
46 changes: 36 additions & 10 deletions log21/CrashReporter/Reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class Reporter:
raise_after_report: bool

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

return wrap

def catch(self, exception: BaseException):
"""
Add an exception to the list of exceptions to catch.
:param exception: Exception to catch.
"""
if self._exceptions_to_catch is None:
self._exceptions_to_catch = set()
self._exceptions_to_catch.add(exception)

def ignore(self, exception: BaseException):
"""
Add an exception to the list of exceptions to ignore.
:param exception: Exception to ignore.
"""
if self._exceptions_to_ignore is None:
self._exceptions_to_ignore = set()
self._exceptions_to_ignore.add(exception)


class ConsoleReporter(Reporter):
"""
Expand Down Expand Up @@ -186,12 +207,13 @@ class ConsoleReporter(Reporter):
"""

def __init__(self, raise_after_report: bool = False, formatter: '_log21.CrashReporter.Formatter' = None,
print_function: _Callable = print):
print_function: _Callable = print, exceptions_to_catch: _Iterable[BaseException] = None,
exceptions_to_ignore: _Iterable[BaseException] = None):
"""
:param raise_after_report: If True, the exception will be raised after the report_function is called.
:param print_function: Function to use to print the message.
"""
super().__init__(self._report, raise_after_report)
super().__init__(self._report, raise_after_report, formatter, exceptions_to_catch, exceptions_to_ignore)

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

self.print = print_function

def _report(self, exception: Exception):
def _report(self, exception: BaseException):
"""
Prints the exception to the console.
Expand All @@ -220,8 +242,10 @@ class FileReporter(Reporter):
"""

def __init__(self, file: _Union[str, _PathLike, _IO], raise_after_report: bool = True,
formatter: '_log21.CrashReporter.Formatter' = None):
super().__init__(self._report, raise_after_report)
formatter: '_log21.CrashReporter.Formatter' = None,
exceptions_to_catch: _Iterable[BaseException] = None,
exceptions_to_ignore: _Iterable[BaseException] = None):
super().__init__(self._report, raise_after_report, formatter, exceptions_to_catch, exceptions_to_ignore)
if isinstance(file, str):
self.file = open(file, 'a')
elif isinstance(file, _PathLike):
Expand All @@ -242,7 +266,7 @@ def __init__(self, file: _Union[str, _PathLike, _IO], raise_after_report: bool =
else:
self.formatter = _log21.CrashReporter.Formatters.Formatter(**_FILE_REPORTER_FORMAT)

def _report(self, exception: Exception):
def _report(self, exception: BaseException):
"""
Writes the exception to the file.
Expand Down Expand Up @@ -292,8 +316,10 @@ class EmailReporter(Reporter):
"""

def __init__(self, mail_host: str, port: int, from_address: str, to_address: str, password: str, username: str = '',
tls: bool = True, raise_after_report: bool = True, formatter: '_log21.CrashReporter.Formatter' = None):
super().__init__(self._report, raise_after_report)
tls: bool = True, raise_after_report: bool = True, formatter: '_log21.CrashReporter.Formatter' = None,
exceptions_to_catch: _Iterable[BaseException] = None,
exceptions_to_ignore: _Iterable[BaseException] = None):
super().__init__(self._report, raise_after_report, formatter, exceptions_to_catch, exceptions_to_ignore)
self.mail_host = mail_host
self.port = port
self.from_address = from_address
Expand Down Expand Up @@ -328,7 +354,7 @@ def __init__(self, mail_host: str, port: int, from_address: str, to_address: str
else:
self.formatter = _log21.CrashReporter.Formatters.Formatter(**_EMAIL_REPORTER_FORMAT)

def _report(self, exception: Exception):
def _report(self, exception: BaseException):
"""
Sends an email with the exception.
Expand Down
2 changes: 1 addition & 1 deletion log21/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from log21.Formatters import ColorizingFormatter, DecolorizingFormatter
from log21.Colors import Colors, get_color, get_colors, ansi_escape, get_color_name, closest_color

__version__ = "2.3.7"
__version__ = "2.3.8"
__author__ = "CodeWriter21 (Mehrad Pooryoussof)"
__github__ = "Https://GitHub.com/MPCodeWriter21/log21"
__all__ = ['ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter', 'DecolorizingFormatter',
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
long_description = file.read()

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

setup(
name='log21',
Expand Down

0 comments on commit 3f53d58

Please sign in to comment.