redirecting stdout/err to mysql table

N

n00b

greetings,

i need to log to the db directly and wrote a little script to do so.
since i'm pretty new to python,
i was wondering if a) you could review the enclosed code and b)
provide suggestions to harden to code to turn it into a more general,
broadly reusable class.

thank you very much.

import sys
import MySQLdb

class DBLogger(object):
def __init__(self):
self.db_name = 'xxx'
self.db_host = '127.0.0.1'
self.db_table = 'yyy'
self.db_uname = 'root'
self.db_passwd = ''
self.db_port = 3306
self.db = None
self.cur = None
self.sql = 'INSERT INTO %s' %self.db_table + ' VALUES(null, NOW
(), %s)'


def openDb(self):
try:
self.db = MySQLdb.connect(host = self.db_host,
user = self.db_uname,
passwd = self.db_passwd,
db = self.db_name,
)

self.cur = self.db.cursor()
return self.db, self.cur
except Exception, e:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
print e[0], e[1]
sys.exit(1)

def closeDb(self):
self.cur.close()
self.db.close()

def write(self, string):
s = string.strip('\n')
if not s=='':
self.openDb()
self.cur.execute(self.sql, (s))
self.db.commit()
self.closeDb()


dbl = DBLogger()
sys.stdout = dbl
sys.stderr = dbl
#a = 'test string'
#dbl.write(a)

print 'a b c '
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__

thanks again for your time
 
P

pruebauno

greetings,

i need to log to the db directly and wrote a little script to do so.
since i'm pretty new to python,
i was wondering if a) you could review the enclosed code and b)
provide suggestions to harden to code to turn it into a more general,
broadly reusable class.

thank you very much.

import sys
import MySQLdb

class DBLogger(object):
def __init__(self):
self.db_name = 'xxx'
self.db_host = '127.0.0.1'
self.db_table = 'yyy'
self.db_uname = 'root'
self.db_passwd = ''
self.db_port = 3306
self.db = None
self.cur = None
self.sql = 'INSERT INTO %s' %self.db_table + ' VALUES(null, NOW
(), %s)'

def openDb(self):
try:
self.db = MySQLdb.connect(host = self.db_host,
user = self.db_uname,
passwd = self.db_passwd,
db = self.db_name,
)

self.cur = self.db.cursor()
return self.db, self.cur
except Exception, e:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
print e[0], e[1]
sys.exit(1)

def closeDb(self):
self.cur.close()
self.db.close()

def write(self, string):
s = string.strip('\n')
if not s=='':
self.openDb()
self.cur.execute(self.sql, (s))
self.db.commit()
self.closeDb()

dbl = DBLogger()
sys.stdout = dbl
sys.stderr = dbl
#a = 'test string'
#dbl.write(a)

print 'a b c '
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__

thanks again for your time

Looks good to me.
 
A

Aleksandar Radulovic

greetings,

i need to log to the db directly and wrote a little script to do so.
since i'm pretty new to python,
i was wondering if a) you could review the enclosed code and b)
provide suggestions to harden to code to turn it into a more general,
broadly reusable class.

First of all, your code is not quite pythonic and it will also open the
connection to the database *every* time you log. That can be quite
expensive and generally is not advisable. Plus it may block your code.

I suggest checking out (already existing) logging module in Python
distribution and extend the BaseHandler to implement the logging to
a database.

I highly recommend implementing pooling of connections to the DB
(ie. by simply using SqlAlchemy instead of direct MySQLDb module).
 
N

Nebur

I've had a similar requiredment and made a small tool for direct
logging into databases (see:
http://sourceforge.net/projects/rrlog/
)

It's origins are somewhat older than the Python 2.3 standard logging
framework, so it can be used without that (but can also be simply
integrated with it.) It even does some more than you currently need
(log rotation and remote logging).

Ratsberg
 
J

John Nagle

Aleksandar said:
First of all, your code is not quite pythonic and it will also open the
connection to the database *every* time you log. That can be quite
expensive and generally is not advisable. Plus it may block your code.

True. That's not only slow, it can fail simply because the
server is busy. Open the connection once, but commit after each
write.

If most runs produce no log entries, open the connection at
the first write.

Put a an automatic sequence number in each entry. Timestamps
aren't enough to retain order; the unit of measure for timestamps
is only one second.

You might want to put the host name or IP address and the process
ID in each log entry, if you have many programs logging to the same
database. I do logging in MySQL, with multiple clients logging
to the same MySQL database on a separate machine, and this is
necessary to keep the data sorted out.
I suggest checking out (already existing) logging module in Python
distribution and extend the BaseHandler to implement the logging to
a database.

I highly recommend implementing pooling of connections to the DB
(ie. by simply using SqlAlchemy instead of direct MySQLDb module).

John Nagle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top