Custom handler for Python's logging module

Posted by mikael on 2009-08-30 00:43, section: Software

After spending some time messing with Python's logging module at work I decided to create a new handler for it, this was mainly to clean up my code a bit (and so I wouldn't have to manually call a custom module for sending SMS). Since there appears to be little info online about how to easily create a new handler I've decided to share my approach.

We start off with the actual handler, or a simplified version anyway:

# smshandler.py
import logging
import sendsms # our internal module for sending SMS

class SMSHandler(logging.Handler): # Inherit from logging.Handler
        def __init__(self, phonenumber):
                # run the regular Handler __init__
                logging.Handler.__init__(self)
                # Our custom argument
                self.phonenumber = phonenumber
        def emit(self, record):
                # record.message is the log message
                sendsms.send(self.phonenumber, record.message)

Now, the above code is pretty useless on its own, we need to use it:

# handlertest.py
import logging
import logging.handlers
import smshandler

# Create a logging object (after configuring logging)
logging.basicConfig(filename=LOG_FILENAME,level=logging.WARNING)
logger = logging.getLogger()

# A little trickery because, at least for me, directly creating
# an SMSHandler object didn't work
logging.handlers.SMSHandler = smshandler.SMSHandler

# create the handler object
testHandler = logging.handlers.SMSHandler('46701234567')
# Configure the handler to only send SMS for critical errors
testHandler.setLevel(logging.CRITICAL)

# and finally we add the handler to the logging object
logger.addHandler(testHandler)

# And finally a test
logger.debug('Test 1')
logger.info('Test 2')
logger.warning('Test 3')
logger.error('Test 4')
logger.critical('Test 5')

If we were to run this script then the only message that would be sent as an SMS would be the last one, 'Test 5'. And this code is simple enough that I doubt anyone using the logging module would have little trouble adapting it to whatever non-standard method of logging they want to employ.

Safari saturation bug

Posted by mikael on 2009-07-05 19:27, section: Software

Not that long ago I upgraded to Safari 4.0.1 and all was well, lots of new neatness although the fact that Apple once again managed to change the keyboard shortcuts for the worse annoyed me a bit. Then I discovered a neat little bug...

Example of the effect

It seems that when scrolling the saturation of the colors on the rendered page increases a little for every time you scroll, as long as you scroll it out of view then when you scroll back to the same part of the page it will render normally but if you scroll back and forth with some part of the page always visible then slowly you'll start to get the above look.

I'm a bit surprised this somehow managed to make it into production code, especially since I've never seen this kind of behaviour in Safari before.

Apparently resetting your monitor color profile settings will fix this bug for some people but that just makes me even more confused, why does this bug only affect Safari? And why did it pop up now?

Update 2009-10-12: It seems that this problem only occurs for me when using an external monitor, when I'm just using the regular 24" display on my iMac there are no issues.

Vim and slow connections

Posted by mikael on 2009-03-26 20:07, section: Hacks

Today I was setting up a VPN connection between my work laptop and the office to enable me to work from home and ran into a bit of a problem, whenever I tried opening a file at the office in Vim it would be unbearably slow, so slow in fact that getting any work done at all became practically impossible.

After some careful googling and I finally found a solution to my problem and I thought I'd share it here, it really boils down to a couple of simple lines in your .vimrc file.

set dir=C:\\tmp
let loaded_matchparen = 1

The first line makes Vim create swap files locally instead of on the remote server. The second line disables parenthesis matching which for some reason generates a lot of disk I/O, without it Vim becomes quite snappy.

iPhone 3G unpacking

Posted by mikael on 2008-07-11 12:54, section: Hardware

Well, today I went to the local Telia store around 7 in the morning and placed myself in line, I was number twelve in line and around thirty minutes after the store opened I was finally let in and got to wait another twenty minutes before I was able to get my hands on an iPhone.

After hastily declining to let them activate my phone and new SIM card I rushed home to unpack and take pictures. So without further ado I now give you an image of the unpacking of a Swedish iPhone 3G.

iPhone 3G unpacking

Long time and no updates

Posted by mikael on 2008-05-20 15:37, section: Index Apple Wireless Keyboard

Well, I've been neglecting this website for quite a while, the fact that the last post before this one is me wishing you merry christmas should tell you that quite clearly. As always I've been working on stuff behind the scenes, new design (that I was disappointed with and scrapped) and some new features for the CMS (which are not finished yet) are both things that I have been working on, but until next time I give you a panorama photo and hope anyone reading this has a great summer, just in case I don't post anything until this fall.

Merry Christmas

Posted by mikael on 2007-12-24 12:01, section: Index

Or to be more correct, God jul!

Not a lot of updates recently although I've considered writing a short review of some hardware I bought recently, stay tuned.