redirecting stdout/err to mysql table

John Nagle nagle at animats.com
Sun Nov 23 03:30:02 EST 2008


Aleksandar Radulovic wrote:
> On Tue, Nov 18, 2008 at 7:07 PM, n00b <pyn00b at gmail.com> wrote:
>> 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.

    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



More information about the Python-list mailing list