redirecting stdout/err to mysql table

Discussion in 'Python' started by n00b, Nov 18, 2008.

  1. n00b

    n00b Guest

    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
    n00b, Nov 18, 2008
    #1
    1. Advertising

  2. n00b

    Guest

    On Nov 18, 2:07 pm, n00b <> 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.
    >
    > 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.
    , Nov 19, 2008
    #2
    1. Advertising

  3. On Tue, Nov 18, 2008 at 7:07 PM, n00b <> 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.

    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).



    --
    a lex 13 x
    http://www.a13x.info
    Aleksandar Radulovic, Nov 19, 2008
    #3
  4. n00b

    Nebur Guest

    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
    Nebur, Nov 20, 2008
    #4
  5. n00b

    John Nagle Guest

    Aleksandar Radulovic wrote:
    > On Tue, Nov 18, 2008 at 7:07 PM, n00b <> 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
    John Nagle, Nov 23, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Douard
    Replies:
    4
    Views:
    805
    David Bolen
    Feb 3, 2005
  2. n00b
    Replies:
    1
    Views:
    230
  3. Terry Cooper
    Replies:
    7
    Views:
    420
    Janos Sebok
    Jun 9, 2009
  4. Replies:
    1
    Views:
    557
  5. Replies:
    1
    Views:
    419
    Roedy Green
    Aug 29, 2012
Loading...

Share This Page