记录模块的奇怪行为? [英] strange behavious of the logging module?
问题描述
有以下代码:
导入日志
logging.basicConfig(level = logging.ERROR,
format =''%(levelname)-8s%(message)s'',
filename =''mc_rigid.log'',
filemode =''w'')
#定义一个Handler,它将INFO消息或更高的消息写入sys.stderr
console = logging.StreamHandler ()
console.setLevel(logging.INFO)
#设置一种更易于控制台使用的格式
formatter = logging.Formatter( ''%(message)s'')
#告诉处理程序使用这种格式
console.setFormatter(formatter)
#add根记录器的处理程序
logging.getLogger('''')。addHandler(控制台)
我在控制台上看不到任何内容。但是,如果我将log.ERROR中的级别改为
(刚刚导入之后)到logging.DEBUG,我确实会在控制台上看到所有
信息行。
有谁可以告诉我的错误?我认为更改basicConfig
中的级别对以下StreamHandler实例没有影响。
此外,将%(funcName)-8s添加到basigConfig中的格式化程序不起作用
对我来说:代替函数名称,小写的级别会被插入日志文件中。
我的python:
Python 2.5.1(r251:54863,2007年5月2日,16:56:35)
[GCC 4.1.2(Ubuntu 4.1.2-0ubuntu4)] on linux2
TIA
Christian
En Sun,2007年9月23日18:53:53 -0300,Christian Meesters
< me ****** @ uni-mainz.deescribi ???:
导入日志记录
logging.basicConfig(level = logging.ERROR,
format =''%(levelname)-8s%(message)s'',
filename =''mc_rigid.log'',
filemode =''w'')
#defi将一个或多个INFO消息写入sys.stderr的处理程序
console = logging.StreamHandler()
console.setLevel(logging.INFO)
#设置一种更易于控制台使用的格式
formatter = logging.Formatter(''%(message)s'')
#告诉处理程序使用这种格式
console.setFormatter(格式化程序)
#将处理程序添加到根记录器
logging.getLogger('''') .addHandler(控制台)
我在控制台上看不到任何内容。但是,如果我将log.ERROR中的级别改为
(刚刚导入之后)到logging.DEBUG,我会在控制台上看到所有
信息行。
basicConfig设置根记录器级别;如果它是错误的,那么ERROR
以下的任何内容都将通过。你的处理程序没有机会收到消息。
有人可以告诉我的错误吗?我认为更改basicConfig
中的级别对以下StreamHandler实例没有影响。
basicConfig设置应用程序的整体详细程度。
另外,添加%(funcName)-8s到basigConfig中的格式化程序不起作用
对我来说:代替函数名称,小写的级别得到
插入
到日志文件中。
看起来像个bug ...
-
Gabriel Genellina >
Christian Meesters写道:
此外,将%(funcName)-8s添加到basigConfig中的格式化程序对我来说不起作用
:代替函数名称,小写的级别会被插入到日志文件中。
。
当你调用logging.info(...)时,info指的是实际上是函数名称。作为一个
解决方法,使用logging.getLogger(...)。info(...)。
提交文件仍然是个好主意错误报告。
Peter
9月24日上午8:03,Peter Otten< __ pete ... @ web .dewrote:
>
提交错误报告仍然是一个好主意。
不确定错误的位置。下面的脚本在调用时打印
信息。到控制台和INFO some_func信息按照我的预期,
mc_rigid.log。 (Python 2.5.1)
祝你好运,
Vinay
导入日志记录
logging.basicConfig(level = logging.DEBUG,
format =''%(levelname)-8s%(funcName)-8s%
(消息)s'',
filename =''mc_rigid.log'',
filemode =''w'')
#define将INFO消息或更高信息写入
sys.stderr的处理程序
console = logging.StreamHandler()
console.setLevel(日志记录。信息)
#设置一种更易于控制台使用的格式
formatter = logging.Formatter(''%(message)s'')
#告诉处理程序使用这种格式
console.setFormatter(格式化程序)
#将处理程序添加到根记录器
logging。 getLogger('''')。addHandler(console)
def some_func():
logging.info(" Information)
if __name__ ==" __ main __":
some_func()
Hi,
having the following code:
import logging
logging.basicConfig(level=logging.ERROR,
format=''%(levelname)-8s %(message)s'',
filename=''mc_rigid.log'',
filemode=''w'')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter(''%(message)s'')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('''').addHandler(console)
I observe nothing printed on the console. However if I change the level
(just after the import) from logging.ERROR to logging.DEBUG I do see all
info lines on the console as intended.
Can anybody tell my mistake? I thought changing the level in basicConfig
would have no impact on the following StreamHandler instance.
Also, adding %(funcName)-8s to the formatter in basigConfig does not work
for me: instead of the functions name the level in lower case gets inserted
into the logfile.
My python:
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
TIA
Christian
En Sun, 23 Sep 2007 18:53:53 -0300, Christian Meesters
<me******@uni-mainz.deescribi???:
import logging
logging.basicConfig(level=logging.ERROR,
format=''%(levelname)-8s %(message)s'',
filename=''mc_rigid.log'',
filemode=''w'')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter(''%(message)s'')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('''').addHandler(console)
I observe nothing printed on the console. However if I change the level
(just after the import) from logging.ERROR to logging.DEBUG I do see all
info lines on the console as intended.basicConfig sets the root logger level; if it''s ERROR, nothing below ERROR
will pass. Your handler does not have a chance to get the message.
Can anybody tell my mistake? I thought changing the level in basicConfig
would have no impact on the following StreamHandler instance.basicConfig sets the overall verbosity of your application.
Also, adding %(funcName)-8s to the formatter in basigConfig does not work
for me: instead of the functions name the level in lower case gets
inserted
into the logfile.Looks like a bug...
--
Gabriel Genellina
Christian Meesters wrote:
Also, adding %(funcName)-8s to the formatter in basigConfig does not work
for me: instead of the functions name the level in lower case gets inserted
into the logfile.When you call logging.info(...), "info" actually is the function name. As a
workaround use logging.getLogger(...).info(...).
It would still be a good idea to file a bug report.
Peter
On Sep 24, 8:03 am, Peter Otten <__pete...@web.dewrote:>
It would still be a good idea to file a bug report.Not sure where the bug is. The script below, when called, prints
"Information" to the console and "INFO some_func Information" to
mc_rigid.log, as I would expect. (Python 2.5.1)
Best regards,
Vinay
import logging
logging.basicConfig(level=logging.DEBUG,
format=''%(levelname)-8s %(funcName)-8s %
(message)s'',
filename=''mc_rigid.log'',
filemode=''w'')
# define a Handler which writes INFO messages or higher to the
sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter(''%(message)s'')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('''').addHandler(console)
def some_func():
logging.info("Information")
if __name__ == "__main__":
some_func()
这篇关于记录模块的奇怪行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!