FileHandler没有将输出发送到我想要的任何位置 [英] FileHandler not sending output to either location I want
问题描述
第一次使用日志记录模块.我确实有两个问题:
First time playing around with the logging module. I have two questions really:
我在创建FileHandler时发现的所有示例都只使用一个虚拟文件名,但是我希望我的日志文件具有某种标题格式.
All of the examples I have found on creating a FileHandler just use a dummy file name, but I want my log file to have a certain title format.
log = time.strftime('./logs/'+'%H:%M:%S %d %b %Y', time.localtime())+'.log'
logger = logging.getLogger('myproject')
formatter = logging.Formatter('%(asctime)s %(message)s', '%H:%M:%S %d %b %Y')
handler = logging.FileHandler(log)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
该似乎首先起作用,因为创建了日志文件,但是当我杀死脚本并对其进行检查时,其中没有任何内容.在建议使用with open(log) as f:
之前,在handler = logging.FileHandler(log)
中使用f
代替log
给我有关文件甚至未创建的错误.没有写入日志文件,这是我做错了吗?是因为我试图对文件名太聪明吗?
This seems to work at first, in that the log file is created, but when I kill the script and examine it, there is nothing in it. And before you suggest using with open(log) as f:
, using f
in place of log
in handler = logging.FileHandler(log)
gives me errors about the file not even being created. What am I doing wrong to not have the log file written to? Is it because I'm trying to be too clever with the filename?
第二个问题可能以某种方式包含第一个问题.我真正想要的是一种同时写入控制台和日志文件的方法,因为该脚本需要很长时间,并且我想评估进度.我已经看到了有关如何执行此操作的答案,例如
The second question may subsume the first somehow. What I would really like is a way to write to both the console and the log file concurrently, since this script takes a long time and I'd like to gauge progress. I have seen that there are answers on how to do this, such as here, but all of these seem to assume that I want different levels of logging for the two different outputs; I don't. Incidentally, I do realize that if my script didn't dynamically generate the name of the log file, I could do something like python myscript.py | tee log.file
to avoid making different handlers.
由于第一个问题,我觉得我很难回答第二个问题,但是由于生病和随后缺乏睡眠,我现在有雾.如果有人像五岁的孩子一样向我解释如何在控制台和日志文件中写入相同的输出,我将不胜感激.
I feel I am having trouble answering my second question because of the variables involved in the first, but I am foggy right now due to illness and subsequent lack of sleep. If someone could explain to me like a five-year-old how to write to both a console and log file with identical output, I would greatly appreciate it.
非常感谢.
推荐答案
您的代码正在配置一个名为"myproject"的记录器:
Your code is configuring a logger named 'myproject':
logger = logging.getLogger('myproject')
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
如果您在该记录器上使用方法,那么一切都会起作用:
If you use methods on that logger, everything will work:
logger.info('foo bar')
logging.getLogger('myproject').info('foo bar')
但是,如果仅使用模块级功能,则不会:
But if you just use the module-level functions, it will not:
logging.info('foo bar')
为什么?由于模块级功能使用默认的根记录器,因此它不是您配置的.有关其工作原理的详细信息,请参见文档.
Why? Because the module-level functions use the default root logger, which is not the one you've configured. See the docs for details on how this works.
通常,处理此问题的方法是为每个模块创建模块级logger
对象,如下所示:
Generally, the way you deal with this is either to create a module-level logger
object for each module, like this:
logger = logging.getLogger(__name__)
…或类或实例属性logger
,如下所示:
… or a class or instance attribute logger
, something like this:
self.logger = logging.getLogger('{}.{}'.format(__name__, cls.__name__))
然后通过该logger
或self.logger
对象(而不是通过模块级函数)进行所有操作.
And then do everything through that logger
or self.logger
object, not through the module-level functions.
那么,为什么还要模块级功能呢?为方便起见,主要是简单的程序.如果这听起来很适合您,并且您想使用它们,则可以;可以.您只需要配置root记录器即可,而不必配置其他记录器.将第一行更改为此:
So, why are the module-level functions even there? For convenience in simple programs, mainly. If that sounds appropriate for you, and you want to use them, you can; you just have to configure the root logger instead of a different one. Change the first line to this:
logger = logging.getLogger()
现在,在配置logger
时,您还正在配置模块级功能.
Now when you configure logger
, you're also configuring the module-level functions.
这篇关于FileHandler没有将输出发送到我想要的任何位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!