Start of topic | Skip to actions
PythonLoggingHackA 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()
| |||||||