Python日志记录 - 检查日志文件的位置?

时间:2023-01-12 23:44:31

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?

这与记录器的行为/保存方式有何关联?

4 个解决方案

#1


17  

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.basicConfig(filename = log_file_name),其中log_file_name是您希望写入消息的文件的名称(请注意,您必须先执行此操作,然后才能调用日志记录中的任何其他内容) ,然后记录到所有记录器的所有消息(除非稍后进行一些进一步的重新配置)将写入那里。请注意记录器设置的级别;如果内存服务,则info低于默认日志级别,因此您必须在basicConfig的参数中包含level = logging.INFO以及最终在文件中的消息。

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对象,其名称设置为当前模块的名称。这是与日志记录一起使用的常见模式,因为它会导致记录器结构镜像代码的模块结构,这通常会使日志消息在调试时更加有用。

#2


6  

To get the log location of a simple file logger, try

要获取简单文件记录器的日志位置,请尝试

logging.getLoggerClass().root.handlers[0].baseFilename

#3


0  

To find the logfile location, try instantiating your log object in a Python shell in your environment and looking at the value of:

要查找日志文件位置,请尝试在环境中的Python shell中实例化日志对象,并查看以下值:

log.handlers[0].stream

log.handlers [0] .stream

#4


0  

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很难检查。这将获取记录器的所有处理程序的文件名:

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块处理文件名查找失败时发生的异常。

#1


17  

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.basicConfig(filename = log_file_name),其中log_file_name是您希望写入消息的文件的名称(请注意,您必须先执行此操作,然后才能调用日志记录中的任何其他内容) ,然后记录到所有记录器的所有消息(除非稍后进行一些进一步的重新配置)将写入那里。请注意记录器设置的级别;如果内存服务,则info低于默认日志级别,因此您必须在basicConfig的参数中包含level = logging.INFO以及最终在文件中的消息。

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对象,其名称设置为当前模块的名称。这是与日志记录一起使用的常见模式,因为它会导致记录器结构镜像代码的模块结构,这通常会使日志消息在调试时更加有用。

#2


6  

To get the log location of a simple file logger, try

要获取简单文件记录器的日志位置,请尝试

logging.getLoggerClass().root.handlers[0].baseFilename

#3


0  

To find the logfile location, try instantiating your log object in a Python shell in your environment and looking at the value of:

要查找日志文件位置,请尝试在环境中的Python shell中实例化日志对象,并查看以下值:

log.handlers[0].stream

log.handlers [0] .stream

#4


0  

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很难检查。这将获取记录器的所有处理程序的文件名:

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块处理文件名查找失败时发生的异常。