Skip to content

Commit 672ecdb

Browse files
Added formatter argument to StreamHandler and FileHandler. You can use it to set the formatter of the handler when
you create it. Added `handlers` argument to `Logger`. You can use it to add handlers to the logger when you create it.
1 parent b39b19e commit 672ecdb

8 files changed

Lines changed: 42 additions & 31 deletions

File tree

CHANGES-LOG.md

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

9+
### 2.3.1
10+
11+
Added `formatter` argument to `StreamHandler` and `FileHandler`. You can use it to set the formatter of the handler when
12+
you create it. Added `handlers` argument to `Logger`. You can use it to add handlers to the logger when you create it.
13+
914
### 2.3.0
1015

1116
Added progressbar custom formatting.

README.md

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,10 @@ python setup.py install
5353
Changes
5454
-------
5555

56-
### 2.3.0
56+
### 2.3.1
5757

58-
Added progressbar custom formatting.
59-
60-
Now you can use your own formatting for the progressbar instead of the default one.
61-
62-
Let's see an example:
63-
64-
```python
65-
# We import the ProgressBar class from log21
66-
from log21 import ProgressBar
67-
# psutil is a module that can be used to get the current memory usage or cpu usage of your system
68-
# If you want to try this example, you need to install psutil: pip install psutil
69-
import psutil
70-
# We use the time module to make a delay between the progressbar updates
71-
import time
72-
73-
cpu_bar = ProgressBar(format_='CPU Usage: {prefix}{bar}{suffix} {percentage}%', style='{', new_line_when_complete=False)
74-
75-
while True:
76-
cpu_bar.update(psutil.cpu_percent(), 100)
77-
time.sleep(0.1)
78-
```
58+
Added `formatter` argument to `StreamHandler` and `FileHandler`. You can use it to set the formatter of the handler when
59+
you create it. Added `handlers` argument to `Logger`. You can use it to add handlers to the logger when you create it.
7960

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

log21/CrashReporter/Reporters.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,11 @@ class EmailReporter(Reporter):
234234
>>> divide(10, 0)
235235
Traceback (most recent call last):
236236
File "<stdin>", line 1, in <module>
237-
File "%localappdata%\Programs\Python\Python310\lib\site-packages\log21\CrashReporter\Reporters.py", line 81, in wrap
237+
File "%localappdata%\\Programs\\Python\\Python310\\lib\\site-packages\\log21\\CrashReporter\\Reporters.py",
238+
line 81, in wrap
238239
raise e
239-
File "%localappdata%\Programs\Python\Python310\lib\site-packages\log21\CrashReporter\Reporters.py", line 77, in wrap
240+
File "%localappdata%\\Programs\\Python\\Python310\\lib\\site-packages\\log21\\CrashReporter\\Reporters.py",
241+
line 77, in wrap
240242
return func(*args, **kwargs)
241243
File "<stdin>", line 3, in divide
242244
ZeroDivisionError: division by zero

log21/FileHandler.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
from log21.Formatters import DecolorizingFormatter as _DecolorizingFormatter
66

77

8-
class DecolorizingFileHandler(_FileHandler):
8+
class FileHandler(_FileHandler):
9+
def __init__(self, filename, mode='a', encoding=None, delay=False, errors=None, formatter=None, level=None):
10+
super().__init__(filename, mode, encoding, delay, errors)
11+
if formatter is not None:
12+
self.setFormatter(formatter)
13+
if level is not None:
14+
self.setLevel(level)
15+
16+
17+
class DecolorizingFileHandler(FileHandler):
918
terminator = ''
1019

1120
def emit(self, record):

log21/Logger.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import logging as _logging
55

6-
from getpass import getpass
6+
from getpass import getpass as _getpass
7+
from typing import Sequence as _Sequence, Union as _Union
78
from logging import raiseExceptions as _raiseExceptions
89

910
import log21 as _log21
@@ -13,10 +14,18 @@
1314

1415

1516
class Logger(_logging.Logger):
16-
def __init__(self, name, level=NOTSET):
17+
def __init__(self, name, level=NOTSET, handlers: _Union[_Sequence[_logging.Handler], _logging.Handler] = None):
1718
super().__init__(name, level)
1819
self.setLevel(level)
1920
self._progress_bar = None
21+
if handlers:
22+
if not isinstance(handlers, _Sequence):
23+
if isinstance(handlers, _logging.Handler):
24+
handlers = [handlers]
25+
else:
26+
raise TypeError("handlers must be a list of logging.Handler objects")
27+
for handler in handlers:
28+
self.addHandler(handler)
2029

2130
def isEnabledFor(self, level):
2231
"""
@@ -164,7 +173,7 @@ def getpass(self, *msg, args: tuple = (), end='', **kwargs):
164173
"""
165174
msg = ' '.join([str(m) for m in msg]) + end
166175
self._log(self.level if self.level >= NOTSET else NOTSET, msg, args, **kwargs)
167-
return getpass('')
176+
return _getpass('')
168177

169178
def print_progress(self, progress: float, total: float):
170179
"""

log21/StreamHandler.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818
class StreamHandler(_StreamHandler):
1919
terminator = ''
2020

21-
def __init__(self, handle_carriage_return: bool = True, handle_new_line: bool = True, stream=None):
21+
def __init__(self, handle_carriage_return: bool = True, handle_new_line: bool = True, stream=None, formatter=None,
22+
level=None):
2223
self.HandleCR = handle_carriage_return
2324
self.HandleNL = handle_new_line
2425
super().__init__(stream=stream)
26+
if formatter is not None:
27+
self.setFormatter(formatter)
28+
if level is not None:
29+
self.setLevel(level)
2530

2631
def check_cr(self, record):
2732
if record.msg:

log21/__init__.py

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

24-
__version__ = "2.3.0"
24+
__version__ = "2.3.1"
2525
__author__ = "CodeWriter21 (Mehrad Pooryoussof)"
2626
__github__ = "Https://GitHub.com/MPCodeWriter21/log21"
2727
__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.3.0'
10+
VERSION = '2.3.1'
1111

1212
setup(
1313
name='log21',

0 commit comments

Comments
 (0)