Added a logging utility class and supplemented, standardized the logging output for all modules.

This commit is contained in:
戒酒的李白
2025-01-27 18:09:30 +08:00
parent 1288b5f0be
commit c62f2b2a8e
13 changed files with 469 additions and 189 deletions
+39 -2
View File
@@ -3,11 +3,11 @@ from utils.mynlp import SnowNLP
from utils.getHomePageData import *
from utils.getHotWordPageData import *
from utils.getTableData import *
from utils.getPublicData import getAllHotWords, getAllTopics
from utils.getPublicData import getAllHotWords, getAllTopics, getArticleByType, getArticleById
from utils.getEchartsData import *
from utils.getTopicPageData import *
from utils.yuqingpredict import *
from utils.getPublicData import getAllHotWords
from utils.logger import app_logger as logging
pb = Blueprint('page',
__name__,
@@ -196,3 +196,40 @@ def yuqingpredict():
def articleCloud():
username = session.get('username')
return render_template('articleContentCloud.html', username=username)
@pb.route('/page/index')
def index():
"""首页路由"""
try:
hotWordList = getAllHotWords()
logging.info("成功获取热词列表")
return render_template('index.html', hotWordList=hotWordList)
except Exception as e:
logging.error(f"渲染首页时发生错误: {e}")
return render_template('error.html', error_message="加载首页失败")
@pb.route('/page/article/<type>')
def article(type):
"""文章列表页路由"""
try:
articleList = getArticleByType(type)
logging.info(f"成功获取类型为 {type} 的文章列表")
return render_template('article.html', articleList=articleList)
except Exception as e:
logging.error(f"获取文章列表时发生错误: {e}")
return render_template('error.html', error_message="加载文章列表失败")
@pb.route('/page/articleChar/<id>')
def articleChar(id):
"""文章详情页路由"""
try:
article = getArticleById(id)
if not article:
logging.warning(f"未找到ID为 {id} 的文章")
return render_template('error.html', error_message="文章不存在")
logging.info(f"成功获取ID为 {id} 的文章详情")
return render_template('articleChar.html', article=article)
except Exception as e:
logging.error(f"获取文章详情时发生错误: {e}")
return render_template('error.html', error_message="加载文章详情失败")
+36 -15
View File
@@ -4,6 +4,7 @@ from flask import Blueprint, redirect, render_template, request, Flask, session
from utils.query import query
from utils.errorResponse import errorResponse
from utils.logger import app_logger as logging
ub = Blueprint('user',
__name__,
@@ -31,21 +32,29 @@ def login():
if request.method == 'GET':
return render_template('login_and_register.html') # 显示登录页面
# 提取表单数据
username = request.form.get('username', '').strip()
password = hash_password(request.form.get('password', '').strip())
# 查询用户信息
user_query = 'SELECT * FROM user WHERE username = %s AND password = %s'
users = query(user_query, [username, password], 'select')
if not users:
# 登录失败,返回登录页面并显示错误信息
return render_template('login_and_register.html', error='账号或密码错误', username=username)
# 登录成功,设置会话并重定向
session['username'] = username
return redirect('/page/home')
try:
username = request.form.get('username')
password = request.form.get('password')
if not username or not password:
logging.warning("登录失败:用户名或密码为空")
return render_template('login_and_register.html', msg='用户名和密码不能为空')
# 查询用户
sql = "SELECT * FROM user WHERE username = %s AND password = %s"
result = query(sql, [username, password], "select")
if result:
session['username'] = username
logging.info(f"用户 {username} 登录成功")
return redirect('/page/home')
else:
logging.warning(f"用户 {username} 登录失败:用户名或密码错误")
return render_template('login_and_register.html', msg='用户名或密码错误')
except Exception as e:
logging.error(f"登录过程发生错误: {e}")
return render_template('login_and_register.html', msg='登录失败,请稍后重试')
@ub.route('/register', methods=['GET', 'POST'])
@@ -82,3 +91,15 @@ def register():
def logOut():
session.clear()
return redirect('/user/login')
@ub.route('/user/logout')
def logout():
"""用户登出"""
try:
username = session.get('username')
session.clear()
logging.info(f"用户 {username} 成功登出")
return redirect('/user/login')
except Exception as e:
logging.error(f"登出过程发生错误: {e}")
return redirect('/user/login')