Centralizing logs
Python comes with the logging
package, which lets you stream logs to a variety of places including standard out, rotating log files, syslog, or a TCP or UDP socket.
There's even an SMTP backend. In the following example, the email_errors
decorator will send an email every time an exception is happening in the decorated function. Note that the handler is doing a telnet session with the SMTP server to send the email, so if there's any issue during that session, you might get a second exception when the logger.exception()
function is called:
import logging from logging.handlers import SMTPHandler host = "smtp.example.com", 25 handler = SMTPHandler(mailhost=host, fromaddr="tarek@ziade.org", toaddrs=["tarek@ziade.org"], subject="Service Exception") logger = logging.getLogger('theapp') logger.setLevel(logging.INFO) logger.addHandler(handler) def email_errors(func): def _email_errors...