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, optional) –

    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. The default is Profile.production.

  • log_level (LogLevel, optional) – The Python log level. May be given as a LogLevel enum (preferred) or a case-insensitive string. The default is LogLevel.INFO.

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

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")
Return type:

None