我正在尝试从简单的打印切换到正确的日志记录。
我想使用两个不同的记录器,第一个在屏幕上显示信息,另一个在文件中显示。
我的问题是,即使我将处理程序级别设置为DEBUG,消息也只显示在WARNING中。
以下是我的代码示例:
def setup_logger(self):
"""
Configures our logger to save error messages
"""
# create logger for 'facemovie'
self.my_logger = logging.getLogger('FileLog')
# create file handler which logs even debug messages
fh = logging.FileHandler('log/fm.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
self.console_logger = logging.getLogger('ConsoleLog')
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
#ch.setFormatter(formatter)
##Start logging in file
self.my_logger.info("######")
# add the handlers to the logger
self.my_logger.addHandler(fh)
self.console_logger.addHandler(ch)
# DEBUG
self.console_logger.info("MFCKR")
self.console_logger.debug("MFCKR")
self.console_logger.warning("MFCKR")
self.console_logger.error("MFCKR")
self.console_logger.critical("MFCKR")
self.my_logger.info("MFCKR")
self.my_logger.debug("MFCKR")
self.my_logger.warning("MFCKR")
self.my_logger.error("MFCKR")
self.my_logger.critical("MFCKR")
输出:
[jll@jll-VirtualBox:~/Documents/FaceMovie]$ python Facemoviefier.py -i data/inputs/samples -o data/
Selected profile is : frontal_face
MFCKR
MFCKR
MFCKR
Starting Application !
Output #0, avi, to 'data/output.avi':
Stream #0.0: Video: mpeg4, yuv420p, 652x498, q=2-31, 20780 kb/s, 90k tbn, 5 tbc
FaceMovie exited successfully!
[jll@jll-VirtualBox:~/Documents/FaceMovie]$ cat log/fm.log
2012-07-15 22:23:24,303 - FileLog - WARNING - MFCKR
2012-07-15 22:23:24,303 - FileLog - ERROR - MFCKR
2012-07-15 22:23:24,303 - FileLog - CRITICAL - MFCKR
我更新了文档并在网上搜索了类似的错误,但找不到任何错误。
您是否了解记录器未显示DEBUG和INFO的原因?
Thx!
答案 0 :(得分:2)