2020年2月17日

[筆記] 在 flask + gunicorn + supervisor 環境中記錄 log

設定
app = Flask(__name__)
log_filename = datetime.datetime.now().strftime("/tmp/flask-%Y-%m-%d.log")
handler = logging.FileHandler(log_filename, encoding='UTF-8')
handler.setLevel(logging.DEBUG)
logging_format = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
handler.setFormatter(logging_format)
app.debug = True


紀錄
app.logger.debug('message')
app.logger.info('message')
app.logger.warning('message')
app.logger.error('message')
app.logger.critical('message')


supervisor 設定檔
[program:flask]
directory = /home/flask
command = /usr/local/bin/gunicorn -w 1 -b 0.0.0.0:5000 index:app
autostart = true
startsecs = 5
autorestart = true
startretries = 3
stopasgroup=true
killasgroup=true
user = flask
redirect_stderr = true
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
stdout_logfile = /tmp/flask.log


查看
$ supervisorctl tail -f flask

$ tail -f /tmp/flask-xxxx-xx-xx.log

$ tail -f /tmp/flask.log

0 意見:

張貼留言