From aa27858a9531d7bbe187a8303c4d56da1072d1f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E4=B8=BA=E4=B9=8B?= <560397@gree.com.cn> Date: Fri, 3 Nov 2023 11:46:42 +0800 Subject: [PATCH] Update project --- application/config/config.yaml | 8 ++++---- application/lib/flask_loguru/format.py | 2 ++ application/lib/flask_loguru/logger.py | 15 ++++++++++----- application/utils/dsn/dsn.py | 4 ++-- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/application/config/config.yaml b/application/config/config.yaml index 4831032..6c967a5 100644 --- a/application/config/config.yaml +++ b/application/config/config.yaml @@ -2,8 +2,8 @@ System: Env: public MySQL: - USER: root - PASSWORD: localhost123 - HOST: localhost - PORT: 3306 + User: root + Password: localhost123 + Host: localhost + Port: 3306 DB: elp diff --git a/application/lib/flask_loguru/format.py b/application/lib/flask_loguru/format.py index ca5fe95..a24227c 100644 --- a/application/lib/flask_loguru/format.py +++ b/application/lib/flask_loguru/format.py @@ -14,6 +14,7 @@ import json def serialize(record: dict) -> str: + """Customize logger format""" time_stamp = record["time"] time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S") subset = { @@ -27,5 +28,6 @@ def serialize(record: dict) -> str: def patching(record: dict) -> str: + """Set the patch""" record["extra"]["serialized"] = serialize(record) return "{extra[serialized]}\n" diff --git a/application/lib/flask_loguru/logger.py b/application/lib/flask_loguru/logger.py index 6b015d5..fd7ff8b 100644 --- a/application/lib/flask_loguru/logger.py +++ b/application/lib/flask_loguru/logger.py @@ -10,6 +10,7 @@ # @Description : """ +import os import sys from flask import Flask, request, g @@ -23,14 +24,18 @@ class FlaskLoguru: if app is not None: self.init_app(app) - def init_app(self, app: Flask): + def init_app(self, app: Flask) -> None: + """Initialize the app""" logger.remove() - logger.add(sys.stderr, format=patching) - if not hasattr(app, "extensions"): + # 获取日志级别 + level = app.config.Logger.Level if app.config.get('Logger') is not None else 'DEBUG' + logger.add(sys.stderr, level=level, format=patching) + # 注册扩展 + if not hasattr(app, 'extensions'): app.extensions = {} - app.extensions.setdefault("loguru", {}) - app.extensions["loguru"][self] = logger + app.extensions.setdefault('loguru', {}) + app.extensions['loguru'][self] = logger @app.before_request def before_request(): diff --git a/application/utils/dsn/dsn.py b/application/utils/dsn/dsn.py index 0f7b544..87937cf 100644 --- a/application/utils/dsn/dsn.py +++ b/application/utils/dsn/dsn.py @@ -23,5 +23,5 @@ def dsn(app: Flask) -> str: mysql_cfg = app.config.get('MySQL') if mysql_cfg is None: raise KeyError('Key MySQL error') - return (f'mysql+pymysql://{app.config.MySQL.USER}:' - f'{app.config.MySQL.PASSWORD}@{app.config.MySQL.HOST}:{app.config.MySQL.PORT}/{app.config.MySQL.DB}') + return (f'mysql+pymysql://{app.config.MySQL.User}:' + f'{app.config.MySQL.Password}@{app.config.MySQL.Host}:{app.config.MySQL.Port}/{app.config.MySQL.DB}') -- GitLab