Commit fe0e6f82 authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent 41394999
...@@ -20,3 +20,6 @@ Elasticsearch: ...@@ -20,3 +20,6 @@ Elasticsearch:
CaCerts: CaCerts:
Index: filebeat-ctocst_router-2021.11 Index: filebeat-ctocst_router-2021.11
Type: _doc Type: _doc
Logger:
Level: DEBUG
...@@ -10,35 +10,60 @@ ...@@ -10,35 +10,60 @@
# @Description : # @Description :
""" """
import os
import sys import sys
from flask import Flask, request, g from flask import Flask, request, g
from loguru import logger from loguru import logger
from .format import patching from .format import patching
from application.common import ConfigHelper
class FlaskLoguru: class FlaskLoguru:
def __init__(self, app=None): 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: if app is not None:
self.init_app(app) self.init_app(app)
def init_app(self, app: Flask) -> None: 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() 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) 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'): if not hasattr(app, 'extensions'):
app.extensions = {} app.extensions = {}
# Initialize the app's 'loguru' extension if it does not already exist
app.extensions.setdefault('loguru', {}) app.extensions.setdefault('loguru', {})
# Register this logger with the app's 'loguru' extension
app.extensions['loguru'][self] = logger app.extensions['loguru'][self] = logger
@app.before_request @app.before_request
def before_request(): def before_request():
"""
A function to run before each request.
Logs the start of the request.
"""
data = dict( data = dict(
url=request.url, url=request.url,
method=request.method, method=request.method,
...@@ -50,11 +75,19 @@ class FlaskLoguru: ...@@ -50,11 +75,19 @@ class FlaskLoguru:
@app.after_request @app.after_request
def after_request(response): def after_request(response):
"""
A function to run after each request.
Logs the completion of the request.
"""
g.logger.info('Request completed') g.logger.info('Request completed')
return response return response
@app.teardown_request @app.teardown_request
def teardown_request(exception=None): 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: if exception:
data = dict( data = dict(
exception=str(exception) exception=str(exception)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment