默认配置日志记录时,
logging.basicConfig(filename="/tmp/Hom_Controller.log",level=logging.DEBUG)
现在我在日志中得到如下,
DEBUG:Hom_Controller.log:{'datetime': 'Thu Oct 5 11:27:27 2017', 'message': 'Request finished with error, response code: 401 Unauthorized', 'log_type': 'debug'}
ERROR:Hom_Controller.log:{'datetime': 'Thu Oct 5 11:28:08 2017', 'message': 'Request finished with error, response code: 401 Unauthorized', 'log_type': 'error'}
由于我已经以json格式格式化了我的消息,我不想打印ERROR:Hom_Controller.log:或DEBUG:Hom_Controller.log
答案 0 :(得分:2)
使用logging.formatter格式化日志,使其不包含日志。
import logging
# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
答案 1 :(得分:1)
修复它会根据您的特定需求修改格式,例如
logging.basicConfig(format='[%(asctime)s]:%(message)s',filename="/tmp/Hom_Controller.log", level=logging.DEBUG)
对于格式中的更多选项,参数访问官方页面进行日志记录 link