如何在嵌入式python解释器中查看日志输出?

时间:2014-10-13 09:06:30

标签: python logging interpreter

我在我的iOS应用程序中使用嵌入式python解释器。我可以在Xcode控制台中看到print输出,但我无法看到任何logging.info()logging.debug()输出。我该如何启用它?

我的测试python代码:

import logging
import sys

# try to add logging output to stdout
logging.basicConfig()
log = logging.getLogger()

log.addHandler(logging.StreamHandler())
log.addHandler(logging.StreamHandler(sys.stdout))

# for calling from outside
def main__(args):
    print("main__()") # can see output
    print type(args)
    print len(args)
    for eachArg in args:
#        print('#: %s' % eachArg) # can see output
        logging.info(eachArg) # can NOT see output

输出是:

main__()
<type 'list'>
281

1 个答案:

答案 0 :(得分:7)

我认为这些东西是在调试级别。 这段代码适合我:

import logging
import sys

log = logging.getLogger()
log.setLevel(logging.DEBUG)
stream = logging.StreamHandler(sys.stdout)
stream.setLevel(logging.DEBUG)
log.addHandler(stream)

# for calling from outside
def main__(args):
    print("main__()") # can see output
    print type(args)
    print len(args)
    for eachArg in args:
        logging.info(eachArg)

输出:

>>> main__('test')
main__()
<type 'str'>
4
t
e
s
t
>>>