使用禁用功能后,Python日志记录停止工作

时间:2018-06-17 04:48:55

标签: python-3.x logging anaconda spyder

我使用Anaconda和Spyder;我正在学习如何将调试消息打印到控制台以及将它们写入日志文件。

每次切换logging.disable函数时,日志记录都会停止将打印调试消息打印到控制台或写入日志文件。

以下是导致我遇到问题的步骤。

  1. 在我使用logging.disable函数之前,日志记录工作正常。

  2. 使用禁用功能,然后将其注释掉以重新启用日志记录后,对于写入文件或打印到控制台,日志记录都不再起作用。

  3. 重新启动Spyder并再次进行日志记录,但问题仍然存在。

  4. 我发现了类似的stackoverflow问题Here建议使用: (a)logging.disable(logging.NOTSET)或 (b)logging.shutdown() reload(logging)

  5. (a)和(b)都不起作用。

    以下是有问题的代码:

    import logging
    
    #Config for writing to the console
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
    
    #Config for writing to file
    #logging.basicConfig(filename='myProgramLog.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
    
    #logging.disable(logging.CRITICAL)
    
    logging.debug('Start of program')
    
    def factorial(n):
        logging.debug('Start of factorial (%s)' %(n))
        total=1
        for i in range(1, n+1):
            total *= i
            logging.debug('i is %s, total is %s' % (i, total))
    
        logging.debug('Return value is %s' % (total))
        return total
    
    print(factorial(5))
    
    logging.debug('End of program')
    

0 个答案:

没有答案
相关问题