Skip to main content

MLoggerAI: AI-powered Python traceback analyzer

Project description

MLoggerAI is a Python package for automatically analyzing Python tracebacks using AI models (OpenAI / LM Studio) and providing concise bug fixes directly in the logs.

Features

  • Automatically intercepts logged errors (ERROR) and sends them to AI.
  • Prints only the AI solution, avoiding redundant messages or debug noise.
  • Supports customizable output language (e.g., English, Italian).
  • Logs can be saved both to console and to a file using RotatingFileHandler.

Installation

Install directly from the Git repository:

pip install "git+ssh://git@github.com/perronemirko/mloggerai.git"

Usage Examples Example 1: Basic usage

from mloggerai.errorsolver import ErrorSolver
from dotenv import load_dotenv
load_dotenv()  # Carica le variabili da .env
def main():
    solver = ErrorSolver()
    logger = solver.logger

    try:
        1 / 0
    except Exception as e:
        logger.error("Errore catturato", exc_info=e)

if __name__ == "__main__":
    main()

Configuration with .env

Create a .env file in the project root for defaults: OPENAI_API_URL="<llama.cpp| Ollama| Openai|lmstudio URLS:PORT/v1"> OPENAI_API_KEY="<MY_KEY>" OPENAI_API_MODEL="<MY_MODEL>" OPENAI_API_PROMPT="find the bug and alwayse propose the best solution in a very concise way"

Initialize ErrorSolver with model and desired language

solver = ErrorSolver(
    model="<YOUR_LLM_MODEL>",
    output_language="english"
)

logger = solver.logger

try:
    x = 1 / 0
except Exception as e:
    # Logs the error; AI handler intercepts and prints only AI solution
    logger.error("Caught an exception", exc_info=e)

Output:

🧠💡 AI Solution: Bug: Division by zero. Modify the operation to avoid dividing by zero.

Example 2: Using a custom log file and log level

from mloogerai.errorsolver import ErrorSolver
import logging

solver = ErrorSolver(
    model="<YOUR_LLM_MODEL>",
    log_file="logs/my_custom.log",
    log_level=logging.INFO,
    output_language="italian"
)

logger = solver.logger

try:
    my_list = []
    print(my_list[1])  # IndexError
except Exception as e:
    logger.error("Caught an exception", exc_info=e)
Output (console and file logs/my_custom.log):
🧠💡 Soluzione AI: Bug: Indice fuori intervallo. Controllare che l'elemento esista prima di accedere all'indice.
Example 3: Logging multiple exceptions
from mloggerai.errorsolver import ErrorSolver
import logging

solver = ErrorSolver(model="<YOUR_LLM_MODEL>")
logger = solver.logger

for val in [0, "a", None]:
    try:
        result = 10 / val
    except Exception as e:
        logger.error(f"Error with value: {val}", exc_info=e)
🧠💡 AI Solution: Bug: Division by zero or invalid type. Ensure the value is a non-zero number.

Advanced Configuration

from mloggerai import ErrorSolverBuilder

logger = (
    ErrorSolverBuilder()
    .with_model("lmstudio-community/llama-3.2-3b-instruct")
    .with_output_language("inglese")
    .with_log_file("logs/custom.log")
    .build()
).logger

logger.info("Applicazione avviata")
for val in [0, "a", None]:
    try:
        result = 10 / val
    except Exception as e:
        logger.error(f"Error with value: {val}", exc_info=e)
TypeError: unsupported operand type(s) for /: 'int' and 'str'
2025-10-11 12:57:07,736 - DEBUG - 🧠💡 Soluzione AI: Il bug è che il tipo di `val` non è stato verificato e potrebbe essere una stringa. 

Soluzione:
```python
# Invece di utilizzare un tipo di dati generico, specificare il tipo di dati corretto
result = 10 / int(val)

In questo modo, si assicura che val sia sempre un numero intero prima di tentare la divisione. 2025-10-11 12:57:07,736 - ERROR - Error with value: None Traceback (most recent call last): File "/home/albus/PycharmProjects/mloggerai/mloggerai-py/tests/test_errorsolverbuilder.py", line 14, in result = 10 / val TypeError: unsupported operand type(s) for /: 'int' and 'NoneType' 2025-10-11 12:57:08,192 - DEBUG - 🧠💡 Soluzione AI: Il bug è che il valore val è None, quindi non può essere utilizzato per la divisione.

La soluzione è controllare se il valore è None prima di tentare la divisione.

Esempio corretto:

result = 10 / (val if val is not None else 1)

In questo modo, se val è None, il risultato sarà 10.

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

mloggerai-0.0.0.dev8.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mloggerai-0.0.0.dev8-py3-none-any.whl (32.8 kB view details)

Uploaded Python 3

File details

Details for the file mloggerai-0.0.0.dev8.tar.gz.

File metadata

  • Download URL: mloggerai-0.0.0.dev8.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.16

File hashes

Hashes for mloggerai-0.0.0.dev8.tar.gz
Algorithm Hash digest
SHA256 82fd36245f723af7a32e5c2960593ded1f4f2a43946d3daf957ffeaf67322512
MD5 18af57bdf39cd3c3a5ec197df049d556
BLAKE2b-256 a094c22dbe9de44611c3698f08b151c3230e55e3dd220ed97f55d2e5a19ee1ec

See more details on using hashes here.

File details

Details for the file mloggerai-0.0.0.dev8-py3-none-any.whl.

File metadata

  • Download URL: mloggerai-0.0.0.dev8-py3-none-any.whl
  • Upload date:
  • Size: 32.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.16

File hashes

Hashes for mloggerai-0.0.0.dev8-py3-none-any.whl
Algorithm Hash digest
SHA256 5e9693498603761ba8b3f6bcaa2f7dd5dbaefe7be43acfe0be0f6a05de61ebb2
MD5 bd63181d13c3e9966ece9ea55e1f0e3f
BLAKE2b-256 c60f26eaf52d632f0fd95c52453b3e23773a46daba375e451f003161837b726f

See more details on using hashes here.

Supported by

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