Skip to main content

A contextual logging framework for Python

Project description

📘 SmartProfiler & ContextLogger

A lightweight Python toolkit for profiling function execution times and enhancing logging with contextual information and AI-powered error suggestions.


✨ Features

🔍 SmartProfiler

  • Captures nested function timings using sys.setprofile.
  • Measures execution time of each function call.
  • Provides detailed breakdowns for performance tuning.

📝 ContextLogger

  • Adds caller context (function, file, line number) to every log.
  • Supports JSON and human-readable logging modes.
  • Captures exceptions with tracebacks.
  • Generates error fix suggestions using Hugging Face’s transformers.

🚀 Installation

pip install transformers torch

torch is required for Hugging Face transformers.


⚡ Quick Start

1. Setup Logging

import logging
from your_module import ContextLogger

logging.basicConfig(level=logging.DEBUG)
base_logger = logging.getLogger("app")

logger = ContextLogger(base_logger, json_mode=False)

2. Logging Examples

Info & Debug Logs

logger.info("Application started")
logger.debug("Configuration loaded successfully")

Warnings

logger.warning("Low disk space detected")

Errors with Auto-Suggestions

try:
    1 / 0
except Exception:
    logger.error("Division error occurred")

📌 Example output:

2025-08-26 19:45:12 - ERROR - <module> (app.py:12) - Division error occurred
Traceback:
Traceback (most recent call last):
  File "app.py", line 12, in <module>
    1 / 0
ZeroDivisionError: division by zero

Suggestion: Check if the divisor is zero before performing division.

JSON Logging

json_logger = ContextLogger(base_logger, json_mode=True)
json_logger.info("User API call", extra={"endpoint": "/users", "method": "GET"})

📌 Example JSON output:

{
  "timestamp": "2025-08-26 19:45:12",
  "level": "INFO",
  "message": "User API call",
  "context": "<module> (app.py:20)",
  "extra": {"endpoint": "/users", "method": "GET"}
}

⏱️ Profiling Functions

@logger.timeit
def process_data(n):
    total = 0
    for i in range(n):
        total += helper(i)
    return total

def helper(x):
    return x * x

result = process_data(1000)
print("Result:", result)

📌 Example output:

2025-08-26 19:45:12 - INFO - process_data (app.py:10) - Function process_data executed in 0.003200 seconds
2025-08-26 19:45:12 - DEBUG - process_data (app.py:10) -    └── helper (app.py:16) took 0.001500 seconds

🔧 Advanced Usage

Adding Metadata

logger.info("User logged in", extra={"user_id": 42, "role": "admin"})

Manual Suggestions

logger.error("File not found", suggestion="Verify the file path before accessing.")

📊 Use Cases

  • Debugging slow functions with nested profiling.
  • Capturing and understanding errors faster with AI suggestions.
  • Structured JSON logs for microservices & observability tools.
  • Automatic caller context for simpler debugging.

📜 License

MIT License – Free to use and modify.


👨‍💻 Author

Crafted by an Ramakrishna Bapathu to improve debugging, observability, and developer productivity.

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

context_log_python-0.2.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

context_log_python-0.2.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file context_log_python-0.2.0.tar.gz.

File metadata

  • Download URL: context_log_python-0.2.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for context_log_python-0.2.0.tar.gz
Algorithm Hash digest
SHA256 64a56fd1c2054133b3e006730e5e806955744a49bc77c294e57b4dc129d9c357
MD5 bf225c30dc21ded7bc4506f39d9243d0
BLAKE2b-256 2cb3382c25883dfbb8f24d1dd586f269616e48c69b7323777a4b8c2bd259126c

See more details on using hashes here.

File details

Details for the file context_log_python-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for context_log_python-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c588e82f8e488921ce3253a4230f40cb0e518ed80ebfe174576a22fc18b22dc9
MD5 faae549d0f6a0ce5fe5fe40df04d1c88
BLAKE2b-256 9fadfdb3b7fcbcc359f0a5da19ae9231a61bca3a03fcba4f348d97ca56bf8b38

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