Skip to content

Commit ee99e36

Browse files
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.
1 parent 602f714 commit ee99e36

5 files changed

Lines changed: 40 additions & 29 deletions

File tree

CHANGES-LOG.md

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

9+
### 2.4.7
10+
11+
Added `extra_values` argument to `CrashReporter.Formatter` which will let you pass extra static or dynamic values to the
12+
report formatter.
13+
They can be used in the format string. For dynamic values you can pass a function that takes no
14+
arguments as the value.
15+
916
### 2.4.6
1017

1118
Shortened the usage syntax for the CrashReporters:

README.md

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

56-
### 2.4.6
56+
### 2.4.7
5757

58-
Shortened the usage syntax for the CrashReporters:
59-
60-
```python
61-
import log21
62-
63-
# Define a ConsoleReporter object
64-
console_reporter = log21.CrashReporter.ConsoleReporter()
65-
66-
67-
# This works with other `log21.CrashReporter.Reporter` subclasses as well.
68-
69-
# Old syntax (still supported)
70-
@console_reporter.reporter
71-
def divide_old(a, b):
72-
return a / b
73-
74-
75-
# New Syntax
76-
@console_reporter.reporter
77-
def divide_new(a, b):
78-
return a / b
79-
80-
```
81-
82-
`console_crash_reporter` and `file_crash_reporter` are removed!
58+
Added `extra_values` argument to `CrashReporter.Formatter` which will let you pass extra static or dynamic values to the
59+
report formatter.
60+
They can be used in the format string. For dynamic values you can pass a function that takes no
61+
arguments as the value.
8362

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

log21/CrashReporter/Formatters.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@
44
import traceback
55

66
from datetime import datetime as _datetime
7+
from typing import Dict as _Dict, Union as _Union, Callable as _Callable, Any as _Any
78

89
__all__ = ['Formatter', 'CONSOLE_REPORTER_FORMAT', 'FILE_REPORTER_FORMAT', 'EMAIL_REPORTER_FORMAT']
910

11+
RESERVED_KEYS = (
12+
'__name__',
13+
'type',
14+
'message',
15+
'traceback',
16+
'name',
17+
'file',
18+
'lineno',
19+
'function',
20+
'asctime'
21+
)
22+
1023

1124
class Formatter:
12-
def __init__(self, format_: str, style: str = '%', datefmt: str = '%Y-%m-%d %H:%M:%S'):
25+
def __init__(self, format_: str, style: str = '%', datefmt: str = '%Y-%m-%d %H:%M:%S',
26+
extra_values: _Dict[str, _Union[str, _Callable, _Any]] = None):
1327
self._format = format_
1428

1529
if style in ['%', '{']:
@@ -18,6 +32,12 @@ def __init__(self, format_: str, style: str = '%', datefmt: str = '%Y-%m-%d %H:%
1832
raise ValueError('Invalid style: "' + str(style) + '" Valid styles: %, {')
1933

2034
self.datefmt = datefmt
35+
self.extra_values = dict()
36+
if extra_values:
37+
for key in extra_values:
38+
if key in RESERVED_KEYS:
39+
raise ValueError(f'`{key}` is a reserved-key and cannot be used in `extra_values`.')
40+
self.extra_values[key] = extra_values[key]
2141

2242
def format(self, exception: BaseException):
2343
exception_dict = {
@@ -31,6 +51,11 @@ def format(self, exception: BaseException):
3151
'function': exception.__traceback__.tb_next.tb_frame.f_code.co_name,
3252
'asctime': _datetime.now().strftime(self.datefmt),
3353
}
54+
for key, value in self.extra_values.items():
55+
if callable(value):
56+
exception_dict[key] = value()
57+
else:
58+
exception_dict[key] = value
3459

3560
if self.__style == '%':
3661
return self._format % exception_dict

log21/__init__.py

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

25-
__version__ = "2.4.6"
25+
__version__ = "2.4.7"
2626
__author__ = "CodeWriter21 (Mehrad Pooryoussof)"
2727
__github__ = "Https://GitHub.com/MPCodeWriter21/log21"
2828
__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.4.6'
10+
VERSION = '2.4.7'
1111

1212
setup(
1313
name='log21',

0 commit comments

Comments
 (0)