当你调用logging.info(...)时,info指的是实际上是函数名称。作为一个 解决方法,使用logging.getLogger(...)。info(...)。 提交文件仍然是个好主意错误报告。 Peter 不确定错误的位置。下面的脚本在调用时打印 信息。到控制台和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 logginglogging.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.stderrconsole = logging.StreamHandler()console.setLevel(logging.INFO)# set a format which is simpler for console useformatter = logging.Formatter(''%(message)s'')# tell the handler to use this formatconsole.setFormatter(formatter)# add the handler to the root loggerlogging.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 allinfo lines on the console as intended.Can anybody tell my mistake? I thought changing the level in basicConfigwould have no impact on the following StreamHandler instance.Also, adding %(funcName)-8s to the formatter in basigConfig does not workfor me: instead of the functions name the level in lower case gets insertedinto 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 linux2TIAChristian 解决方案basicConfig sets the root logger level; if it''s ERROR, nothing below ERRORwill pass. Your handler does not have a chance to get the message.basicConfig sets the overall verbosity of your application.Looks like a bug...--Gabriel GenellinaWhen you call logging.info(...), "info" actually is the function name. As aworkaround use logging.getLogger(...).info(...).It would still be a good idea to file a bug report.PeterNot sure where the bug is. The script below, when called, prints"Information" to the console and "INFO some_func Information" tomc_rigid.log, as I would expect. (Python 2.5.1)Best regards,Vinayimport logginglogging.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 thesys.stderrconsole = logging.StreamHandler()console.setLevel(logging.INFO)# set a format which is simpler for console useformatter = logging.Formatter(''%(message)s'')# tell the handler to use this formatconsole.setFormatter(formatter)# add the handler to the root loggerlogging.getLogger('''').addHandler(console)def some_func():logging.info("Information")if __name__ == "__main__":some_func() 这篇关于记录模块的奇怪行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 10:27