www.flickr.com
tres frijoles' photos More of tres frijoles' photos
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script -->
You are here: tearsoffire.org > Projects Web > SoftwareDevelopment > PythonLoggingHack r1 - 15 Apr 2009 - 02:43 - ChristopherPepe


Start of topic | Skip to actions

PythonLoggingHack

A legacy server still ran a lot of new software but was 'too important' to be upgraded and was in bad need of retirement. Still I needed logging and the version of python on the box didn't have it. This is not bulletproof but is sufficient for basic logging


try:
    import logging
    logger = logging.getLogger('thisApp')
except ImportError, e:
    #no logging module, hack it
    class Logging(object):
        def __init__(self, level='INFO'):
            import re
            self.level = level
        def log(self, level, msg):
            #NOTE: logger actually logs to /var/log/messages, changing thisApp.log to messages
            # breaks all logging when this script is run as apache.
            logCmd = '/usr/bin/logger -f /var/log/thisApp.log -t thisApp '
            quote_regex = re.compile('[\'\"]+')
            #substitute blank strings for all ' or " i.e. patty o'hara -> patty ohara
            msg = quote_regex.sub('', msg )
            cmd = '%s "thisApp:%s: %s"' % (logCmd, level, msg)
            if mode == 'prod':
                status, output = commands.getstatusoutput(cmd)
            else:
                print cmd
            #status and output are ignored
        def debug(self, msg):
            if self.level == 'DEBUG':
                self.log('DEBUG', str(msg))
        def info(self, msg):
            self.log('INFO', str(msg))
        def warning(self, msg):
            self.log('WARNING', str(msg))
        def error(self, msg):
            self.log('ERROR', str(msg))
        def exception(self, msg):
            self.log('EXCEPTION', str(msg))
    logger = Logging()

-- ChristopherPepe - 15 Apr 2009

Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r1 | More topic actions
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding tearsoffire.org? Send feedback