From 3f53d58e91a7eb0b3dc4976aa60952eaf13db52d Mon Sep 17 00:00:00 2001 From: CodeWriter21 Date: Tue, 16 Aug 2022 16:52:10 +0430 Subject: [PATCH] Added `catch` and `ignore` methods to `log21.CrashReporter.Reporter`. --- CHANGES-LOG.md | 4 +++ README.md | 4 +-- log21/CrashReporter/Formatters.py | 2 +- log21/CrashReporter/Reporters.py | 46 ++++++++++++++++++++++++------- log21/__init__.py | 2 +- setup.py | 2 +- 6 files changed, 45 insertions(+), 15 deletions(-) diff --git a/CHANGES-LOG.md b/CHANGES-LOG.md index 5782714..83b0a8a 100644 --- a/CHANGES-LOG.md +++ b/CHANGES-LOG.md @@ -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. diff --git a/README.md b/README.md index d2dd359..9f221eb 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/log21/CrashReporter/Formatters.py b/log21/CrashReporter/Formatters.py index 0e2e5b2..b7f5114 100644 --- a/log21/CrashReporter/Formatters.py +++ b/log21/CrashReporter/Formatters.py @@ -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), diff --git a/log21/CrashReporter/Reporters.py b/log21/CrashReporter/Reporters.py index 6d83e4a..771d933 100644 --- a/log21/CrashReporter/Reporters.py +++ b/log21/CrashReporter/Reporters.py @@ -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. @@ -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): """ @@ -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): @@ -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. @@ -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): @@ -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. @@ -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 @@ -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. diff --git a/log21/__init__.py b/log21/__init__.py index 463188a..86f2790 100644 --- a/log21/__init__.py +++ b/log21/__init__.py @@ -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', diff --git a/setup.py b/setup.py index 66115ff..ee539eb 100644 --- a/setup.py +++ b/setup.py @@ -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',