문제 : Flask 로깅을 위해서 Python의 기본 logging 패키지를 사용했을 경우 config값을 사용할 수 없어서 서버별로 파일 디렉토리가 다른 경우가 존재
해결 : 로깅설정을 Flask App에 설정하여 config 값에 따라 경로 및 설정 변경 가능하도록 변경
...
def create_app(config_name: str):
from config import config
current_confg = config[config_name]
app = Flask(__name__)
configure_app(app. current_config)
configure_logging(app)
...
return app
def configure_logging(app: Flask) -> None:
import logging
from logging import FileHandler
from logging import Formatter
logger = logging.getLogger('로거명')
logging.basicConfig()
log_file_handler = FileHandler(filename=app.config["LOGGER_PATH"], encoding="utf-8", mode="a")
log_formatter = Formatter("[%(process)d:%(processName)s:%(thread)d:%(threadName)s] %(asctime)s : %(message)s [in %(filename)s:%(lineno)d]")
log_file_handler.setFormatter(log_formatter)
app.logger = logger
if app.config["DEBUG"] is True:
app.logger.setLevel(logging.DEBUG)
else:
app.logger.setLevel(logging.INFO)
app.logger.addHandler(log_file_handler)
'개발 > Python' 카테고리의 다른 글
Python, mod_wsgi, Apache 설정 시 libpython 에러 (0) | 2020.12.02 |
---|---|
Python3.8 가상환경 pip 이슈 (0) | 2020.10.29 |
Flask에서 Excel 읽어오기(flask-excel) (0) | 2020.07.05 |
wtforms-json 사용하여 유효성 확인 (0) | 2020.07.05 |
Python VirtualHost 설정 (0) | 2020.06.03 |