Python 日志记录配置文件
我在尝试将日志记录到我的 python 项目时似乎遇到了一些问题。
我只是尝试模仿以下配置:
但是,我不想在代码内部执行此操作,而是希望将其放在配置文件中。
以下是我的配置文件:
[loggers]
keys=root
[logger_root]
handlers=screen,file
[formatters]
keys=simple,complex
[formatter_simple]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
[formatter_complex]
format=%(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s
[handlers]
keys=file,screen
[handler_file]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=5
formatter=complex
level=DEBUG
args=('logs/testSuite.log',)
[handler_screen]
class=StreamHandler
formatter=simple
level=INFO
args=(sys.stdout,)
问题是我的屏幕输出如下所示:
2010-12-14 11:39:04,066 - 根 - 警告 - 3
2010-12-14 11:39:04,066 - 根 - 错误 - 4
2010-12-14 11:39:04,066 - root - CRITICAL - 5
我的文件已输出,但看起来与上面相同(尽管包含额外信息)。但是,调试和信息级别都不会输出到其中任何一个。
我使用的是 Python 2.7
这是我显示失败的简单示例:
import os
import sys
import logging
import logging.config
sys.path.append(os.path.realpath("shared/"))
sys.path.append(os.path.realpath("tests/"))
class Main(object):
@staticmethod
def main():
logging.config.fileConfig("logging.conf")
logging.debug("1")
logging.info("2")
logging.warn("3")
logging.error("4")
logging.critical("5")
if __name__ == "__main__":
Main.main()
I seem to be having some issues while attempting to implement logging into my python project.
I'm simply attempting to mimic the following configuration:
Python Logging to Multiple Destinations
However instead of doing this inside of code, I'd like to have it in a configuration file.
Below is my config file:
[loggers]
keys=root
[logger_root]
handlers=screen,file
[formatters]
keys=simple,complex
[formatter_simple]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
[formatter_complex]
format=%(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s
[handlers]
keys=file,screen
[handler_file]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=5
formatter=complex
level=DEBUG
args=('logs/testSuite.log',)
[handler_screen]
class=StreamHandler
formatter=simple
level=INFO
args=(sys.stdout,)
The problem is that my screen output looks like:
2010-12-14 11:39:04,066 - root - WARNING - 3
2010-12-14 11:39:04,066 - root - ERROR - 4
2010-12-14 11:39:04,066 - root - CRITICAL - 5
My file is output, but looks the same as above (although with the extra information included). However the debug and info levels are not output to either.
I am on Python 2.7
Here is my simple example showing failure:
import os
import sys
import logging
import logging.config
sys.path.append(os.path.realpath("shared/"))
sys.path.append(os.path.realpath("tests/"))
class Main(object):
@staticmethod
def main():
logging.config.fileConfig("logging.conf")
logging.debug("1")
logging.info("2")
logging.warn("3")
logging.error("4")
logging.critical("5")
if __name__ == "__main__":
Main.main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来您已经为处理程序设置了级别,但没有为记录器设置了级别。记录器的级别会在每条消息到达其处理程序之前对其进行过滤,默认值为 WARNING 及以上(如您所见)。将根记录器的级别设置为
NOTSET
,以及将其设置为DEBUG
(或您希望记录的最低级别)应该可以解决您的问题。It looks like you've set the levels for your handlers, but not your logger. The logger's level filters every message before it can reach its handlers and the default is
WARNING
and above (as you can see). Setting the root logger's level toNOTSET
as you have, as well as setting it toDEBUG
(or whatever is the lowest level you wish to log) should solve your issue.将以下行添加到根记录器可以解决我的问题:
Adding the following line to the root logger took care of my problem:
只需在
[logger_root]
中添加日志级别即可。这是有效的。Just add log level in
[logger_root]
. It is worked.写入终端和文件的简单方法如下:
然后在代码中使用它,如下所示:
A simple approach to both write to terminal and file would be as following:
And then use it in your code like this:
我认为你应该将disable_existing_loggers添加为false。
I think you should add the disable_existing_loggers to false.