试图理解和学习如何编写包...用我一直使用的东西进行测试,记录...
您能帮我理解为什么“日志”变量不起作用...并且屏幕上没有日志记录吗?
Cảm ơn!
主要文件:
#!/opt/local/bin/python
import sys
sys.path.append('CLUSTER')
import clusterlogging.differentlogging
clusterlogging.differentlogging.consolelogging()
log.debug("Successfully logged in")
不同的日志记录.py
#!/opt/local/bin/python
def consolelogging():
import logging
class NullHandler(logging.Handler):
def emit(self, record):
vượt qua
print "Console Logging loaded"
DEFAULTLOGLEVEL=logging.INFO
log = logging.getLogger(__name__)
log.addHandler(NullHandler())
log.debug("Successfully logged in")
def mysqllogging():
print "mysql logging module here"
def sysloglogging():
print "rsyslog logging module here"
输出
Console Logging loaded
Traceback (most recent call last):
File "./svnprod.py", line 10, in
log.debug("Successfully logged in")
NameError: name 'log' is not defined
log
Đúng differentlogging
模块中的一个全局变量。因此您可以访问它作为clusterlogging.differentlogging.log
.
您还可以执行类似from clusterlogging.differentlogging import log
的操作,然后将其作为log
访问。
biên tập:实际上,在再次检查您的代码时,我不知道该怎么做。您能否修复您的代码缩进,使其有意义?您是否在 consolelogging
函数中定义了 log
?如果是这样,您需要使用 global log
使其成为全局的,或者从函数返回它并将其分配给调用该函数的行上的变量 log
.
Tôi là một lập trình viên xuất sắc, rất giỏi!