Skip to content

Instantly share code, notes, and snippets.

@brccabral
Created February 6, 2026 01:20
Show Gist options
  • Select an option

  • Save brccabral/e8211965eb0a157644bff18ae932f425 to your computer and use it in GitHub Desktop.

Select an option

Save brccabral/e8211965eb0a157644bff18ae932f425 to your computer and use it in GitHub Desktop.
Python log to both file and stderr

Python log to both file and stderr

import sys
import logging
from logging.handlers import RotatingFileHandler

stderr_handler = logging.StreamHandler(sys.stderr)
rotate_handler = RotatingFileHandler("logs/rotate.log", maxBytes=5000000, backupCount=10)

logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(name)s.%(funcName)s:%(lineno)d - %(message)s",
    datefmt="%Y-%m-%dT%H:%M:%S",
    level=logging.INFO,
    handlers=[rotate_handler, stderr_handler],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment