Python自定义日志记录

时间:2014-09-15 13:32:03

标签: python logging flask

我有一个webapp(Flask),我需要启用一些日志记录。我需要使用以下列登录到mysql表:

id, timestamp, userid, orderid, service, msg, response

我想使用python的std日志记录。 示例用法是:

special_logger = logging.getSpecialLogger() # instead of logging.getLogger()

special_logger.setLevel(level=logging.DEBUG)
special_logger.addHandler(myMysqlHandler)
special_logger.debug(current_uid, myorderid, 'SERVICE1', 'Exception happened', serviceResponse)

我怎么能做到这一点?

1 个答案:

答案 0 :(得分:1)

只需使用普通记录器和数据库处理程序,如this answer中所述。虽然我的另一个答案并没有特别针对MySQL,但它使用的是DB-API,因此应该适用于符合DB-API 2.0的MySQL Python驱动程序。

更新:您可以按照文档here将额外数据传递到日志。