0
응용 프로그램 프레임 워크에서 구현할 사용자 지정 로깅 모듈을 만들고 있습니다. 그리고 Pycharm을 unittest
모듈과 nosetests를 테스트 러너로 사용하고 있습니다.로그 정보를 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.')
로그 출력을 파일에 기록하려면 어떻게해야합니까? stdout
과 stderr
을 캡처하는 nosetests와 관련이있는 것처럼 보입니다. 그러나 나는 알아낼 수 없습니다.
Python logging cookbook에서 코드를 받았습니다.