Files
FileHost/server/plugins/request-log.ts
T
O96u 901fefb3f5
GitHub Release / release (push) Canceled after 0s
ci / ci (22, ubuntu-latest) (push) Canceled after 0s
Publish Docker image / build (linux/amd64, ubuntu-latest, amd64) (push) Canceled after 0s
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Canceled after 0s
Publish Docker image / merge (push) Canceled after 0s
release: v1.1.1 — session fixes, storage cleanup, logging
2026-08-26 15:38:56 +08:00

43 lines
1.2 KiB
TypeScript

import { clientIp, logError, logInfo, logWarn } from '../utils/logger'
/**
* 只记录 /api/*,避免 /images、/twikoo 直链刷屏。
*/
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('request', (event) => {
event.context._reqStartedAt = Date.now()
})
nitroApp.hooks.hook('afterResponse', (event) => {
const path = getRequestURL(event).pathname
if (!path.startsWith('/api/')) return
// 高频探测接口降噪
if (path === '/api/auth/me' || path === '/api/auth/config') return
const method = getMethod(event)
const status = getResponseStatus(event)
const started = event.context._reqStartedAt as number | undefined
const ms = started ? Date.now() - started : undefined
const reason = status >= 400 ? getResponseStatusText(event) : undefined
const detail: Record<string, unknown> = {
status,
ms,
ip: clientIp(event)
}
if (reason && reason !== String(status)) {
detail.reason = reason
}
const line = `${method} ${path}`
if (status >= 500) {
logError(line, detail)
} else if (status >= 400) {
logWarn(line, detail)
} else {
logInfo(line, detail)
}
})
})