Skip to content

Logging

The logging package provides a simple and easy-to-configure logging system.

The logging feature adheres to the 12-factor app methodology, directing logs to stdout. It supports JSON formatting and allows log level configuration via environment variables.

Dependencies

For the moment the logging package is only working with the loguru Python logging library. When orjson is installed, it will be used as the default JSON serializer for faster performance, otherwise, the standard json library will be used.

Loguru is used as the logging library.

For using logging package, please install the required dependencies:

pip install grelmicro[standard]
pip install loguru
pip install loguru orjson

Configure Logging

Just call the configure_logging function to set up the logging system.

from grelmicro.logging import configure_logging

configure_logging()

Settings

You can change the default settings using the following environment variables:

  • LOG_LEVEL: Set the desired log level (default: INFO).
  • LOG_FORMAT: Choose the log format. Options are TEXT and JSON, or you can provide a custom loguru template (default: TEXT).

Examples

Basic Usage

Here is a quick example of how to use the logging system:

from loguru import logger

from grelmicro.logging import configure_logging

configure_logging()

logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message with context", user="Alice")
logger.error("This is an error message with context", user="Bob")

try:
    raise ValueError("This is an exception message")
except ValueError:
    logger.exception(
        "This is an exception message with context", user="Charlie"
    )

The console output, stdout will be:

{"time":"2024-11-25T15:56:36.066922+01:00","level":"INFO","thread":"MainThread","logger":"__main__:<module>:7","msg":"This is an info message"}
{"time":"2024-11-25T15:56:36.067063+01:00","level":"WARNING","thread":"MainThread","logger":"__main__:<module>:8","msg":"This is a warning message with context","ctx":{"user":"Alice"}}
{"time":"2024-11-25T15:56:36.067105+01:00","level":"ERROR","thread":"MainThread","logger":"__main__:<module>:9","msg":"This is an error message with context","ctx":{"user":"Bob"}}
{"time":"2024-11-25T15:56:36.067134+01:00","level":"ERROR","thread":"MainThread","logger":"__main__:<module>:14","msg":"This is an exception message with context","ctx":{"user":"Charlie","exception":"ValueError: This is an exception"}}

FastAPI Integration

You can use the logging system with FastAPI as well:

from contextlib import asynccontextmanager

from fastapi import FastAPI
from loguru import logger

from grelmicro.logging import configure_logging


@asynccontextmanager
def lifespan_startup():
    # Ensure logging is configured during startup
    configure_logging()
    yield


app = FastAPI()


@app.get("/")
def root():
    logger.info("This is an info message")
    return {"Hello": "World"}

Warning

It is crucial to call configure_logging during the lifespan of the FastAPI application. Failing to do so may result in the FastAPI CLI resetting the logging configuration.