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將日誌輸出寫入文件?看起來這與測試stdout
和stderr
的鼻測有關,但我似乎無法弄清楚。
我從Python logging cookbook得到了代碼。