Skip to main content

A simple Python logging utility

Project description

EchorTech Logging

A robust logging middleware for applications that provides structured logging with correlation IDs, trace IDs, and standardized log formats.

Installation

pip install echor-logger

Features

  • 🔍 Structured logging for applications
  • 📊 Support for info, warn, and error log levels
  • 🔗 Automatic correlation ID and trace ID tracking
  • ⏱️ Request timing and response metrics
  • 🏷️ Custom tagging support
  • 🛡️ Middleware for request context preservation
  • 🚀 Easy integration with FastAPI

Quick Start

from fastapi import FastAPI, Request
from starlette.middleware.base import BaseHTTPMiddleware
from echor_logger import echorLogger
import uuid

app = FastAPI()

class TraceIdMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        request.state.trace_id = str(uuid.uuid4())
        request.state.correlation_id = str(uuid.uuid4())
        response = await call_next(request)
        return response

app.add_middleware(TraceIdMiddleware)

@app.get("/success")
async def success_route(request: Request):
    data = {
        "statusCode": 200,
        "message": "This is a success message",
        "responseTime": 123,
        "result": {"key": "value"}
    }

    modified_request = {
        'info': {'received': None},
        'traceId': request.state.trace_id,
        'correlationId': request.state.correlation_id,
        'auth': {'credentials': {'sub': 'user123'}},
        'method': request.method,
        'url': str(request.url)
    }

    echorLogger.info(modified_request, ['SUCCESS_TAG'], data)
    return data

Detailed Usage Examples

Warning Logging

@app.get("/warn")
async def warn_route(request: Request):
    data = {
        "statusCode": 200,
        "message": "This is a warning message",
        "responseTime": 150,
        "result": {"key": "warning-value"}
    }

    modified_request = {
        'info': {'received': None},
        'traceId': request.state.trace_id,
        'correlationId': request.state.correlation_id,
        'auth': {'credentials': {'sub': 'user123'}},
        'method': request.method,
        'url': str(request.url)
    }

    echorLogger.warn(modified_request, ['WARN_TAG'], data)
    return data

Error Logging

@app.get("/error")
async def error_route(request: Request):
    try:
        # Simulated error scenario
        raise Exception("Simulated error occurred")
    except Exception as error:
        data = {
            'statusCode': 500,
            'message': 'An error occurred',
            'errorCode': 'ERR_SIMULATION',
            'responseTime': 200
        }
        
        modified_request = {
            'info': {'received': None},
            'traceId': request.state.trace_id,
            'correlationId': request.state.correlation_id,
            'auth': {'credentials': {'sub': 'user123'}},
            'method': request.method,
            'url': str(request.url)
        }

        echorLogger.error(modified_request, ['ERROR_TAG'], data, error)
        return {"error": "An error occurred"}

API Reference

Logging Methods

echorLogger.info(request_context, tags, data)

Logs information level messages.

echorLogger.warn(request_context, tags, data)

Logs warning level messages.

echorLogger.error(request_context, tags, data, error)

Logs error level messages with error stack traces.

Request Context Structure

{
    'info': {'received': None},
    'traceId': 'unique-trace-id',
    'correlationId': 'unique-correlation-id',
    'auth': {
        'credentials': {
            'sub': 'user_identifier'
        }
    },
    'method': 'HTTP_METHOD',
    'url': 'request_url'
}

Response Data Structure

Success Response

{
    'statusCode': 200,
    'message': 'Success message',
    'responseTime': 123,
    'result': {'key': 'value'}
}

Error Response

{
    'statusCode': 500,
    'message': 'Error message',
    'errorCode': 'ERR_CODE',
    'responseTime': 200
}

Dependencies

  • python-dotenv

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

echor_logger-0.2.5.tar.gz (4.3 kB view details)

Uploaded Source

File details

Details for the file echor_logger-0.2.5.tar.gz.

File metadata

  • Download URL: echor_logger-0.2.5.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.7

File hashes

Hashes for echor_logger-0.2.5.tar.gz
Algorithm Hash digest
SHA256 fc10f58a90f9c563ce07b2e71e616925e9d8198c2d4b1d5c17153ec8ca804020
MD5 0c607e1b26c41384d6aed3362fab2233
BLAKE2b-256 ed5cc169fd8a403f22873e4d9080aa74f63a8e4d0e53b53e4c546f4d0fff671d

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