Skip to main content

LogFunctionUsage in pylogfunctionusage is a Python decorator designed to enhance the logging capabilities of your functions by wrapping them in a try: block. It logs function usage, including calls, exceptions, and results, to an SQLite table

Project description

pylogfunctionusage

Project Description

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.

Purpose

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.

Features

  • 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.

Performance

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.

Limitations

As this module relies on sqlite db it doesn't support async code.

Difference Over the logging Module

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.

Integration with Visualization Tools

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.

Simplicity

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.

Installation

you can clone the repository and install it from source:

pip install pylogfunctionusage
git clone https://github.com/Ambroise-D/pylogfunctionusage.git
cd pylogfunctionusage
python setup.py install

or copy file pylogfunctionusage.py to your project and simply use it!

Dependencies

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.

Usage

Basic Usage

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"

Error Handling Example

@LogFunctionUsage(if_raise=True)
def function_to_log():
    print(1/0)  # This will throw an error due to 'if_raise = True'

SQL Logging

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.

Example log looks like:

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'

Bugs report

Please submit an issue on GitHub.

Contributing

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.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylogfunctionusage-0.1.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

pylogfunctionusage-0.1.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file pylogfunctionusage-0.1.1.tar.gz.

File metadata

  • Download URL: pylogfunctionusage-0.1.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for pylogfunctionusage-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4238a76812fcc8fd86e5ca9d42a05f4c2dfa7460e5a8f4ab58662fa256a0d1f8
MD5 e27dcce02f879c6a35786dff694346fc
BLAKE2b-256 6d93ce20cef654226ceb752c9e166ff08c757fd42579ea52922a90cf768b2560

See more details on using hashes here.

File details

Details for the file pylogfunctionusage-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pylogfunctionusage-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad6e635540a36773d98d26cbd27a63f678d03c489ba80a94e44f9e8547d3d8e0
MD5 e3c534465df7d981e448f1fe86d3b6d4
BLAKE2b-256 675551b5674d0dcb0481a9036311ccf84c507da91e4c20e3f542b7b57aaca65a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page