Skip to content

Conversation

@sri-dhurkesh
Copy link

Summary

Integrates Structlog as a wrapper around Python's standard logging to improve structured logging, request tracing, and log analysis. Adds separate handlers for file and console output with configurable formats and metadata.

Key Features

  • A Structlog-based wrapper has been implemented in logger.py on top of Python’s standard logging module.
  • A request_id is attached to every incoming request, allowing logs from the same request to be easily traced across the application.
  • Console logs use a human-readable, colored format for better readability during development.
  • File logs can be configured to use JSON format, making them easier to parse and analyze using log aggregation or monitoring tools.
  • All logging behavior (log level, format, and included request metadata) is fully configurable via settings, allowing flexibility across environments.

Log Configuration Settings

# config.py
class FileLoggerSettings(BaseSettings):
    FILE_LOG_MAX_BYTES: int = 10 * 1024 * 1024
    FILE_LOG_BACKUP_COUNT: int = 5
    FILE_LOG_FORMAT_JSON: bool = True
    FILE_LOG_LEVEL: str = "INFO"

    # Include request ID, path, method, client host, and status code in the file log
    FILE_LOG_INCLUDE_REQUEST_ID: bool = True
    FILE_LOG_INCLUDE_PATH: bool = True
    FILE_LOG_INCLUDE_METHOD: bool = True
    FILE_LOG_INCLUDE_CLIENT_HOST: bool = True
    FILE_LOG_INCLUDE_STATUS_CODE: bool = True


class ConsoleLoggerSettings(BaseSettings):
    CONSOLE_LOG_LEVEL: str = "INFO"
    CONSOLE_LOG_FORMAT_JSON: bool = False

    # Include request ID, path, method, client host, and status code in the console log
    CONSOLE_LOG_INCLUDE_REQUEST_ID: bool = True
    CONSOLE_LOG_INCLUDE_PATH: bool = False
    CONSOLE_LOG_INCLUDE_METHOD: bool = False
    CONSOLE_LOG_INCLUDE_CLIENT_HOST: bool = False
    CONSOLE_LOG_INCLUDE_STATUS_CODE: bool = False

Reference Image Console

Console Log Image
image
Log File Image (app.log)
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant