栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

创建日志记录处理程序以连接到Oracle?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建日志记录处理程序以连接到Oracle?

  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 thissource up before you run the test.Copyright (C) 2001-2009 Vinay Sajip. All Rights Reserved."""import sys, string, time, loggingclass 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")


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624262.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号