Skip to main content

Type-safe, customizable logging configuration for Python

Project description


Logo

Type-safe, customizable logging configuration for Python
Explore the docs »

About the Project · Getting Started · Basic Usage · Documentation · Contributing

About the Project

Loggle is a Python logging library that provides a type-safe interface for logging configuration, allowing you to define and configure your logging setup for projects of any size. It provides schemas for logging configuration, pre-built filters and formatters you can use, and integrates with popular libraries including Uvicorn and SQLALchemy.

All of this makes it much easier to maintain complex logging configurations, and customize loggers in your application. No more poring over Python's outdated logging documentation!

Getting Started

Loggle is available on PyPI, so you can use pip (or another package manager) to install it:

pip install loggle

Basic Usage

Declarations

First, you must declare the names of the filters, formatters, handlers, and loggers your configuration contains:

from loggle import FilterName, FormatterName, AtomicHandlerName, CompositeHandlerName, LoggerName
from loggle.collections import UvicornLoggerName, SQLAlchemyLoggerName


class AppFilterName(FilterName):
    ERROR = "error"


class AppFormatterName(FormatterName):
    STANDARD = "standard"
    JSON = "json"


class AppAtomicHandlerName(AtomicHandlerName):
    STANDARD = "standard"
    ERROR = "error"
    JSON_FILE = "json_file"


class AppCompositeHandlerName(CompositeHandlerName):
    QUEUE = "queue"


class AppLoggerName(LoggerName):
    APP = "app"
    UVICORN = UvicornLoggerName.ROOT
    UVICORN_ACCESS = UvicornLoggerName.ACCESS
    UVICORN_ERROR = UvicornLoggerName.ERROR
    UVICORN_ASGI = UvicornLoggerName.ASGI
    SQLALCHEMY_ENGINE = SQLAlchemyLoggerName.ENGINE
    SQLALCHEMY_POOL = SQLAlchemyLoggerName.POOL
    SQLALCHEMY_DIALECTS = SQLAlchemyLoggerName.DIALECTS
    SQLALCHEMY_ORM = SQLAlchemyLoggerName.ORM

Implementations

Now, you can define the actual implementations in your logging configuration:

from pathlib import Path

from loggle import StreamHandlerSchema, LoggingLevel, LoggingStream, FileHandlerSchema, CompositeHandlerSchema, LoggersSchema
from loggle.collections import Filters, Formatters, HandlerClasses


FILTERS = {
    AppFilterName.ERROR: Filters.ERROR,
}

FORMATTERS = {
    AppFormatterName.STANDARD: Formatters.STANDARD,
    AppFormatterName.JSON: Formatters.JSON,
}

HANDLERS = {
    AppAtomicHandlerName.STANDARD: StreamHandlerSchema(
        handler_class=HandlerClasses.STREAM,
        filters=[AppFilterName.ERROR],
        formatter=AppFormatterName.STANDARD,
        level=LoggingLevel.DEBUG,
        stream=LoggingStream.STANDARD_OUT,
    ),
    AppAtomicHandlerName.ERROR: StreamHandlerSchema(
        handler_class=HandlerClasses.STREAM,
        formatter=AppFormatterName.STANDARD,
        level=LoggingLevel.ERROR,
        stream=LoggingStream.STANDARD_ERROR,
    ),
    AppAtomicHandlerName.JSON_FILE: FileHandlerSchema(
        handler_class=HandlerClasses.JSON_FILE,
        formatter=AppFormatterName.JSON,
        level=LoggingLevel.DEBUG,
    ),
    AppCompositeHandlerName.QUEUE: CompositeHandlerSchema(
        handler_class=HandlerClasses.QUEUE,
        handlers=list(AppAtomicHandlerName),
    ),
}

LOGGERS = (
    LoggersSchema[AppLoggerName, AppAtomicHandlerName | AppCompositeHandlerName]
    .from_json(Path(f"./path/to/loggers/configuration.json"))
)

Configuration

Now, define the actual configuration. This follows the same structure as Python's logging.dictConfig input:

from loggle import LoggingConfiguration, Logger


LOGGING_CONFIGURATION = (
    LoggingConfiguration[AppFilterName, AppFormatterName, AppAtomicHandlerName, AppCompositeHandlerName, AppLoggerName].create(
        filters=FILTERS,
        formatters=FORMATTERS,
        handlers=HANDLERS,
        loggers=LOGGERS,
    )
)

APP_LOGGER = Logger(name=AppLoggerName.APP)

Usage

Finally, you can apply the configuration, and use loggers:

LOGGING_CONFIGURATION.set_configuration()

APP_LOGGER.debug("This is a debug message.")
APP_LOGGER.info("This is an informational message.")
APP_LOGGER.error("This is an error message.")

You can also get the underlying configuration dictionary that is passed into logging.dictConfig, which can be useful in Uvicorn applications:

LOGGING_CONFIGURATION.to_configuration_dictionary()

Contributing

Distributed under the MIT License. Feel free to contribute to the project by opening issues or submitting pull requests. For any questions or suggestions, don't hesitate to reach out. Enjoy better, more type-safe logging with Loggle!

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

loggle-0.1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

loggle-0.1.0-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file loggle-0.1.0.tar.gz.

File metadata

  • Download URL: loggle-0.1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.1 Linux/6.8.0-1021-azure

File hashes

Hashes for loggle-0.1.0.tar.gz
Algorithm Hash digest
SHA256 699444a2f7a3527d1fa8386bd086133b9043ad54ef184e47e6e473c0309b4c91
MD5 fb60768f38844a382c247c63986e200e
BLAKE2b-256 9712e433c4ca62f07edc19078df5d9f2258ae7bd386ba4853c0cc17da77f4cb9

See more details on using hashes here.

File details

Details for the file loggle-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: loggle-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.1 Linux/6.8.0-1021-azure

File hashes

Hashes for loggle-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e94bd390eeb5663631c87e4f0e288b9774554cbaf6d846e6c8c0bd583e3c7fff
MD5 d066c83343e1721f580bc9cca2ac1cc4
BLAKE2b-256 58f6c9c1dc3fdf09d87524443e3bfcf790975e2909103eef7837d3d5a931d57c

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