我从Automate The Boring Stuff With Python获得了代码。
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s(levelname)s- %(message)s')
logging.debug('Start of program')
def factorial(n):
logging.debug('Start of factorial( %)' % (n))
total = 1
for i in range(n + 1):
total *= i
logging.debug('i is ' + str(i) + ', total is ' + str(total))
logging.debug('End of factorial( %)' % (n))
return total
print (factorial(5))
logging.debug('End of program')
运行代码时,会发生以下错误:
2015-06-18 12:51:47,073 - 调试 - 开始计划 Traceback(最近一次调用最后一次): 文件“/home/raqeeb.alam/PycharmProjects/E_mail/sub_links.py”,第14行,在 打印阶乘(5) 文件“/home/raqeeb.alam/PycharmProjects/E_mail/sub_links.py”,第6行,在factorial中 logging.debug('阶乘(%)'%(n)的开头) ValueError:索引21处不支持的格式字符')'(0x29)
对于这些行:
print (factorial(5))
logging.debug('End of program')
答案 0 :(得分:3)
应该是:
logging.debug('Start of factorial(%s)' % (n))
...
logging.debug('End of factorial(%s)' % (n))
顺便说一句:括号在这里是多余的。可以简单地说:
logging.debug('Start of factorial(%s)' % n)
...
logging.debug('End of factorial(%s)' % n)
' %(asctime)s(levelname)s- %(message)s'
应为' %(asctime)s%(levelname)s- %(message)s'
答案 1 :(得分:2)
您错过了%
标志:
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s%(levelname)s- %(message)s')
# ^ HERE
答案 2 :(得分:2)
logging.debug('Start of factorial( %)' % (n))
logging.debug('End of factorial( %)' % (n))
这两行应该是
logging.debug('Start of factorial(%s)' % (n))
logging.debug('End of factorial(%s)' % (n))