diff --git a/CHANGES-LOG.md b/CHANGES-LOG.md index 5b1ee5e..ef2d258 100644 --- a/CHANGES-LOG.md +++ b/CHANGES-LOG.md @@ -6,6 +6,13 @@ Help this project by [Donation](DONATE.md) Changes log ----------- +### 2.4.7 + +Added `extra_values` argument to `CrashReporter.Formatter` which will let you pass extra static or dynamic values to the +report formatter. +They can be used in the format string. For dynamic values you can pass a function that takes no +arguments as the value. + ### 2.4.6 Shortened the usage syntax for the CrashReporters: diff --git a/README.md b/README.md index 475b81f..95d77f7 100644 --- a/README.md +++ b/README.md @@ -53,33 +53,12 @@ python setup.py install Changes ------- -### 2.4.6 +### 2.4.7 -Shortened the usage syntax for the CrashReporters: - -```python -import log21 - -# Define a ConsoleReporter object -console_reporter = log21.CrashReporter.ConsoleReporter() - - -# This works with other `log21.CrashReporter.Reporter` subclasses as well. - -# Old syntax (still supported) -@console_reporter.reporter -def divide_old(a, b): - return a / b - - -# New Syntax -@console_reporter.reporter -def divide_new(a, b): - return a / b - -``` - -`console_crash_reporter` and `file_crash_reporter` are removed! +Added `extra_values` argument to `CrashReporter.Formatter` which will let you pass extra static or dynamic values to the +report formatter. +They can be used in the format string. For dynamic values you can pass a function that takes no +arguments as the value. [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 b7f5114..01c94aa 100644 --- a/log21/CrashReporter/Formatters.py +++ b/log21/CrashReporter/Formatters.py @@ -4,12 +4,26 @@ import traceback from datetime import datetime as _datetime +from typing import Dict as _Dict, Union as _Union, Callable as _Callable, Any as _Any __all__ = ['Formatter', 'CONSOLE_REPORTER_FORMAT', 'FILE_REPORTER_FORMAT', 'EMAIL_REPORTER_FORMAT'] +RESERVED_KEYS = ( + '__name__', + 'type', + 'message', + 'traceback', + 'name', + 'file', + 'lineno', + 'function', + 'asctime' +) + class Formatter: - def __init__(self, format_: str, style: str = '%', datefmt: str = '%Y-%m-%d %H:%M:%S'): + def __init__(self, format_: str, style: str = '%', datefmt: str = '%Y-%m-%d %H:%M:%S', + extra_values: _Dict[str, _Union[str, _Callable, _Any]] = None): self._format = format_ if style in ['%', '{']: @@ -18,6 +32,12 @@ def __init__(self, format_: str, style: str = '%', datefmt: str = '%Y-%m-%d %H:% raise ValueError('Invalid style: "' + str(style) + '" Valid styles: %, {') self.datefmt = datefmt + self.extra_values = dict() + if extra_values: + for key in extra_values: + if key in RESERVED_KEYS: + raise ValueError(f'`{key}` is a reserved-key and cannot be used in `extra_values`.') + self.extra_values[key] = extra_values[key] def format(self, exception: BaseException): exception_dict = { @@ -31,6 +51,11 @@ def format(self, exception: BaseException): 'function': exception.__traceback__.tb_next.tb_frame.f_code.co_name, 'asctime': _datetime.now().strftime(self.datefmt), } + for key, value in self.extra_values.items(): + if callable(value): + exception_dict[key] = value() + else: + exception_dict[key] = value if self.__style == '%': return self._format % exception_dict diff --git a/log21/__init__.py b/log21/__init__.py index b17eb3d..9ce64b1 100644 --- a/log21/__init__.py +++ b/log21/__init__.py @@ -22,7 +22,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.4.6" +__version__ = "2.4.7" __author__ = "CodeWriter21 (Mehrad Pooryoussof)" __github__ = "Https://GitHub.com/MPCodeWriter21/log21" __all__ = ['ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter', 'DecolorizingFormatter', diff --git a/setup.py b/setup.py index bffaea3..5b4fe72 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.4.6' +VERSION = '2.4.7' setup( name='log21',