configure_logging¶
- safir.logging.configure_logging(*, name: str, profile: str = 'production', log_level: str = 'info', add_timestamp: bool = False) 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
, 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.
The default is
production
.log_level (
str
, optional) –The log level, in string form (case-insensitive):
DEBUG
INFO
WARNINGS
ERROR
The default is
INFO
.add_timestamp (
bool
) – Whether to add an ISO-format timestamp to each log message. The default isFalse
.
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")