Python日志记录-检查日志文件的位置? [英] Python logging - check location of log files?

查看:755
本文介绍了Python日志记录-检查日志文件的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了解Python日志语句存储在哪里的方法是什么?

What is the methodology for knowing where Python log statements are stored?

即如果我这样做:

import logging
log = logging.getLogger(__name__)
log.info('Test')

我在哪里可以找到日志文件?另外,当我打电话时:

Where could I find the logfile? Also, when I call:

logging.getLogger(__name__)

这与记录器的行为/保存方式有某种关系吗?

Is that somehow related to how the logger will behave/save?

推荐答案

logging模块使用附加到记录器的处理程序来决定最终如何存储,显示消息或显示消息.您可以默认配置logging来也写入文件.您确实应该阅读文档,但是如果您调用logging.basicConfig(filename=log_file_name),其中log_file_name是您要写入消息的文件(请注意,您必须先执行此操作,然后再调用logging中的其他任何内容),然后所有记录到所有记录器的消息(除非稍后进行一些进一步的重新配置)都将写入该文件.请注意记录器设置为什么级别;如果有内存可用,则info会低于默认日志级别,因此,您还必须在basicConfig的参数中包括level=logging.INFO,消息才能最终出现在文件中.

The logging module uses handlers attached to loggers to decide how, where, or even if messages ultimately get stored or displayed. You can configure logging by default to write to a file as well. You should really read the docs, but if you call logging.basicConfig(filename=log_file_name) where log_file_name is the name of the file you want messages written to (note that you have to do this before anything else in logging is called at all), then all messages logged to all loggers (unless some further reconfiguration happens later) will be written there. Be aware of what level the logger is set to though; if memory serves, info is below the default log level, so you'd have to include level=logging.INFO in the arguments to basicConfig as well for your message to end up in the file.

对于问题的另一部分,logging.getLogger(some_string)返回一个Logger对象,该对象从根记录器插入到层次结构中的正确位置,名称为some_string的值.调用时不带参数,它将返回根记录器. __name__返回当前模块的名称,因此logging.getLogger(__name__)返回名称设置为当前模块名称的Logger对象.这是logging常用的模式,因为它会使记录器结构镜像您代码的模块结构,这通常使调试时记录消息更加有用.

As to the other part of your question, logging.getLogger(some_string) returns a Logger object, inserted in to the correct position in the hierarchy from the root logger, with the name being the value of some_string. Called with no arguments, it returns the root logger. __name__ returns the name of the current module, so logging.getLogger(__name__) returns a Logger object with the name set to the name of the current module. This is a common pattern used with logging, as it causes the logger structure to mirror your code's module structure, which often makes logging messages much more useful when debugging.

这篇关于Python日志记录-检查日志文件的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆