I would think that someone would have already implemented a SQLite back-end for logging stuff in Python using the built-in ‘logging’ module, but I couldn’t find anything, so I just made one. Not implemented yet, but planned: logging to different tables based on logger name or possibly something else.
Here is sqlitehandler.py:
import logging
import sqlite3
import time
class SQLiteHandler(logging.Handler): # Inherit from logging.Handler
def __init__(self, filename):
global db
# run the regular Handler __init__
logging.Handler.__init__(self)
# Our custom argument
db = sqlite3.connect(filename) # might need to use self.filename
db.execute("CREATE TABLE IF NOT EXISTS debug(date text, loggername text, srclineno integer, func text, level text, msg text)")
db.commit()
def emit(self, record):
# record.message is the log message
thisdate = time.time()
print record.getMessage()
db.execute('INSERT INTO debug(date, loggername, srclineno, func, level, msg) VALUES(?,?,?,?,?,?)', (thisdate, record.name, record.lineno, record.funcName, record.levelname, record.msg))
db.commit()
And here is a sample implementation with some other stuff from my bitcoin trading bot:
import urllib2
import json
import logging
import logging.handlers
import sqlitehandler
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('bitbot.conf')
# ---- INIT ----
def getRate():
result = json.load(urllib2.urlopen(parser.get('mtgox', 'ApiTickerUrl')))
return result['ticker']['buy']
print parser._sections['mtgox']
print getRate()
# ---- MAIN ----
# Create a logging object (after configuring logging)
logger = logging.getLogger('someLoggerNameLikeDebugOrWhatever')
# optionally, we could define a logger like so:
#myHandler = sqlitehandler.SQLiteHandler('debugLog.sqlite')
# set the lowest level of event to log. If undefined, defaults to logging.WARNING
# valid levels: 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'
logger.setLevel(logging.DEBUG)
# Add our custom logging handler to logger
# The arg we're passing is the name of the SQLite database file to use
logger.addHandler(sqlitehandler.SQLiteHandler('debugLog.sqlite'))
# test out the different log levels
logger.debug('Test 1')
logger.info('Test 2')
logger.warning('Test 3')
logger.error('Test 4')
logger.critical('Test 5')
Lastly, here is bitbot.conf which you’ll need to run the without modification:
[mtgox]
email: [email protected]
pass: somepassword
ApiTickerUrl: https://mtgox.com/code/data/ticker.php
ApiGetFundsUrl: https://mtgox.com/code/getFunds.php
ApiSellUrl: https://mtgox.com/code/sellBTC.php
Have fun, and check out bitbot for how I’m actually using this in a project. I’ll try to update this post when I improve this handler to do more cool stuff, but I might forget.
Clicky Kitty:
