2017-10-21 74 views
0

我正在创建自定义日志记录模块以在应用程序框架中实现。我正在使用PyCharm和unittest模块,并将我的测试人员作为测试人员。如何将日志信息保存到具有nosetests和日志包的文件中

这是一个简单的测试案例,但它不会像我所希望的那样将输出保存到文件/tmp/test.log

import unittest 
import logging 

class Logger_class____test_cases(unittest.TestCase): 
    def test_log_file_basics(self): 
     # set up logging to file - see previous section for more details 
     logging.basicConfig(level=logging.DEBUG, 
          filename='/tmp/test.log') 
     # define a Handler which writes INFO messages or higher to the sys.stderr 
     console = logging.StreamHandler() 
     console.setLevel(logging.INFO) 
     # set a format which is simpler for console use 
     formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') 
     # tell the handler to use this format 
     console.setFormatter(formatter) 
     # add the handler to the root logger 
     logging.getLogger('').addHandler(console) 

     # Now, we can log to the root logger, or any other logger. First the root... 
     logging.info('Jackdaws love my big sphinx of quartz.') 

     # Now, define a couple of other loggers which might represent areas in your 
     # application: 
     logger1 = logging.getLogger('myapp.area1') 
     logger2 = logging.getLogger('myapp.area2') 

     logger1.debug('Quick zephyrs blow, vexing daft Jim.') 
     logger1.info('How quickly daft jumping zebras vex.') 
     logger2.warning('Jail zesty vixen who grabbed pay from quack.') 
     logger2.error('The five boxing wizards jump quickly.') 

如何获取nosetests将日志输出写入文件?看起来这与测试stdoutstderr的鼻测有关,但我似乎无法弄清楚。

我从Python logging cookbook得到了代码。

回答

0

我想通了。我在我的框架中的全局模块中配置了一个默认的日志文件名。

我有这样的事情这是压倒一切的我的测试情况如下:

logging.basicConfig(level=logging.DEBUG, 
         format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', 
         datefmt='%m-%d %H:%M', 
         filename='log_file_name.log', 
         filemode='w') 

当然墨菲定律的同时,我张贴问题,我会很快找到答案。大声笑

相关问题