python - Formatting Flask app logs in json -
i'm working python/flask application , trying logs formatted (by line) in json.
using python-json-logger package, i've modified formatter app.logger follows:
from pythonjsonlogger import jsonlogger formatter = jsonlogger.jsonformatter( '%(asctime) %(levelname) %(module) %(funcname) %(lineno) %(message)') app.logger.handlers[0].setformatter(formatter)
this works expected. messages passed app.logger
correctly formatted in json.
however application automatically logging requests. information shows in stdout follows:
127.0.0.1 - - [19/jun/2015 12:22:03] "get /portal/ http/1.1" 200 -
i want information formatted in json well. i've been looking logger/code responsible creating output no success.
where output generated? mechanisms change formatting of logged information?
when use app.logger
attribute first time, flask sets log handlers:
- a debug logger set log level
debug
, filters onapp.debug
being true. - a production logger set
error
.
you can remove handlers again yourself, running:
app.logger.handlers[:] = []
however, log line see not logged flask, wsgi server. both built-in werkzeug server (used when use app.run()
) , various other wsgi servers this. gunicorn example uses default python logger record access.
the built-in wsgi server should never used in production (it won't scale well, , not battle-hardened against malicious attackers).
for gunicorn, can disable log propagation keep logging separate:
logging.getlogger('gunicorn').propagate = false
Comments
Post a Comment