简体   繁体   中英

Python logging: Flask + uWsgi + nginx

All, I am not able to log INFO and DEBUG messages when running my Flask application behind uwsgi. not only the Debug and Info message are not being logged, but also the formatting is not working.

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.setLevel(logging.DEBUG)
handler.setFormatter(formatter)

application.logger.addHandler(handler)

logging.debug('Debug Message')
logging.info('Info Message')
logging.warning('Warning Message')
logging.error('Error Message')
logging.critical('Critical ')

The output in the uwsgi log file is:

*** Operational MODE: preforking ***
WARNING:root:Warning Message
ERROR:root:Error Message
CRITICAL:root:Critical 
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x26f39b0 pid: 9574 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 9574)

As you can see from above, only Error, Warning and Critical messages are being logged. And the formatting is not working. (All works fine when running locally on werkzeug).

the uwsgi config file is as follow:

[uwsgi]
project = myproject
uid = nginx
group = nginx
base = /srv
req-logger = file:/var/log/uwsgi/access.log
logger = file:/var/log/uwsgi/error.log

chdir = %(base)/www
home = %(base)/env
module = application
callable = application

master = true
processes = 4

socket = /var/run/uwsgi/%(project).sock
chown-socket = %(uid):%(group)
chmod-socket = 664
vacuum = true

I checked all the solutions on StackOverflow, but none of them worked.

Any help is much appreciated.

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 Flask
app = 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 Message
2020-10-26T14:13:27Z INFO 3197 ---- MainThread  test_log : <module> {test_log.py:78} Info Message
2020-10-26T14:13:27Z WARNING 3197 ---- MainThread  test_log : <module> {test_log.py:79} Warning Message
2020-10-26T14:13:27Z ERROR 3197 ---- MainThread  test_log : <module> {test_log.py:80} Error Message
2020-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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM