Skip to content

Commit

Permalink
Merge pull request #3 from barealek/0.1.3
Browse files Browse the repository at this point in the history
Update mainstream to 0.1.3
  • Loading branch information
barealek authored Jul 11, 2023
2 parents 0a97dfc + 00c7cdb commit 0977d16
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion VERSION-BUMP-ME
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.2
0.1.3
1 change: 0 additions & 1 deletion styledlogger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from .classes.styleconfig import StyleConfig

from .classes.callback import Callback

__AUTHOR__ = "ImAlek (https://github.com/barealek)"
__LICENSE__ = "See https://github.com/barealek/StyledLogger for license"
10 changes: 6 additions & 4 deletions styledlogger/classes/callback.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .callbackctx import CallbackContext

class Callback:
"""
A callback that can be added to a logger. The callback will be called with the logger name as the first argument and message as the second argument.
Expand All @@ -12,16 +14,16 @@ class Callback:
The callback function. This will be called with the logger name as the first argument, the level as the second and message as the third argument.
"""

def __init__(self, name: str, activation_level: int, callback: callable):
def __init__(self, name: str, activation_levels: int | tuple, callback: callable):
self.name = name
self.activation_level = activation_level
self.activation_levels = activation_levels
self.callback = callback

def run_callback(self, level, message):
self.callback(self.name, level, message)
self.callback(CallbackContext(self.name, level, message))

def __repr__(self):
return f"<Callback name={self.name} activation_level={self.activation_level} callback={self.callback}>"
return f"<Callback name={self.name} activation_levels={self.activation_levels} callback={self.callback}>"

def __str__(self):
return self.__repr__()
14 changes: 14 additions & 0 deletions styledlogger/classes/callbackctx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from dataclasses import dataclass

@dataclass
class CallbackContext:
"""Context for a callback function.
Attributes:
name: The name of the logger.
level: The log level.
message: The log message.
"""
name: str
level: int
message: str
35 changes: 23 additions & 12 deletions styledlogger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,29 @@ def __init__(self, name: str, *, file: str = None, level: int = 1) -> None:
self.file_path = file
self.callbacks = []

def set_level(self, level):
def set_level(self, level: int):
"""
Set the log level. 0 = debug, 1 = info, 2 = warn, 3 = error, 4 = fatal. All prints lower than the level will be ignored.
"""
self.level = level

# Create a decorator, which takes in a name, and adds the decorated function to the logger's callbacks
def callback(self, name: str, level: int = 1):
def callback(self, name: str, levels: int | tuple[int, ...]):
"""
Decorator to add a callback to the logger.
:param name: The name of the callback
:param level: The level which the callback will be activated on. Higher levels than the specified level will be activated as well.
:param levels: The levels which the callback will be called upon.
The decorated function should take these parameters:
:param name: The name of the logger which called the callback
:param level: The level which called the callback
:param message: The message which the logger was called with
The decorated function will receive an instance of `styledlogger.CallbackContext`,
with the following attributes:
:param name: The name of the logger which activated the callback.
:param level: The log level which activated the callback.
:param message: The content of the log message which activated the callback.
"""

def decorator_function(original_func):
self.callbacks.append(LoggerCallback(name, level, original_func))
self.callbacks.append(LoggerCallback(name, levels, original_func))
return original_func

return decorator_function
Expand All @@ -66,50 +67,60 @@ def debug(self, message):
"""
Log a debug message
"""
self._process_callbacks(message, Debug)
if self.level <= 0:
self._log(message, Debug)

def info(self, message):
"""
Log an info message
"""
self._process_callbacks(message, Info)
if self.level <= 1:
self._log(message, Info)

def warn(self, message):
"""
Log a warning message
"""
self._process_callbacks(message, Warn)
if self.level <= 2:
self._log(message, Warn)

def error(self, message):
"""
Log an error message
"""
self._process_callbacks(message, Error)
if self.level <= 3:
self._log(message, Error)

def fatal(self, message):
"""
Log a fatal message
"""
self._process_callbacks(message, Fatal)
if self.level <= 4:
self._log(message, Fatal)

def system(self, message):
"""
Log a system message
"""
self._process_callbacks(message, System)
self._log(message, System)

def _process_callbacks(self, print_type, message):
def _process_callbacks(self, message, print_type):
for callback in self.callbacks:
if callback.activation_level <= print_type.level:
callback.run_callback(level=print_type.level, message=message)
if isinstance(callback.activation_levels, int):
if print_type.level == callback.activation_levels:
callback.run_callback(level=print_type.level, message=message)
return
if isinstance(callback.activation_levels, tuple):
if print_type.level in callback.activation_levels:
callback.run_callback(level=print_type.level, message=message)

def _log(self, message, print_type: PrintType):
self._process_callbacks(print_type, message)

if self.is_muted:
return
Expand Down

0 comments on commit 0977d16

Please sign in to comment.