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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:43:56  来源:igfitidea点击:

Python logging - check location of log files?

pythonlinuxfilelogging

提问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 loggingmodule uses handlers attached to loggers to decide how, where, or even if messages ultimately get stored or displayed. You can configure loggingby 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_nameis the name of the file you want messages written to (note that you have to do this before anything else in loggingis 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, infois below the default log level, so you'd have to include level=logging.INFOin the arguments to basicConfigas 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 Loggerobject, 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 Loggerobject 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 logobject 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, Loggersare difficult to inspect. This gets the filenames of all Handlersfor a logger:

很好的问题@zallarak。不幸的是,虽然它们很容易创建,Loggers但很难检查。这将获取Handlersa的所有文件名logger

filenames = []
for handler in logger.handlers:
    try:
        filenames.append(handler.fh.name)
    except:
        pass

The tryblock handles exceptions that occur when the filename lookup fails.

try块处理文件名查找失败时发生的异常。