龙空技术网

Python项目中记录日志异常捕获loguru的简单实用

魔法小木瓜 261

前言:

此刻我们对“python 日志函数”都比较关怀,你们都想要了解一些“python 日志函数”的相关内容。那么小编在网摘上收集了一些有关“python 日志函数””的相关知识,希望姐妹们能喜欢,咱们一起来了解一下吧!

Python项目中记录日志及异常捕获

推荐使用 loguru,pip install loguru

Loguru是一个旨在以Python带来令人愉悦的日志记录的库。Loguru的主要概念是只有一个 logger。没有 Handler, 没有 Formatter, 没有Filter只有一个add函数。循环、保留、压缩、异常捕获、结构化日志、解析器、异步、线程安全、多线程安全...好处多多。

loguru旨在通过添加一系列有用的功能来解决标准记录器的注意事项,从而减少 Python 日志记录的痛苦。

详细可参考:

loguru基础功能测试

运行后,test.log不断增大,当达到设置的上限大小后,比如1 MB,则会按照其规则进行打包,例如:test.2022-09-07_02-48-34_016556.log.tar.gz

main.py

from loguru import logger    if __name__ == '__main__':	log_path_file = "./test.log"	logger.add(log_path_file,               # Automatically rotate too big file               # for example:"10 MB"、"0.5 GB"、"1 month 2 weeks"、"4 days"、"10h"、"18:00"...               rotation="1 MB",               # Cleanup after some time               # for example: "10 days"、"1 week, 3 days"、"2 months"、"1 minutes"...               retention="1 minutes",               encoding="utf-8",               # Asynchronous, Thread-safe, Multiprocess-safe               enqueue=True,               # Save some loved space               # for example: "gz", "bz2", "xz", "lzma", "tar", "tar.gz", "tar.bz2", "tar.xz", "zip".               compression="tar.gz")    # 日志滚动测试    while 1:      logger.debug("This's a log message")      logger.info("你好世界")      logger.error("hello world")
flask基础loguru实现日志记录及异常捕获

main.py

from flask import Flask, jsonify, request, gfrom flask_cors import *import os, traceback, timefrom loguru import loggerlog_path = f"../logs"if not os.path.exists(log_path):    os.makedirs(log_path)t = time.strftime("%Y_%m_%d")log_path_file = f"{log_path}/interface_log_{t}.log"def setup_logging():    logger.add(log_path_file,                rotation="10 MB",                retention="90 days",                encoding="utf-8",                enqueue=True,                level='INFO',                compression="tar.gz")setup_logging()app = Flask(__name__)app.config['JSON_AS_ASCII'] = FalseCORS(app, supports_cordentials=True)@app.before_requestdef before_request_middle_req():    try:        g.start_time = time.time()        url_method = request.method.upper()        url_str = request.url        if 'OPTIONS' == url_method:            return None        logger.info(f'(starting:)server receive,url={url_str},method={url_method}')    except Exception as e:        logger.error(f'请求异常:{e.__dict__},traceback={traceback.format_exc()}')        resultObj = {'code': 900, 'message': 'request error'}        return jsonify(resultObj)@app.after_requestdef after_request_middle_req(response):    url_str = request.url    url_method = request.method.upper()    if 'OPTIONS' == url_method:        return response    if "start_time" not in g:        return response    request_duration = round((time.time() - g.start_time) * 1000, 2)    logger.info(f"(statistics:)"                 f"method={url_method}"                 f" path={request.path} status={response.status_code}"                 f" duration={request_duration}ms")    return response@app.route('/test', methods=['POST', 'GET'])def test():    try:        resultObj = {'code': 0, 'message': 'success'}        return jsonify(resultObj)    except Exception as e:        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')        resultObj = {'code': 1000, 'message': 'Service running exception'}        return jsonify(resultObj)if __name__ == '__main__':    app.run(host='0.0.0.0', port=8080, debug=False)

标签: #python 日志函数 #python日志库 #python log10函数怎么打