Logger is a Python class that provides logging functionality. It allows you to create a logger with a specific name, log file, and logging level. It also provides decorators to disable logging, enable debug, time function execution, and tag the start and end of a function.
I made this script for general scripting, trying to utilize the DRY principle. There are some convenience decorators that I used when I didn't have the convenience of a debugger.
You can install the package directly from GitHub using pip:
pip install git+https://github.com/toann1980/Logger.git
from pathlib import Path
from logger import Logger
# Create a logger
filename = Path(__file__).stem
logger = Logger(logger_name=filename)
logger.set_logging_level("debug")
# Use the logger
logger.debug('This is an debug message')
Logger provides several decorators that can be used to modify the behavior of your functions:
@logger.disable_logging
: This decorator disables the debug output of the decorated function.@logger.enable_debug
: This decorator enables the debug output of the decorated function.@logger.time_function
: This decorator measures the execution time of the decorated function and logs the result.@logger.tag_start_end
: This decorator adds logging statements to the start and end of the decorated function.
In this example, my_function
will log its execution time and a start and end message.
from pathlib import Path
from logger import Logger
# Create a logger
filename = Path(__file__).stem
logger = Logger(logger_name=filename)
logger.set_logging_level("debug")
class MyClass:
def __init__(self):
pass
@logger.time_function
@logger.tag_start_end
def my_function(self):
logger.info('This is an info message')
my_class = MyClass()
my_class.my_function()
If you want to contribute to this project, please feel free to fork the repository, make your changes, and open a pull request. I appreciate any contributions!