Skip to content

Commit

Permalink
Added formatter argument to StreamHandler and FileHandler. You …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
MPCodeWriter21 committed Jul 1, 2022
1 parent b39b19e commit 672ecdb
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGES-LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Help this project by [Donation](DONATE.md)
Changes log
-----------

### 2.3.1

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.

### 2.3.0

Added progressbar custom formatting.
Expand Down
25 changes: 3 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,10 @@ python setup.py install
Changes
-------

### 2.3.0
### 2.3.1

Added progressbar custom formatting.

Now you can use your own formatting for the progressbar instead of the default one.

Let's see an example:

```python
# We import the ProgressBar class from log21
from log21 import ProgressBar
# psutil is a module that can be used to get the current memory usage or cpu usage of your system
# If you want to try this example, you need to install psutil: pip install psutil
import psutil
# We use the time module to make a delay between the progressbar updates
import time

cpu_bar = ProgressBar(format_='CPU Usage: {prefix}{bar}{suffix} {percentage}%', style='{', new_line_when_complete=False)

while True:
cpu_bar.update(psutil.cpu_percent(), 100)
time.sleep(0.1)
```
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.

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

Expand Down
6 changes: 4 additions & 2 deletions log21/CrashReporter/Reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,11 @@ class EmailReporter(Reporter):
>>> divide(10, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "%localappdata%\Programs\Python\Python310\lib\site-packages\log21\CrashReporter\Reporters.py", line 81, in wrap
File "%localappdata%\\Programs\\Python\\Python310\\lib\\site-packages\\log21\\CrashReporter\\Reporters.py",
line 81, in wrap
raise e
File "%localappdata%\Programs\Python\Python310\lib\site-packages\log21\CrashReporter\Reporters.py", line 77, in wrap
File "%localappdata%\\Programs\\Python\\Python310\\lib\\site-packages\\log21\\CrashReporter\\Reporters.py",
line 77, in wrap
return func(*args, **kwargs)
File "<stdin>", line 3, in divide
ZeroDivisionError: division by zero
Expand Down
11 changes: 10 additions & 1 deletion log21/FileHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
from log21.Formatters import DecolorizingFormatter as _DecolorizingFormatter


class DecolorizingFileHandler(_FileHandler):
class FileHandler(_FileHandler):
def __init__(self, filename, mode='a', encoding=None, delay=False, errors=None, formatter=None, level=None):
super().__init__(filename, mode, encoding, delay, errors)
if formatter is not None:
self.setFormatter(formatter)
if level is not None:
self.setLevel(level)


class DecolorizingFileHandler(FileHandler):
terminator = ''

def emit(self, record):
Expand Down
15 changes: 12 additions & 3 deletions log21/Logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import logging as _logging

from getpass import getpass
from getpass import getpass as _getpass
from typing import Sequence as _Sequence, Union as _Union
from logging import raiseExceptions as _raiseExceptions

import log21 as _log21
Expand All @@ -13,10 +14,18 @@


class Logger(_logging.Logger):
def __init__(self, name, level=NOTSET):
def __init__(self, name, level=NOTSET, handlers: _Union[_Sequence[_logging.Handler], _logging.Handler] = None):
super().__init__(name, level)
self.setLevel(level)
self._progress_bar = None
if handlers:
if not isinstance(handlers, _Sequence):
if isinstance(handlers, _logging.Handler):
handlers = [handlers]
else:
raise TypeError("handlers must be a list of logging.Handler objects")
for handler in handlers:
self.addHandler(handler)

def isEnabledFor(self, level):
"""
Expand Down Expand Up @@ -164,7 +173,7 @@ def getpass(self, *msg, args: tuple = (), end='', **kwargs):
"""
msg = ' '.join([str(m) for m in msg]) + end
self._log(self.level if self.level >= NOTSET else NOTSET, msg, args, **kwargs)
return getpass('')
return _getpass('')

def print_progress(self, progress: float, total: float):
"""
Expand Down
7 changes: 6 additions & 1 deletion log21/StreamHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
class StreamHandler(_StreamHandler):
terminator = ''

def __init__(self, handle_carriage_return: bool = True, handle_new_line: bool = True, stream=None):
def __init__(self, handle_carriage_return: bool = True, handle_new_line: bool = True, stream=None, formatter=None,
level=None):
self.HandleCR = handle_carriage_return
self.HandleNL = handle_new_line
super().__init__(stream=stream)
if formatter is not None:
self.setFormatter(formatter)
if level is not None:
self.setLevel(level)

def check_cr(self, record):
if record.msg:
Expand Down
2 changes: 1 addition & 1 deletion log21/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.0"
__version__ = "2.3.1"
__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.3.0'
VERSION = '2.3.1'

setup(
name='log21',
Expand Down

0 comments on commit 672ecdb

Please sign in to comment.