我想通过定义一个可用于吞吐模块的变量来将调试输出与生产输出分开。它无法在环境中定义。对模块中的类重复使用全局变量的任何建议? 另外有一种方法可以配置这个变量标志,用于告诉不使用此代码的appengine。
答案 0 :(得分:12)
查看Google App Engine完全支持的logging module。您可以指定日志级别,例如调试,警告,错误等。它们将显示在开发服务器控制台中,并且还将存储在请求日志中。
如果您仅在运行开发服务器时执行特定代码,则可以执行以下操作:
if os.environ['SERVER_SOFTWARE'].startswith('Development'):
print 'Hello world!'
SERVER_SOFTWARE变量始终由Google App Engine设置。
至于模块特定变量;模块是对象,可以像任何其他对象一样具有值:
my_module.debug = True
答案 1 :(得分:1)
所有模块级变量对模块中的所有类都是全局的。
这是我的档案:mymodule.py
import this
import that
DEBUG = True
class Foo( object ):
def __init__( self ):
if DEBUG: print self.__class__, "__init__"
# etc.
class Bar( object ):
def do_work( self ):
if DEBUG: print self.__class__, "do_work"
# etc.
这两个类的所有实例都将找到单个模块级DEBUG
变量。其他模块(例如,this.py
和that.py
)可以拥有自己的DEBUG
个变量。这些将是this.DEBUG
或that.DEBUG
,并且不相关。