Wednesday, February 18, 2009

Python: Logging to Email in Pylons

I figured out how to get Pylons to send logging messages via email. Note, this is separate of the Paste mechanism which sends email when there is an uncaught exception. Here's a diff of my .ini file:
 [handlers]
-keys = console
+keys = console, smtp

[formatters]
keys = generic

[logger_root]
level = INFO
-handlers = console
+handlers = console, smtp

[handler_console]
class = StreamHandler
@@ -67,5 +67,11 @@
level = NOTSET
formatter = generic

+[handler_smtp]
+class = logging.handlers.SMTPHandler
+args = ('127.0.0.1', 'myapp@localhost', ['sendto@example.com'], 'myapp log message')
+level = WARN
+formatter = generic
+
Once everything is setup, you can add calls like the following to your controllers:
log.error("This is a test of the logging system.")

2 comments:

Noah Gift said...

One problem with this email alert stuff is that it grows to become a nuisance. I still wonder why someone hasn't written something that collects email on intervals, based on some smart logic, and then makes a bundle.

For example, if you get 30 uncaught exceptions in 1 minute, one email, with 30 dups is better than 30 emails. I get this a lot in Film, they use email like normal people use the television :)

Shannon -jj Behrens said...

To some extent, Gmail does that for me.