我正在 python 中使用logging库。我正在使用多个 logger 对象来输出信息。我已经到了输出太多的地步,我需要将其发送到日志文件而不是控制台。 我找不到配置记录器对象的输出流的函数,并且 basicConf 函数似乎无法解决问题。 这就是我所拥有的:
import logging # Debug logging framework
logging.basicConfig(filename='loggggggmeee.txt')
logger = logging.getLogger('simulation')
logger.setLevel(logging.INFO)
#--------------- etc-------------------#
logger.info('This is sent to the console')
有什么想法吗?谢谢!
最佳答案
尝试添加一个文件位置,如下所示,它将解决您的问题:
import logging # Debug logging framework
logging.basicConfig(filename='c:\\loggggggmeee.txt')
logger = logging.getLogger('simulation')
logger.setLevel(logging.INFO)
#--------------- etc-------------------#
logger.info('This is sent to the console')
顺便说一句,没有特定位置的文件将位于模块库中...
我真的建议您阅读Logging HOWTO它非常简单且有用。
还有Cookbook有很多有用的例子。
关于从文档中命名记录器的另一个好技巧:
a good convention to use when naming loggers is to use a module-level logger, in each module which uses logging, named as follows:
logger = logging.getLogger(__name__)
This means that logger names track the package/module hierarchy, and it’s intuitively obvious where events are logged just from the logger name.
关于python - 如何将 python 中 Logger 对象的输出发送到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23536594/