Change Flask Logging Output Format/Handler

Apr 25, 2019
import sysimport loggingfrom flask import Flaskapp = Flask(__name__)log = app.loggerformatter = logging.Formatter(fmt="%(asctime)s %(levelname)s %(module)s: %(message)s",                          datefmt="%H:%M:%S")handler = logging.StreamHandler(sys.stdout)handler.setLevel(logging.DEBUG)handler.setFormatter(formatter)# if this is not called, log will output both original and new formatlog.handlers.clear()log.addHandler(handler)@app.route('/')def test_logging():    app.logger.info('app.logger')    return 'Test logging'if __name__ == '__main__':    app.run(host='127.0.0.1', port=8088, debug=True)

New Logging Format

11:04:49 INFO test: app.logger

Original Flask Logging Format

[2019-04-25 11:09:27,943] INFO in test: app.logger

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.