1

这是我想做的例子。如果我能够将 logging.warning() 的输出存储到变量中,我将能够将其存储到 Mongodb

import logging
logging.basicConfig(level=logging.DEBUG,filename='logDemo.log')
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logging.basicConfig(format='%(asctime)s %(message)s')

def divide(a,b):
    try :

        divide = a/b
        return divide
    except ZeroDivisionError:           

    a=logging.warning('Watch out!') # will print a message to the console
    logging.info('I told you so') # will not print anything
    logging.warning('is when this event was logged.')        
    print a


divide(60,0)

我试图将 logging.warning() 的输出存储到一个变量中,但它失败了,有没有办法将日志记录函数输出存储到 mongodb

4

1 回答 1

2

正确的解决方案是编写自己的 MongoDBLoggingHandler 并实现处理 MongoDB 特定保存的 emit() 方法。看

http://docs.python.org/2/library/logging.handlers.html

于 2012-12-01T07:01:23.900 回答