Package turbomail :: Package extensions :: Module maillog
[hide private]
[frames] | no frames]

Source Code for Module turbomail.extensions.maillog

 1  # encoding: utf-8 
 2   
 3  import logging 
 4   
 5  from turbomail import interface, Message 
 6   
 7   
8 -class TurboMailHandler(logging.Handler):
9 """A class which sends records out via e-mail. 10 11 The built-in SMTPHandler is insufficient for most jobs, restricting 12 developers to unauthenticated communication over the standard port, 13 with little control over additional delivery options. 14 15 This handler should be configured using the same configuration 16 directives that TurboMail itself understands. If you do not specify 17 `mail.on` in the configuration, this handler will attempt to use 18 a previously configured TurboMail environment. 19 20 Be sure that TurboMail is running before messages are emitted using 21 this handler, and be careful how many notifications get sent. 22 23 It is suggested to use background delivery using the 'demand' manager. 24 25 Configuration options for this handler are as follows:: 26 27 * mail.handler.priorities = [True/False] 28 Set message priority using the following formula: 29 record.levelno / 10 - 3 30 31 * 32 """ 33
34 - def __init__(self, *args, **config):
35 """Initialize the instance, optionally configuring TurboMail itself. 36 37 If no additional arguments are supplied to the handler, re-use any 38 existing running TurboMail configuration. 39 40 To get around limitations of the INI parser, you can pass in a tuple 41 of name, value pairs to populate the dictionary. (Use `{}` dict 42 notation in produciton, though.) 43 """ 44 45 logging.Handler.__init__(self) 46 47 self.config = dict() 48 49 if args: 50 config.update(dict(zip(*[iter(args)]*2))) 51 52 if config and 'mail.on' in config: 53 # Initilize TurboMail using the configuration directives passed 54 # to this handler, generally from an INI configuration file. 55 turbomail.interface.start(config) 56 return 57 58 # If we get a configuration that doesn't explicitly start TurboMail 59 # we use the configuration to populate the Message instance. 60 self.config = config
61
62 - def emit(self, record):
63 """Emit a record.""" 64 65 try: 66 message = Message() 67 68 if self.config: 69 for i, j in self.config.iteritems(): 70 if i.startswith('mail.message'): 71 i = i[13:] 72 setattr(message, i, j) 73 74 message.plain = self.format(record) 75 message.send() 76 77 except (KeyboardInterrupt, SystemExit): 78 raise 79 80 except: 81 self.handleError(record)
82 83 84 logging.TurboMailHandler = TurboMailHandler 85