diff --git a/src/shared/lib/audit-logger.ts b/src/shared/lib/audit-logger.ts index 69f425f..7cfc948 100644 --- a/src/shared/lib/audit-logger.ts +++ b/src/shared/lib/audit-logger.ts @@ -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 { try { @@ -40,7 +44,10 @@ export async function logAudit(params: LogAuditParams): Promise { 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" + ) } } diff --git a/src/shared/lib/change-logger.ts b/src/shared/lib/change-logger.ts index a7db148..7c4325f 100644 --- a/src/shared/lib/change-logger.ts +++ b/src/shared/lib/change-logger.ts @@ -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 { try { @@ -37,7 +41,10 @@ export async function logDataChange(params: LogDataChangeParams): Promise 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" + ) } } diff --git a/src/shared/lib/login-logger.ts b/src/shared/lib/login-logger.ts index ceb841d..f4583ec 100644 --- a/src/shared/lib/login-logger.ts +++ b/src/shared/lib/login-logger.ts @@ -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 { try { @@ -36,7 +40,10 @@ export async function logLoginEvent(params: LogLoginEventParams): Promise 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" + ) } }