Python logging: Flask + uWsgi + nginx
By accessing application.logger.addHandler(handler)
you initialized Flask's logger. However, in the lines after you're using root
logger with logging.debug('Debug Message')
, instead of application logger with application.logger.debug('Debug Message')
.
Also, app.logger.setLevel(logging.DEBUG)
.
Full example:
from flask import Flaskapp = Flask(__name__)formatter = logging.Formatter( # pylint: disable=invalid-name '%(asctime)s %(levelname)s %(process)d ---- %(threadName)s ' '%(module)s : %(funcName)s {%(pathname)s:%(lineno)d} %(message)s','%Y-%m-%dT%H:%M:%SZ')handler = StreamHandler()handler.setFormatter(formatter)app.logger.setLevel(logging.DEBUG)app.logger.addHandler(handler)app.logger.removeHandler(default_handler)app.logger.debug('Debug Message')app.logger.info('Info Message')app.logger.warning('Warning Message')app.logger.error('Error Message')app.logger.critical('Critical ')
prints:
...2020-10-26T14:13:27Z DEBUG 3197 ---- MainThread test_log : <module> {test_log.py:77} Debug Message2020-10-26T14:13:27Z INFO 3197 ---- MainThread test_log : <module> {test_log.py:78} Info Message2020-10-26T14:13:27Z WARNING 3197 ---- MainThread test_log : <module> {test_log.py:79} Warning Message2020-10-26T14:13:27Z ERROR 3197 ---- MainThread test_log : <module> {test_log.py:80} Error Message2020-10-26T14:13:27Z CRITICAL 3197 ---- MainThread test_log : <module> {test_log.py:81} Critical ...
Please update your Flask version to 1.0.2. Here is a small example using the app.logger behind uwsgi-emperor and nginx:
def create_app(**kwargs): app = Flask(__name__) app.logger.setLevel(logging.DEBUG) @app.route('/ping/', methods=['GET']) def ping_pong(): app.logger.critical('Healthcheck called') app.logger.error('Healthcheck called') app.logger.warning('Healthcheck called') app.logger.info('Healthcheck called') app.logger.debug('Healthcheck called') return jsonify({ 'status': 'Epic success', 'message': 'pong!' })
All log messages appear in syslog:
[2018-05-14 08:43:33,778] CRITICAL in __init__: Healthcheck called[2018-05-14 08:43:33,779] ERROR in __init__: Healthcheck called[2018-05-14 08:43:33,779] WARNING in __init__: Healthcheck called[2018-05-14 08:43:33,779] INFO in __init__: Healthcheck called[2018-05-14 08:43:33,779] DEBUG in __init__: Healthcheck called
Hope that helps.