Skip to content

Logging

Palfrey supports built-in logging and external logging configuration files.

Supported levels

  • critical
  • error
  • warning
  • info
  • debug
  • trace

CLI examples:

palfrey myapp.main:app --log-level info
palfrey myapp.main:app --log-level debug --access-log

Configuration inputs

  • JSON/YAML file for logging.config.dictConfig
  • INI-style file for logging.config.fileConfig

Programmatic JSON config example:

from __future__ import annotations

import json
from pathlib import Path

logging_config = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "default": {"format": "%(asctime)s %(levelname)s %(name)s %(message)s"},
    },
    "handlers": {
        "default": {"class": "logging.StreamHandler", "formatter": "default"},
    },
    "root": {"handlers": ["default"], "level": "INFO"},
}

Path("logging.json").write_text(json.dumps(logging_config, indent=2), encoding="utf-8")

Equivalent CLI:

palfrey myapp.main:app --log-config logging.json

Access log control

palfrey myapp.main:app --no-access-log

Practical production guidance

  • include request IDs/trace IDs in app or middleware logs
  • use structured logs for machine processing
  • keep access and error logs distinct
  • validate log ingestion after each release

Common logging issues

  • wrong path passed to --log-config
  • YAML config without PyYAML
  • level mismatch between root logger and named loggers

Plain-language summary

Good logs are how teams reconstruct what happened during incidents. Treat logging configuration as production-critical code.