之前有写一个logging模块的使用方法的笔记:https://sulao.cn/post/615.html,其实在flask中也是使用的这个模块,我们一起来看看简单的使用方法
#!/usr/bin/python3 #coding:utf-8 from flask import Flask,request import logging app = Flask(__name__) @app.route('/') def index(): app.logger.info('this is a info ') app.logger.debug('this debug mode !') app.logger.warning('this is a warning !') app.logger.error('this is a error !') return request.url if __name__ == "__main__": logging.basicConfig(filename="logs/app.log",format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') app.run(debug=True)
具体详细用法可以参考我原来的笔记,我们来看看app.log下打印的日志
flask应用工程配置
def configure_logger(app): log_dir_name = "logs" log_file_name = 'logger-' + time.strftime('%Y-%m-%d', time.localtime(time.time())) + '.log' log_file_str = log_dir_name + os.sep + log_file_name log_level = logging.WARNING handler = logging.FileHandler(log_file_str, encoding='UTF-8') handler.setLevel(log_level) logging_format = logging.Formatter( '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s') handler.setFormatter(logging_format) app.logger.addHandler(handler)