diff --git a/CHANGES-LOG.md b/CHANGES-LOG.md index b4f97e2..9d35df4 100644 --- a/CHANGES-LOG.md +++ b/CHANGES-LOG.md @@ -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. diff --git a/README.md b/README.md index 368a682..a58c374 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/log21/CrashReporter/Reporters.py b/log21/CrashReporter/Reporters.py index 137c29e..eb02325 100644 --- a/log21/CrashReporter/Reporters.py +++ b/log21/CrashReporter/Reporters.py @@ -234,9 +234,11 @@ class EmailReporter(Reporter): >>> divide(10, 0) Traceback (most recent call last): File "", line 1, in - 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 "", line 3, in divide ZeroDivisionError: division by zero diff --git a/log21/FileHandler.py b/log21/FileHandler.py index bddd1a4..d4f7678 100644 --- a/log21/FileHandler.py +++ b/log21/FileHandler.py @@ -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): diff --git a/log21/Logger.py b/log21/Logger.py index 3a5315d..30a8988 100644 --- a/log21/Logger.py +++ b/log21/Logger.py @@ -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 @@ -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): """ @@ -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): """ diff --git a/log21/StreamHandler.py b/log21/StreamHandler.py index e4510c5..161f3b3 100644 --- a/log21/StreamHandler.py +++ b/log21/StreamHandler.py @@ -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: diff --git a/log21/__init__.py b/log21/__init__.py index 7b2f2cb..042a0f2 100644 --- a/log21/__init__.py +++ b/log21/__init__.py @@ -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', diff --git a/setup.py b/setup.py index e456333..cb4bbeb 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.3.0' +VERSION = '2.3.1' setup( name='log21',