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.1.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.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: context_log_python-0.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 20f7e5e6055c104a90a34221c2f1fea28eaf3dce99dc1bde9cea9cbae959b1e6
MD5 915dbaf8f129bca266c0966adfa1afc1
BLAKE2b-256 8b5f0a95fbcb884f6a604fc12fa2a1fce655313eb663d5bf2b36d987e835fd5c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for context_log_python-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5b3abe9091cb2bc3ff9d8ba3438b53ea679d3f00f83112ea153ffaab34b8993b
MD5 fd97c0dc41b8103775eddf4228c8db22
BLAKE2b-256 d26a2a3e3e134a0c236712f4be91715314570791c63416bd0708e31dfea3fa5f

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