#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4

import logging
import logging.handlers
import random          
import sys

LOG_CHANNEL = "rapidsms"
LOG_SIZE    = 8192 # 8192 bytes = 64 kb
LOG_BACKUPS = 256 # number of logs to keep around
LOG_FORMAT  = "%(asctime)s %(levelname)s [%(component)s]: %(message)s"
LOG_LEVEL   = "debug"
LOG_FILE    = "/tmp/rapidsms.log"

class Logger (object):
    """A simple wrapper around the standard python logger."""
    def __init__(self, level=LOG_LEVEL, file=LOG_FILE):
        # set up a specific logger with our desired output level
        self.log = logging.getLogger(LOG_CHANNEL)
        self.log.setLevel(getattr(logging, level.upper()))
        formatter = logging.Formatter(LOG_FORMAT)
        try:
            # add the log message handler and formatter to the log
            file_handler = logging.handlers.RotatingFileHandler(
                file, maxBytes=LOG_SIZE, backupCount=LOG_BACKUPS)
            file_handler.setFormatter(formatter)
            self.log.addHandler(file_handler)
        except Exception, e:
            print >>sys.stderr, "Error starting log file %s: %s" % (file,e)
        stderr_handler = logging.StreamHandler()
        stderr_handler.setFormatter(formatter)
        self.log.addHandler(stderr_handler)
        
    def write(self, sender, level, msg, *args):
        level = getattr(logging, level.upper())
        kwargs = {"extra":{"component":sender.name}}
        self.log.log(level, msg, *args, **kwargs)

@@ -501,16 +501,71 @@
 LOG_FILE
+,%0A                       format=LOG_FORMAT, stderr=True
 ):%0A     
@@ -771,26 +771,22 @@
 rmatter(
-LOG_FORMAT
+format
 )%0A      
@@ -1190,16 +1190,65 @@
 file,e)%0A
+            stderr = True%0A        if stderr:%0A    
         
@@ -1288,32 +1288,36 @@
 ndler()%0A        
+ 
+   
 stderr_handler.s
@@ -1331,32 +1331,36 @@
 tter(formatter)%0A
+    
         self.log

Add format and stderr options to Logger() to make it more configurable and easier to test.