configure_logging

safir.logging.configure_logging(*, name: str, profile: str = 'production', log_level: str = 'info') → None

Configure logging and structlog.

Parameters:
  • name (str) – Name of the logger, which is typically the name of your application’s root namespace.
  • profile (str) –

    The name of the application profile:

    development
    Log messages are formatted for easier reading on the terminal.
    production
    Log messages are formatted as JSON objects.
  • log_level (str) –

    The log level, in string form:

    • DEBUG
    • INFO
    • WARNINGS
    • ERROR

Notes

This function helps you configure a useful logging set up for your application that’s based on structlog.

First, it configures the logger for your application to log to STDOUT. Second, it configures the formatting of your log messages through structlog.

In development mode, messages are key-value formatted, like this:

[info     ] Hello world                    [myapp] answer=42

Here, “Hello world” is the message. answer=42 is a value bound to the logger.

In production mode, messages are formatted as JSON objects:

{"answer": 42, "event": "Hello world", "logger": "myapp",
"level": "info"}

Examples

import structlog
from safir.logging import configure_logging

configure_logging(name="mybot")
logger = structlog.get_logger("mybot")
logger.info("Hello world")