-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72a2f5d
commit 6dde758
Showing
4 changed files
with
104 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from typing import Callable, Type | ||
import logging | ||
from functools import wraps | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
def get_logger(module_name: str) -> logging.Logger: | ||
""" | ||
Creates and configures a logger for the given module. | ||
Args: | ||
module_name (str): Name of the module requesting the logger. | ||
Returns: | ||
logging.Logger: Configured logger instance. | ||
""" | ||
logger = logging.getLogger(module_name) | ||
if not logger.hasHandlers(): # Prevents adding handlers multiple times if reused | ||
handler = logging.StreamHandler() # Logs to stdout by default | ||
formatter = logging.Formatter( | ||
"%(asctime)s - %(name)s - %(levelname)s - %(message)s" | ||
) | ||
handler.setFormatter(formatter) | ||
logger.addHandler(handler) | ||
logger.setLevel(logging.INFO) # Set your desired logging level here | ||
return logger | ||
|
||
|
||
def log_execution(func: Callable) -> Callable: | ||
"""Decorator to log the execution of a function or method.""" | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
logger.info(f"Entering: {func.__qualname__}") | ||
result = func(*args, **kwargs) | ||
logger.info(f"Exiting: {func.__qualname__}") | ||
return result | ||
return wrapper | ||
|
||
|
||
def log_all_methods(cls: Type): | ||
"""Class decorator to log all method calls in a class.""" | ||
for attr_name, attr_value in cls.__dict__.items(): | ||
if isinstance(attr_value, property): | ||
getter = log_execution(attr_value.fget) if attr_value.fget else None | ||
setter = log_execution(attr_value.fset) if attr_value.fset else None | ||
setattr(cls, attr_name, property(getter, setter)) | ||
elif callable(attr_value): | ||
if isinstance(attr_value, staticmethod): | ||
setattr(cls, attr_name, staticmethod(log_execution(attr_value.__func__))) | ||
elif isinstance(attr_value, classmethod): | ||
setattr(cls, attr_name, classmethod(log_execution(attr_value.__func__))) | ||
else: | ||
setattr(cls, attr_name, log_execution(attr_value)) | ||
return cls | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters