Last active
March 27, 2019 10:21
-
-
Save andrisasuke/0d5958c076fcf44e6a15c9d99d9c8c86 to your computer and use it in GitHub Desktop.
flask logging gunicorn and file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from logging.handlers import RotatingFileHandler | |
from flask import Flask | |
import logging | |
from logging import Formatter | |
app = Flask(__name__) | |
logger = logging.getLogger('your-logger-name') | |
file_handler = RotatingFileHandler('test.log', maxBytes=10000, backupCount=1) | |
handler = logging.StreamHandler() | |
file_handler.setFormatter(Formatter( | |
'%(asctime)s %(levelname)s: %(message)s ' | |
'[in %(pathname)s:%(lineno)d]' | |
)) | |
handler.setFormatter(Formatter( | |
'%(asctime)s %(levelname)s: %(message)s ' | |
'[in %(pathname)s:%(lineno)d]' | |
)) | |
logger.addHandler(file_handler) | |
logger.addHandler(handler) | |
logger.setLevel(logging.DEBUG) | |
@app.route('/') | |
def hello(): | |
logger.info('info log') | |
logger.debug('debug log') | |
logger.error('error log') | |
return 'Hello!' | |
if __name__ == '__main__': | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gunicorn run : | |
$> gunicorn run:app -b localhost:3000 --log-file log_path/access.log --g-level debug --error-logfile log_path/access.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have problems using the logger from inside another blueprint. Any idea on how to do it ?