fix(logging): replace silent audit-logger failures with logger.warn

This commit is contained in:
SpecialX
2026-07-07 12:09:38 +08:00
parent a75fdcd60d
commit fefc65702d
3 changed files with 30 additions and 9 deletions

View File

@@ -5,6 +5,9 @@ import { db } from "@/shared/db"
import { auditLogs } from "@/shared/db/schema"
import { getSession } from "@/shared/lib/session"
import { resolveClientIp, getUserAgent } from "@/shared/lib/http-utils"
import { createModuleLogger } from "@/shared/lib/logger"
const log = createModuleLogger("audit-logger")
export type AuditLogStatus = "success" | "failure"
@@ -19,7 +22,8 @@ export interface LogAuditParams {
/**
* Record an audit log entry for the current authenticated user.
* Silently fails on error so it never breaks the main operation.
*
* Note: 失败时记录到 logger.warn 而非静默吞没,确保运维可感知审计写入失败。
*/
export async function logAudit(params: LogAuditParams): Promise<void> {
try {
@@ -40,7 +44,10 @@ export async function logAudit(params: LogAuditParams): Promise<void> {
userAgent,
status: params.status ?? "success",
})
} catch {
// Silently fail - logging should not break the main operation
} catch (error) {
log.warn(
{ err: error, action: params.action, module: params.module },
"Audit log write failed"
)
}
}

View File

@@ -6,6 +6,9 @@ import { db } from "@/shared/db"
import { dataChangeLogs } from "@/shared/db/schema"
import { getSession } from "@/shared/lib/session"
import { resolveClientIp } from "@/shared/lib/http-utils"
import { createModuleLogger } from "@/shared/lib/logger"
const log = createModuleLogger("change-logger")
export type DataChangeAction = "create" | "update" | "delete"
@@ -19,7 +22,8 @@ export interface LogDataChangeParams {
/**
* Record a data change log entry for the current authenticated user.
* Silently fails on error so it never blocks the main operation.
*
* Note: 失败时记录到 logger.warn 而非静默吞没,确保运维可感知变更日志写入失败。
*/
export async function logDataChange(params: LogDataChangeParams): Promise<void> {
try {
@@ -37,7 +41,10 @@ export async function logDataChange(params: LogDataChangeParams): Promise<void>
changedByName: session?.user?.name ?? "unknown",
ipAddress,
})
} catch {
// Silently fail - change logging must not break the main operation
} catch (error) {
log.warn(
{ err: error, tableName: params.tableName, recordId: params.recordId },
"Change log write failed"
)
}
}

View File

@@ -4,6 +4,9 @@ import { createId } from "@paralleldrive/cuid2"
import { db } from "@/shared/db"
import { loginLogs } from "@/shared/db/schema"
import { resolveClientIp, getUserAgent } from "@/shared/lib/http-utils"
import { createModuleLogger } from "@/shared/lib/logger"
const log = createModuleLogger("login-logger")
export type LoginLogAction = "signin" | "signout" | "signup"
export type LoginLogStatus = "success" | "failure"
@@ -19,7 +22,8 @@ export interface LogLoginEventParams {
/**
* Record a login-related log entry (signin/signout/signup).
* Does NOT depend on auth context since it runs during auth events.
* Silently fails on error so it never breaks the auth flow.
*
* Note: 失败时记录到 logger.warn 而非静默吞没,确保运维可感知登录日志写入失败。
*/
export async function logLoginEvent(params: LogLoginEventParams): Promise<void> {
try {
@@ -36,7 +40,10 @@ export async function logLoginEvent(params: LogLoginEventParams): Promise<void>
userAgent,
errorMessage: params.errorMessage ?? null,
})
} catch {
// Silently fail - logging should not break the auth flow
} catch (error) {
log.warn(
{ err: error, action: params.action, userEmail: params.userEmail },
"Login log write failed"
)
}
}