79699312

Date: 2025-07-12 13:37:33
Score: 0.5
Natty:
Report link

To log messages to both a file and stdout using Python’s logging module, you just need to add a second handler—for example, a StreamHandler for stdout—in addition to your existing RotatingFileHandler.

# Stream handler (stdout)
stream_handler = logging.StreamHandler(sys.stdout)
stream_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
stream_handler.setFormatter(stream_formatter)

# add stream handler to logger
logger.addHandler(stream_handler)

For a deeper guide on setting up logging the right way in Python web apps (including best practices, gotchas, and advanced tips), check out this article:
👉 The Right Way to Maintain Logs in Python Web Apps (Part 1)

Reasons:
  • Blacklisted phrase (1): this article
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: WAEX