创建日志处理程序以连接到 Oracle?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/935930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a logging handler to connect to Oracle?
提问by UberJumper
So right now i need to create and implement an extension of the Python logging module that will be used to log to our database. Basically we have several python applications(that all run in the background) that currently log to a random mishmash of text files. Which makes it almost impossible to find out if a certain application failed or not.
所以现在我需要创建并实现 Python 日志模块的扩展,该模块将用于记录到我们的数据库。基本上,我们有几个 Python 应用程序(都在后台运行),它们当前登录到随机混杂的文本文件中。这使得几乎不可能找出某个应用程序是否失败。
The problem given to me is to move said logging to text files to an oracle DB. The tables have already been defined, and where things need to be logged to but right now, im looking at adding another logging handler that will log to the DB.
给我的问题是将所述日志记录到文本文件移动到 oracle DB。表已经定义,并且需要将事情记录到哪里,但现在,我正在考虑添加另一个将记录到数据库的日志处理程序。
I am using python 2.5.4 and cx_Oracle and the applications in general can be ether run as a service/daemon or a straight application.
我正在使用 python 2.5.4 和 cx_Oracle,并且应用程序通常可以作为服务/守护程序或直接应用程序运行。
I'm just mainly curious about what would be the best possible way to go about this. Few questions:
我只是主要想知道什么是最好的方法来解决这个问题。几个问题:
If any errors occur with cx_Oracle, where should these errors be logged to? If its down would it be best to just go and have the logger retreat to the default text file?
Awhile back we started enforcing that people use sys.stderr/stdout.write instead of print, so worst case scenario we wouldn't run into any issues with print becoming deprecated. Is there a way to seamlessly make all of the thousands of sys.std calls be piped directly into the logger, and have the logger pickup the slack?
After every logged message, should the script automatically do a commit? (there's going to be several dozen a second.)
What is the best way to implement a new handler for the logging system? Inheriting from the basic Handler class seems to be easiest.
如果 cx_Oracle 出现任何错误,应将这些错误记录到何处?如果它关闭,最好只是让记录器退回到默认文本文件?
不久前,我们开始强制人们使用 sys.stderr/stdout.write 而不是打印,因此在最坏的情况下,我们不会遇到打印被弃用的任何问题。有没有一种方法可以无缝地将所有数千个 sys.std 调用直接通过管道传输到记录器中,并让记录器接收空闲时间?
在每条记录的消息之后,脚本是否应该自动进行提交?(每秒会有几十个。)
为日志记录系统实现新处理程序的最佳方法是什么?从基本的 Handler 类继承似乎是最简单的。
Any ideas / suggestions would be great.
任何想法/建议都会很棒。
回答by Vinay Sajip
- If errors occur with cx_Oracle, it's probably best to log these to a text file.
- You could try redirecting sys.stdout and sys.stderr to file-like objects which log whatever's written to them to a logger.
- I would guess you do want to commit after each event, unless you have strong reasons for not doing this. Alternatively, you can buffer several events and write them all in a single transaction every so often.
- Below is an example which uses mx.ODBC, you can probably adapt this to cx_Oracle without too much trouble. It's meant to be Python DB-API 2.0 compliant, I think.
- 如果 cx_Oracle 发生错误,最好将这些记录到文本文件中。
- 您可以尝试将 sys.stdout 和 sys.stderr 重定向到类似文件的对象,这些对象将写入它们的任何内容记录到记录器中。
- 我猜你确实想在每个事件之后提交,除非你有充分的理由不这样做。或者,您可以缓冲多个事件并每隔一段时间将它们全部写入单个事务中。
- 下面是一个使用 mx.ODBC 的例子,你可以很容易地将它适配到 cx_Oracle。我认为这意味着符合 Python DB-API 2.0。
The standalone Python logging distribution (before logging was added to Python) is at http://www.red-dove.com/python_logging.htmland although the logging package in Python is much more up to date, the standalone distribution contains a test directory which has a lot of useful examples of derived handler classes.
独立的 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("Hymandaws 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")