之前有写一个logging模块的使用方法的笔记:https://sulao.cn/post/612,其实在flask中也是使用的这个模块,我们一起来看看简单的使用方法
01.#!/usr/bin/python302.#coding:utf-803.from flask import Flask,request04.import logging05.app = Flask(__name__)06. 07.@app.route('/')08.def index():09. app.logger.info('this is a info ')10. app.logger.debug('this debug mode !')11. app.logger.warning('this is a warning !')12. app.logger.error('this is a error !')13. return request.url14. 15.if __name__ == "__main__":16. logging.basicConfig(filename="logs/app.log",format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')17. app.run(debug=True)
具体详细用法可以参考我原来的笔记,我们来看看app.log下打印的日志
flask应用工程配置
01.def configure_logger(app):02. log_dir_name = "logs"03. log_file_name = 'logger-' + time.strftime('%Y-%m-%d', time.localtime(time.time())) + '.log'04. log_file_str = log_dir_name + os.sep + log_file_name05. log_level = logging.WARNING06. handler = logging.FileHandler(log_file_str, encoding='UTF-8')07. handler.setLevel(log_level)08. logging_format = logging.Formatter(09. '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')10. handler.setFormatter(logging_format)11. app.logger.addHandler(handler)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://sulao.cn/post/633
评论列表