记录器为python。不记录自定义日志条目

时间:2015-04-01 08:43:51

标签: python logging

对于python项目,我有一个简单的记录器,如下所示:

#!flask/bin/python
from flask import Flask,request, Response
import os.path
import json
import sys
import logging
import logging.handlers
from dbMongoManager import saveToMongo
from dbSQLManager import saveToMYSQL
from FailedRequest import FailedRequest
from JSONValidation import validateJSON



#create logger
logger = logging.getLogger('werkzeug')
#defines logger file and max size
handler = logging.handlers.RotatingFileHandler('request.log',maxBytes=5000000)
#define logger format
formatter = logging.Formatter("test %(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
handler.setFormatter(formatter)
#add loggerhandler to applications
logger.addHandler(handler)
app.logger.addHandler(handler)

logger.info("Logger started")
...
#app implementation here
.

创建了文件request.log,但条目“logger started”无处可寻。 尽管来自flask的事件(服务器启动/停止,请求响应)也会被记录。 我的所有自定义日志(logger.info,logger.warning等)都没有出现在文件中。哪里可能是我的错? 我见过的所有例子显然都适用于这种实现。

重新加载脚本后的日志内容:

2015-04-01 08:54:52,479 [MainThread  ] [INFO ]   * Detected change in '/usr/local/bin/restService/restService.py', reloading
2015-04-01 08:55:08,393 [Thread-1    ] [INFO ]  194.209.7.10 - - [01/Apr/2015 08:55:08] "POST / HTTP/1.1" 400 -
2015-04-01 08:55:08,559 [Thread-1    ] [INFO ]  194.209.7.10 - - [01/Apr/2015 08:55:08] "POST / HTTP/1.1" 400 -

1 个答案:

答案 0 :(得分:1)

这应该有效

import logging
import logging.handlers

# create logger
root_logger = logging.getLogger()
root_logger.setLevel(0)
# defines logger file and max size
handler = logging.handlers.RotatingFileHandler('request.log', maxBytes=5000000)
# define logger format
formatter = logging.Formatter("test %(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
handler.setFormatter(formatter)
handler.setLevel(0)
# add loggerhandler to applications
root_logger.addHandler(handler)

logger = logging.getLogger('app')

logger.info("Logger started")