fix: 日志系统修复
- userId 延迟到 finish 事件获取(authMiddleware 已执行) - 日志记录 UTC 时间戳,支持前端按用户时区显示 - 日期筛选和级别筛选 watch 监听修复 - 添加 showDatePicker 变量定义
This commit is contained in:
@@ -109,6 +109,7 @@ const logLines = ref<string[]>([])
|
|||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
const page = ref(1)
|
const page = ref(1)
|
||||||
const total = ref(0)
|
const total = ref(0)
|
||||||
|
const showDatePicker = ref(false)
|
||||||
|
|
||||||
const noMore = computed(() => logLines.value.length >= total.value && total.value > 0)
|
const noMore = computed(() => logLines.value.length >= total.value && total.value > 0)
|
||||||
|
|
||||||
@@ -173,6 +174,7 @@ function loadMore() {
|
|||||||
|
|
||||||
function selectDate(date: string) {
|
function selectDate(date: string) {
|
||||||
selectedDate.value = date
|
selectedDate.value = date
|
||||||
|
page.value = 1
|
||||||
loadLogs()
|
loadLogs()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +191,10 @@ function getLineClass(line: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 筛选条件变化时重新加载
|
// 筛选条件变化时重新加载
|
||||||
watch([filterLevel], () => loadLogs())
|
watch([filterLevel, selectedDate], () => {
|
||||||
|
page.value = 1
|
||||||
|
loadLogs()
|
||||||
|
})
|
||||||
|
|
||||||
function goBack() {
|
function goBack() {
|
||||||
uni.navigateBack()
|
uni.navigateBack()
|
||||||
|
|||||||
@@ -23,9 +23,11 @@ function formatTime(date: Date): string {
|
|||||||
function writeLog(level: string, message: string, meta?: Record<string, any>) {
|
function writeLog(level: string, message: string, meta?: Record<string, any>) {
|
||||||
const dateStr = getDateStr()
|
const dateStr = getDateStr()
|
||||||
const logFile = path.join(LOG_DIR, `${dateStr}.log`)
|
const logFile = path.join(LOG_DIR, `${dateStr}.log`)
|
||||||
const time = formatTime(new Date())
|
const now = new Date()
|
||||||
|
const time = formatTime(now)
|
||||||
|
const timestamp = now.toISOString() // 记录 UTC 时间戳,前端可按用户时区显示
|
||||||
const metaStr = meta ? ` ${JSON.stringify(meta)}` : ''
|
const metaStr = meta ? ` ${JSON.stringify(meta)}` : ''
|
||||||
const line = `[${time}] ${level} ${message}${metaStr}\n`
|
const line = `[${time}] [${timestamp}] ${level} ${message}${metaStr}\n`
|
||||||
|
|
||||||
fs.appendFile(logFile, line, (err) => {
|
fs.appendFile(logFile, line, (err) => {
|
||||||
if (err) console.error('[Logger] Write error:', err)
|
if (err) console.error('[Logger] Write error:', err)
|
||||||
@@ -37,13 +39,12 @@ export function requestLogger(req: Request, res: Response, next: NextFunction) {
|
|||||||
const startTime = Date.now()
|
const startTime = Date.now()
|
||||||
const { method, url, ip } = req
|
const { method, url, ip } = req
|
||||||
|
|
||||||
// 提取用户 ID(如果有 auth 中间件设置)
|
// 响应结束时记录日志(此时 authMiddleware 已执行,userId 可用)
|
||||||
const userId = (req as any).userId
|
|
||||||
|
|
||||||
// 响应结束时记录日志
|
|
||||||
res.on('finish', () => {
|
res.on('finish', () => {
|
||||||
const duration = Date.now() - startTime
|
const duration = Date.now() - startTime
|
||||||
const { statusCode } = res
|
const { statusCode } = res
|
||||||
|
// 延迟获取 userId,确保 authMiddleware 已执行
|
||||||
|
const userId = (req as any).userId
|
||||||
|
|
||||||
const meta = {
|
const meta = {
|
||||||
method,
|
method,
|
||||||
|
|||||||
Reference in New Issue
Block a user