2017-10-21 79 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') 

當然墨菲定律的同時,我張貼問題,我會很快找到答案。大聲笑

相關問題