Welcome, guest | Sign In | My Account | Store | Cart

This is a handler for the standard logging module that sends notifications to the Amazon Simple Notification Service.

You can use it like so:

logging.config.dictConfig({
   
'version': 1,
   
'disable_existing_loggers': False,
   
'formatters': {
       
'verbose': {
           
'format': '%(levelname)s %(asctime)s %(module)s %(process)d '
                     
'%(thread)d %(message)s',
       
},
       
'simple': {
           
'format': '%(levelname)s %(message)s',
       
},
   
},
   
'handlers': {
       
'sns': {
           
'level': 'INFO',
           
'class': 'SNSHandler',
           
'formatter': 'verbose',
           
'topic_arn': 'YOUR SNS TOPIC ARN',
       
},
   
},
   
'loggers': {
       
'YOUR MODULE': {
           
'handlers': ['sns'],
           
'level': 'INFO',
           
'propagate': True,
       
},
   
},
}
Python, 19 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import logging
import boto.sns


class SNSHandler(logging.Handler):

    def __init__(self, topic_arn, *args, **kwargs):
        super(SnsHandler, self).__init__(*args, **kwargs)

        region_name = topic_arn.split(':')[3]
        self.sns_connection = boto.sns.connect_to_region(region_name)
        self.topic_arn = topic_arn

    def emit(self, record):
        subject = u'{}:{}'.format(record.name, record.levelname)
        self.sns_connection.publish(
            self.topic_arn,
            self.format(record),
            subject=subject.encode('ascii', errors='ignore')[:99])