configure_logging#

safir.logging.configure_logging(*, name, profile=Profile.production, log_level=LogLevel.INFO, add_timestamp=False)#

Configure logging and structlog.

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

  • profile (Profile | str, default: <Profile.production: 'production'>) –

    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.

    May be given as a Profile enum value (preferred) or a string.

  • log_level (LogLevel | str, default: <LogLevel.INFO: 'INFO'>) – The Python log level. May be given as a LogLevel enum (preferred) or a case-insensitive string.

  • add_timestamp (bool, default: False) – Whether to add an ISO-format timestamp to each log message.

Return type:

None

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",
"severity": "info"}

Examples

import structlog
from safir.logging import configure_logging

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