如何将python日志输出存储到mongoDb中

时间:2012-12-01 06:50:29

标签: python mongodb logging

这是我想做的事情的例子。如果我能够将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

1 个答案:

答案 0 :(得分:2)

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

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