From fe0e6f825b1421baceb7f66df60ebc71d8cc04ba Mon Sep 17 00:00:00 2001 From: cuiweizhi <560397@gree.com.cn> Date: Sat, 4 Nov 2023 12:45:40 +0800 Subject: [PATCH] Update project --- application/config/config.yaml | 3 ++ application/libs/flask_loguru/logger.py | 43 ++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/application/config/config.yaml b/application/config/config.yaml index 7f867b4..5500744 100644 --- a/application/config/config.yaml +++ b/application/config/config.yaml @@ -20,3 +20,6 @@ Elasticsearch: CaCerts: Index: filebeat-ctocst_router-2021.11 Type: _doc + +Logger: + Level: DEBUG diff --git a/application/libs/flask_loguru/logger.py b/application/libs/flask_loguru/logger.py index fd7ff8b..0c32c6c 100644 --- a/application/libs/flask_loguru/logger.py +++ b/application/libs/flask_loguru/logger.py @@ -10,35 +10,60 @@ # @Description : """ -import os import sys from flask import Flask, request, g from loguru import logger from .format import patching +from application.common import ConfigHelper class FlaskLoguru: def __init__(self, app=None): + """ + Initialize the FlaskLoguru instance. + If an app is provided, also initialize that app for logging. + """ if app is not None: self.init_app(app) def init_app(self, app: Flask) -> None: - """Initialize the app""" + """ + Initialize the given app for logging with Loguru. + This involves setting up a new logger and registering it with the app. + """ + # Remove any existing handlers from the logger logger.remove() - # 获取日志级别 - level = app.config.Logger.Level if app.config.get('Logger') is not None else 'DEBUG' + + config_helper = ConfigHelper(app) + + # Fetch the logging configuration from the app's config + cfg = config_helper.Logger + + # Get the log level from the config, or default to 'DEBUG' if not provided + level = cfg.Level or 'DEBUG' + + # Add a new handler to the logger with the configured log level logger.add(sys.stderr, level=level, format=patching) - # 注册扩展 + + # Register this logger with the app + # If the app does not already have an 'extensions' attribute, add one if not hasattr(app, 'extensions'): app.extensions = {} + # Initialize the app's 'loguru' extension if it does not already exist app.extensions.setdefault('loguru', {}) + + # Register this logger with the app's 'loguru' extension app.extensions['loguru'][self] = logger @app.before_request def before_request(): + """ + A function to run before each request. + Logs the start of the request. + """ data = dict( url=request.url, method=request.method, @@ -50,11 +75,19 @@ class FlaskLoguru: @app.after_request def after_request(response): + """ + A function to run after each request. + Logs the completion of the request. + """ g.logger.info('Request completed') return response @app.teardown_request def teardown_request(exception=None): + """ + A function to run when ending each request, + either because the request was completed or because an error occurred. + """ if exception: data = dict( exception=str(exception) -- GitLab