创建一个连接Oracle的日志处理程序?

Ube*_*per 14 python oracle logging

所以现在我需要创建并实现将用于登录数据库的Python日志记录模块的扩展.基本上我们有几个python应用程序(都在后台运行),当前记录到文本文件的随机混搭.这使得几乎不可能发现某个应用程序是否失败.

给我的问题是将所述日志记录移动到文本文件到oracle DB.这些表已经被定义,并且需要记录的东西,但是现在,我正在寻找添加另一个将记录到数据库的日志记录处理程序.

我使用的是python 2.5.4和cx_Oracle,一般应用程序可以作为服务/守护程序或直接应用程序进行以太运行.

我只是好奇地想知道最好的方法是什么.几个问题:

  1. 如果cx_Oracle发生任何错误,应将这些错误记录在哪里?如果它的下降最好只是让记录器退回到默认文本文件?

  2. 一段时间后,我们开始强制人们使用sys.stderr/stdout.write而不是print,所以最糟糕的情况是我们不会遇到任何关于print被弃用的问题.有没有办法无缝地将所有成千上万的sys.std调用直接传送到记录器中,并让记录器拾取松弛?

  3. 在每条记录的消息之后,脚本是否应自动执行提交?(每秒会有几十个.)

  4. 为日志记录系统实现新处理程序的最佳方法是什么?从基本的Handler类继承似乎是最简单的.

任何想法/建议都会很棒.

Vin*_*jip 20

  1. 如果cx_Oracle发生错误,最好将这些错误记录到文本文件中.
  2. 您可以尝试将sys.stdout和sys.stderr重定向到类似文件的对象,这些对象将写入它们的任何内容记录到记录器中.
  3. 我猜你确实想在每个事件之后提交,除非你有充分的理由不这样做.或者,您可以缓冲多个事件,并且每隔一段时间就将它们全部写入一个事务中.
  4. 下面是一个使用mx.ODBC的例子,你可以将它调整到cx_Oracle而不会有太多麻烦.我认为这意味着符合Python DB-API 2.0标准.

独立的Python日志记录分发(在将日志记录添加到Python之前)位于http://www.red-dove.com/python_logging.html,尽管Python中的日志记录包更新,但独立发行版包含一个测试目录,它有很多有用的派生处理程序类的例子.

#!/usr/bin/env python
#
# Copyright 2001-2009 by Vinay Sajip. All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of Vinay Sajip
# not be used in advertising or publicity pertaining to distribution
# of the software without specific, written prior permission.
# VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# This file is part of the standalone Python logging distribution. See
# http://www.red-dove.com/python_logging.html
#
"""
A test harness for the logging module. An example handler - DBHandler -
which writes to an Python DB API 2.0 data source. You'll need to set this
source up before you run the test.

Copyright (C) 2001-2009 Vinay Sajip. All Rights Reserved.
"""
import sys, string, time, logging

class DBHandler(logging.Handler):
    def __init__(self, dsn, uid='', pwd=''):
        logging.Handler.__init__(self)
        import mx.ODBC.Windows
        self.dsn = dsn
        self.uid = uid
        self.pwd = pwd
        self.conn = mx.ODBC.Windows.connect(self.dsn, self.uid, self.pwd)
        self.SQL = """INSERT INTO Events (
                        Created,
                        RelativeCreated,
                        Name,
                        LogLevel,
                        LevelText,
                        Message,
                        Filename,
                        Pathname,
                        Lineno,
                        Milliseconds,
                        Exception,
                        Thread
                   )
                   VALUES (
                        %(dbtime)s,
                        %(relativeCreated)d,
                        '%(name)s',
                        %(levelno)d,
                        '%(levelname)s',
                        '%(message)s',
                        '%(filename)s',
                        '%(pathname)s',
                        %(lineno)d,
                        %(msecs)d,
                        '%(exc_text)s',
                        '%(thread)s'
                   );
                   """
        self.cursor = self.conn.cursor()

    def formatDBTime(self, record):
        record.dbtime = time.strftime("#%m/%d/%Y#", time.localtime(record.created))

    def emit(self, record):
        try:
            #use default formatting
            self.format(record)
            #now set the database time up
            self.formatDBTime(record)
            if record.exc_info:
                record.exc_text = logging._defaultFormatter.formatException(record.exc_info)
            else:
                record.exc_text = ""
            sql = self.SQL % record.__dict__
            self.cursor.execute(sql)
            self.conn.commit()
        except:
            import traceback
            ei = sys.exc_info()
            traceback.print_exception(ei[0], ei[1], ei[2], None, sys.stderr)
            del ei

    def close(self):
        self.cursor.close()
        self.conn.close()
        logging.Handler.close(self)

dh = DBHandler('Logging')
logger = logging.getLogger("")
logger.setLevel(logging.DEBUG)
logger.addHandler(dh)
logger.info("Jackdaws love my big %s of %s", "sphinx", "quartz")
logger.debug("Pack my %s with five dozen %s", "box", "liquor jugs")
try:
    import math
    math.exp(1000)
except:
    logger.exception("Problem with %s", "math.exp")
Run Code Online (Sandbox Code Playgroud)