파이썬 프로그램 실행시 화면에 나오는 것을 로깅해서 디버깅할 때 이용할 필요가 있다.
이에, 화면에 출력하면서 파일로 저장하는 방법에 대한 고찰임
ref :stackoverflow.com/questions/9321741/printing-to-screen-and-writing-to-a-file-at-the-same-time
import logging
level = logging.INFO
format = ' %(message)s'
handlers = [logging.FileHandler('filename.log'), logging.StreamHandler()]
logging.basicConfig(level = level, format = format, handlers = handlers)
logging.info('Hey, this is working!')
print문을 재정의하는 방법
from typing import Callable
def print_logger(
old_print: Callable,
file_name: str,
) -> Callable:
"""Returns a function which calls `old_print` twice, specifying a `file=` on the second call.
Arguments:
old_print: The `print` function to call twice.
file_name: The name to give the log file.
"""
def log_print(*args, **kwargs):
old_print(*args, **kwargs)
with open(file_name, "a") as log_file:
old_print(*args, file=log_file, **kwargs)
return log_print
print = print_logger(print, "logs/my_log.log")
'Python' 카테고리의 다른 글
Python Image Processing Library (0) | 2013.04.23 |
---|---|
python code를 바꾼 다음 인터프리터에 반영하기 (0) | 2012.07.13 |
display class instance name & print class (0) | 2012.07.13 |
built-in fucntion 1 - ord (0) | 2012.07.11 |
Python 관련 사이트 모음... (0) | 2012.02.16 |