Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
黄南光
/
wiki_knowledge
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit bf7c818d
authored
Jun 15, 2026
by
黄南光
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
思考回答加入日志处理
1 parent
a1a57512
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
69 additions
and
1 deletions
src/api/routes/query.py
src/database/models.py
src/database/sql/create_tables.sql
src/utils/logger.py
src/api/routes/query.py
View file @
bf7c818
This diff is collapsed.
Click to expand it.
src/database/models.py
View file @
bf7c818
...
...
@@ -50,3 +50,19 @@ class WikiOperationLog(Base):
wiki_file
=
relationship
(
"WikiFile"
,
back_populates
=
"logs"
)
class
WikiQueryLog
(
Base
):
__tablename__
=
'wiki_query_logs'
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
session_id
=
Column
(
String
(
100
),
nullable
=
True
,
index
=
True
)
query_text
=
Column
(
Text
,
nullable
=
False
)
history
=
Column
(
Text
,
nullable
=
True
)
# To store conversation history as JSON
answer_text
=
Column
(
Text
,
nullable
=
True
)
model_name
=
Column
(
String
(
50
),
nullable
=
True
)
total_tokens
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
duration_ms
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
status
=
Column
(
String
(
50
),
nullable
=
False
,
default
=
'Success'
)
# Success, Error
error_message
=
Column
(
Text
,
nullable
=
True
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
)
src/database/sql/create_tables.sql
View file @
bf7c818
...
...
@@ -39,3 +39,19 @@ CREATE TABLE IF NOT EXISTS `wiki_operation_logs` (
`created_at`
DATETIME
DEFAULT
CURRENT_TIMESTAMP
COMMENT
'操作发生时间'
,
INDEX
`idx_log_file_id`
(
`file_id`
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8mb4
COLLATE
=
utf8mb4_unicode_ci
COMMENT
=
'Wiki操作日志表'
;
-- 4. Wiki查询日志表 (记录用户查询与AI回答)
CREATE
TABLE
IF
NOT
EXISTS
`wiki_query_logs`
(
`id`
INT
AUTO_INCREMENT
PRIMARY
KEY
COMMENT
'自增主键'
,
`session_id`
VARCHAR
(
100
)
DEFAULT
NULL
COMMENT
'会话ID'
,
`query_text`
TEXT
NOT
NULL
COMMENT
'用户查询内容'
,
`history`
TEXT
DEFAULT
NULL
COMMENT
'对话历史(JSON)'
,
`answer_text`
TEXT
DEFAULT
NULL
COMMENT
'LLM生成的最终答案'
,
`model_name`
VARCHAR
(
50
)
DEFAULT
NULL
COMMENT
'所用模型名称'
,
`total_tokens`
INT
NOT
NULL
DEFAULT
0
COMMENT
'Token消耗量'
,
`duration_ms`
INT
NOT
NULL
DEFAULT
0
COMMENT
'处理耗时(毫秒)'
,
`status`
VARCHAR
(
50
)
NOT
NULL
DEFAULT
'Success'
COMMENT
'状态(Success/Error)'
,
`error_message`
TEXT
DEFAULT
NULL
COMMENT
'错误信息'
,
`created_at`
DATETIME
DEFAULT
CURRENT_TIMESTAMP
COMMENT
'查询时间'
,
INDEX
`idx_session_id`
(
`session_id`
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8mb4
COLLATE
=
utf8mb4_unicode_ci
COMMENT
=
'Wiki查询日志表'
;
src/utils/logger.py
View file @
bf7c818
import
os
import
logging
import
time
from
datetime
import
datetime
from
config
import
BASE_DIR
...
...
@@ -12,15 +13,46 @@ class IgnoreThinkingBlockWarning(logging.Filter):
return
True
class
MonthlyDailyFileHandler
(
logging
.
Handler
):
def
__init__
(
self
,
base_dir
=
"logs"
,
encoding
=
"utf-8"
):
def
__init__
(
self
,
base_dir
=
"logs"
,
encoding
=
"utf-8"
,
retain_days
=
14
):
super
()
.
__init__
()
self
.
base_dir
=
base_dir
self
.
encoding
=
encoding
self
.
retain_days
=
retain_days
self
.
current_date
=
None
self
.
file_handler
=
None
os
.
makedirs
(
self
.
base_dir
,
exist_ok
=
True
)
self
.
_get_file_handler
()
def
_clean_old_logs
(
self
):
"""清理超过保留天数的日志文件"""
if
self
.
retain_days
<=
0
:
return
now
=
time
.
time
()
# 遍历日志根目录
for
root
,
dirs
,
files
in
os
.
walk
(
self
.
base_dir
):
for
file
in
files
:
if
file
.
endswith
(
".log"
):
file_path
=
os
.
path
.
join
(
root
,
file
)
# 如果文件的最后修改时间早于 (当前时间 - 保留天数)
if
os
.
path
.
isfile
(
file_path
)
and
os
.
stat
(
file_path
)
.
st_mtime
<
now
-
self
.
retain_days
*
86400
:
try
:
os
.
remove
(
file_path
)
print
(
f
"已清理旧日志文件: {file_path}"
)
except
OSError
:
pass
# 尝试清理空的月份文件夹
for
dir_name
in
dirs
:
dir_path
=
os
.
path
.
join
(
root
,
dir_name
)
try
:
# 只有目录为空时 rmdir 才会成功
if
not
os
.
listdir
(
dir_path
):
os
.
rmdir
(
dir_path
)
print
(
f
"已清理空日志目录: {dir_path}"
)
except
OSError
:
pass
def
_get_file_handler
(
self
):
now
=
datetime
.
now
()
date_str
=
now
.
strftime
(
"
%
Y-
%
m-
%
d"
)
...
...
@@ -36,6 +68,10 @@ class MonthlyDailyFileHandler(logging.Handler):
if
self
.
formatter
:
self
.
file_handler
.
setFormatter
(
self
.
formatter
)
self
.
current_date
=
date_str
# 每天切换日志文件时,执行一次清理操作
self
.
_clean_old_logs
()
return
self
.
file_handler
def
setFormatter
(
self
,
fmt
):
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment