Linux Python 日志记录 - 检查日志文件的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14406347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python logging - check location of log files?
提问by zallarak
What is the methodology for knowing where Python log statements are stored?
了解 Python 日志语句存储位置的方法是什么?
i.e. if i do:
即如果我这样做:
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?
这是否与记录器的行为/保存方式有关?
采纳答案by Silas Ray
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
模块使用附加到记录器的处理程序来决定消息最终如何、在哪里甚至是否被存储或显示。您也可以logging
默认配置为写入文件。您应该真正阅读文档,但是如果您调用logging.basicConfig(filename=log_file_name)
wherelog_file_name
是您想要写入消息的文件的名称(请注意,您必须在logging
调用其他任何内容之前执行此操作),然后将所有消息记录到所有记录器(除非稍后会进行一些进一步的重新配置)将在那里写入。请注意记录器设置的级别;如果内存有效,info
则低于默认日志级别,因此您必须level=logging.INFO
在参数中包含tobasicConfig
以及您的消息最终出现在文件中。
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.
至于问题的另一部分,logging.getLogger(some_string)
返回一个Logger
对象,从根记录器插入到层次结构中的正确位置,名称为some_string
. 不带参数调用,它返回根记录器。 __name__
返回当前模块的名称,因此logging.getLogger(__name__)
返回Logger
名称设置为当前模块名称的对象。这是与 一起使用的常见模式logging
,因为它会导致记录器结构反映您代码的模块结构,这通常使日志消息在调试时更加有用。
回答by Paul Bissex
To find the logfile location, try instantiating your log
object in a Python shell in your environment and looking at the value of:
要查找日志文件位置,请尝试log
在您的环境中的 Python shell 中实例化您的对象并查看以下值:
log.handlers[0].stream
log.handlers[0].stream
回答by Taran
To get the log location of a simple file logger, try
要获取简单文件记录器的日志位置,请尝试
logging.getLoggerClass().root.handlers[0].baseFilename
回答by Arthur
Excellent question @zallarak. Unfortunately, while they're easy to create, Loggers
are difficult to inspect. This gets the filenames of all Handlers
for a logger
:
很好的问题@zallarak。不幸的是,虽然它们很容易创建,Loggers
但很难检查。这将获取Handlers
a的所有文件名logger
:
filenames = []
for handler in logger.handlers:
try:
filenames.append(handler.fh.name)
except:
pass
The try
block handles exceptions that occur when the filename lookup fails.
该try
块处理文件名查找失败时发生的异常。