Skip to content

Commit

Permalink
Added extra_values argument to CrashReporter.Formatter which will…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
MPCodeWriter21 committed May 9, 2023
1 parent 602f714 commit ee99e36
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
7 changes: 7 additions & 0 deletions CHANGES-LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 5 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
27 changes: 26 additions & 1 deletion log21/CrashReporter/Formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ['%', '{']:
Expand All @@ -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 = {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion log21/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
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.4.6'
VERSION = '2.4.7'

setup(
name='log21',
Expand Down

0 comments on commit ee99e36

Please sign in to comment.