LogFunctionUsage is a Python decorator designed to enhance the logging capabilities of your programs functions by wrapping functions in a try: block. It logs function usage, including calls, exceptions, and results, to an SQLite table, making it easier to monitor and debug your code.
This module was originally written to monitor complex calculations during a master's thesis project. The need for precise and efficient logging arose, leading to the development of a solution that integrates seamlessly with SQLite databases. This enables comprehensive monitoring and debugging without significant performance overhead.
- Database Configuration: Customize the SQLite database name used for logging.
- Table Name Customization: Specify the table name for storing logs.
- Error Handling: Choose whether the program continues or stops on an error.
- Log Table Refresh: Option to clear the log table with each program run.
- Error Printing: Configure whether errors are printed to the console.
- Result Logging: Optionally log the results of functions.
- Robustness: Choose between committing logs immediately or at the end of the program.
- Live Monitoring: Enable live monitoring of program execution.
- Write-Ahead Logging (WAL): Significant speedup for massive function calls, but use with caution.
LogFunctionUsage is optimized for speed by using pure SQL expressions, which contrasts with tools based on ORM systems like SQLAlchemy. This approach ensures faster logging. On laptop with simple functions:
- Adds approximately 0.7ms (0.0007s) to function run time in WAL mode.
- Adds approximately 3ms (0.003s) to function run time in normal mode.
As this module relies on sqlite db it doesn't support async code.
While the Python logging module is versatile and powerful, LogFunctionUsage offers specific advantages for function-level logging:
- Granular Control: Logs detailed function execution data, including arguments, keyword arguments, results, and exceptions.
- SQL Storage: Logs are stored in an SQLite table, making them easily accessible for querying and integration with other tools.
- Performance: Faster logging performance due to direct SQL expressions.
The sql table with logs can be plugged into any visualization or reporting tool, such as:
- Dash
- PowerBI
- Tableau
This flexibility allows for advanced analysis, monitoring and visualization of function execution data.
Using this module is incredibly simple. All you need is to decorate your functions, and the logging is taken care of. No complex setup or extensive configuration is required.
You can pip install:
pip install pylogfunctionusageOr you can clone the repository and install it from source:
git clone https://github.com/Ambroise-D/pylogfunctionusage.git
cd pylogfunctionusage
python setup.py installor copy file pylogfunctionusage.py to your project and simply use it!
LogFunctionUsage does not require any additional packages beyond the standard Python library. It uses built-in modules such as sqlite3, ensuring that you can use it without needing to install external dependencies.
Create an instance of LogFunctionUsage and use it across your code:
from pylogfunctionusage import LogFunctionUsage
log = LogFunctionUsage()
OR
log = LogFunctionUsage(db_name='your_database_name.db',
table_name='function_logs',
if_raise=False,
print_error=True,
refresh_log_table=True,
log_results=False,
robust=True,
live_monitor=False,
wal=False)
@log
def func_to_log(x, y="y"):
return "some results"@LogFunctionUsage(if_raise=True)
def function_to_log():
print(1/0) # This will throw an error due to 'if_raise = True'Logs are saved to an SQLite table with the following columns:
- function_name: Name of the function.
- type: Type of call (Start, End, or Except).
- code_line: Line of code that called the function.
- timestamp: Time of function call.
- args: Argument names passed to the function.
- kwargs: Keyword arguments passed to the function.
- result: Results and exceptions of the function.
| id | function_name | type | code_line | timestamp | args | kwargs | result |
|---|---|---|---|---|---|---|---|
| 1 | func_to_log | START | 42 | 2024-04-20 16:20:00 | "('some_str',)" | "{'y': 'y'}" | None |
| 2 | func_to_log | END | 42 | 2024-04-20 16:20:42 | "('some_str',)" | "{'y': 'y'}" | 'some results' |
Please submit an issue on GitHub.
We welcome contributions to the LogFunctionUsage project! If you have ideas, suggestions, or improvements, or if you want to participate in the development of this module, please feel free to open an issue or submit a pull request on our GitHub repository. Your contributions help us enhance the functionality and usability of the module, and we appreciate your input.
This project is licensed under the MIT License - see the LICENSE file for details.