在Python中启用和禁用调试消息
我目前正在学习Python,为了了解幕后的情况,我编写了很多打印输出。 鉴于返回并注释所有消息非常麻烦,我已经开始编写一个模块,在该模块中,我将所有消息设置为我想要使用的方法,然后使用布尔值来关闭和打开消息。 问题是,我没有得到任何打印输出,而不是调试消息,这不是很优雅。有什么办法可以解决这个问题吗 一些示例代码:在Python中启用和禁用调试消息,python,debugging,nonetype,Python,Debugging,Nonetype,我目前正在学习Python,为了了解幕后的情况,我编写了很多打印输出。 鉴于返回并注释所有消息非常麻烦,我已经开始编写一个模块,在该模块中,我将所有消息设置为我想要使用的方法,然后使用布尔值来关闭和打开消息。 问题是,我没有得到任何打印输出,而不是调试消息,这不是很优雅。有什么办法可以解决这个问题吗 一些示例代码: def setDebug(bool): ''' Toggles the debug messages ''' global _debug _
def setDebug(bool):
'''
Toggles the debug messages
'''
global _debug
_debug = bool
def setNewMsg(msg):
'''
Appends a new debug message to the list
'''
global _debugMsg
_debugMsg.append(msg)
def getDebugMsg(index):
'''
Takes an int for a parameter and returns the debug message needed
'''
global _debug
global _debugMsg
if _debug == True:
return _debugMsg[index]
else:
return
既然你说你是Python的新手,我想你应该考虑使用<代码>日志记录/代码>模块
看看这个,也真的能帮上忙 从Python文档中:This module defines functions and classes which implement a flexible event logging system for applications and libraries.
您可以将日志记录模块设置为将所有打印内容保存到文件中,通过控制日志记录级别,您可以控制发送消息的级别
例如:
import logging
logging.basicConfig(filename='mylog.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
如果
level=logging.DEBUG
您将能够看到所有消息,但通过更改为level=logging.INFO
它将只保存到文件INFO和更高版本。尝试链接非常有用。 因为你说你是Python的新手,我想你应该考虑使用<代码>日志记录>代码>模块
看看这个,也真的能帮上忙
从Python文档中:
This module defines functions and classes which implement a flexible event logging system for applications and libraries.
您可以将日志记录模块设置为将所有打印内容保存到文件中,通过控制日志记录级别,您可以控制发送消息的级别
例如:
import logging
logging.basicConfig(filename='mylog.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
如果
level=logging.DEBUG
您将能够看到所有消息,但通过更改为level=logging.INFO
它将只保存到文件INFO和更高版本。请尝试这些链接,它们非常有用。如果\u debug
变量为false,您将返回它,因此您将得到无
如果\u debug
为false,则可以返回空字符串:
return ''
或者您可能希望返回一条消息:
return 'Debug mode is not set to True'
不返回任何内容与返回None
基本相同。即使不返回任何内容,Python也会将返回值设置为None
:
>>> def test():
pass
>>> a = test()
>>> print a
None
此外,如果要使用方法而不是模块,可能需要检查\u debugMsg[index]
是否存在
if _debug:
try:
return _debugMsg[index]
except IndexError:
return 'The debug message hasn't listed yet.'
希望这有帮助 如果\u debug
变量为false,则返回该变量,因此将得到None
如果\u debug
为false,则可以返回空字符串:
return ''
或者您可能希望返回一条消息:
return 'Debug mode is not set to True'
不返回任何内容与返回None
基本相同。即使不返回任何内容,Python也会将返回值设置为None
:
>>> def test():
pass
>>> a = test()
>>> print a
None
此外,如果要使用方法而不是模块,可能需要检查\u debugMsg[index]
是否存在
if _debug:
try:
return _debugMsg[index]
except IndexError:
return 'The debug message hasn't listed yet.'
希望这有帮助 如果\u debug
为False
,则返回None
,您是否知道您得到的是None
?另外,您不应该将变量命名为bool
,因为您将重写内置函数bool
,那么,我如何返回nothing而不是None呢?另请参见,您是否意识到您得到的是None
,因为如果\u debug
为False
,您将返回None
?另外,您不应该将变量命名为bool
,因为您将重写内置函数bool
,那么,我如何返回nothing而不是None呢?另请参见,谢谢您,先生,一旦我进一步了解了我的编码,我将阅读日志实用程序。现在我正在为MOOC写一些简单的例子。在线测试算法对打印出来的内容非常挑剔,我正在寻找一种简单地关闭登录的快速方法。干杯谢谢你,先生,我会阅读日志实用程序,一旦我有一点进一步提前与我的编码。现在我正在为MOOC写一些简单的例子。在线测试算法对打印出来的内容非常挑剔,我正在寻找一种简单地关闭登录的快速方法。干杯谢谢你的帮助!我的问题是我不想打印任何东西。见上文。干杯谢谢你的帮助!我的问题是我不想打印任何东西。见上文。干杯