fix(logging): replace silent audit-logger failures with logger.warn
This commit is contained in:
@@ -5,6 +5,9 @@ import { db } from "@/shared/db"
|
|||||||
import { auditLogs } from "@/shared/db/schema"
|
import { auditLogs } from "@/shared/db/schema"
|
||||||
import { getSession } from "@/shared/lib/session"
|
import { getSession } from "@/shared/lib/session"
|
||||||
import { resolveClientIp, getUserAgent } from "@/shared/lib/http-utils"
|
import { resolveClientIp, getUserAgent } from "@/shared/lib/http-utils"
|
||||||
|
import { createModuleLogger } from "@/shared/lib/logger"
|
||||||
|
|
||||||
|
const log = createModuleLogger("audit-logger")
|
||||||
|
|
||||||
export type AuditLogStatus = "success" | "failure"
|
export type AuditLogStatus = "success" | "failure"
|
||||||
|
|
||||||
@@ -19,7 +22,8 @@ export interface LogAuditParams {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Record an audit log entry for the current authenticated user.
|
* 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> {
|
export async function logAudit(params: LogAuditParams): Promise<void> {
|
||||||
try {
|
try {
|
||||||
@@ -40,7 +44,10 @@ export async function logAudit(params: LogAuditParams): Promise<void> {
|
|||||||
userAgent,
|
userAgent,
|
||||||
status: params.status ?? "success",
|
status: params.status ?? "success",
|
||||||
})
|
})
|
||||||
} catch {
|
} catch (error) {
|
||||||
// Silently fail - logging should not break the main operation
|
log.warn(
|
||||||
|
{ err: error, action: params.action, module: params.module },
|
||||||
|
"Audit log write failed"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import { db } from "@/shared/db"
|
|||||||
import { dataChangeLogs } from "@/shared/db/schema"
|
import { dataChangeLogs } from "@/shared/db/schema"
|
||||||
import { getSession } from "@/shared/lib/session"
|
import { getSession } from "@/shared/lib/session"
|
||||||
import { resolveClientIp } from "@/shared/lib/http-utils"
|
import { resolveClientIp } from "@/shared/lib/http-utils"
|
||||||
|
import { createModuleLogger } from "@/shared/lib/logger"
|
||||||
|
|
||||||
|
const log = createModuleLogger("change-logger")
|
||||||
|
|
||||||
export type DataChangeAction = "create" | "update" | "delete"
|
export type DataChangeAction = "create" | "update" | "delete"
|
||||||
|
|
||||||
@@ -19,7 +22,8 @@ export interface LogDataChangeParams {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Record a data change log entry for the current authenticated user.
|
* 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> {
|
export async function logDataChange(params: LogDataChangeParams): Promise<void> {
|
||||||
try {
|
try {
|
||||||
@@ -37,7 +41,10 @@ export async function logDataChange(params: LogDataChangeParams): Promise<void>
|
|||||||
changedByName: session?.user?.name ?? "unknown",
|
changedByName: session?.user?.name ?? "unknown",
|
||||||
ipAddress,
|
ipAddress,
|
||||||
})
|
})
|
||||||
} catch {
|
} catch (error) {
|
||||||
// Silently fail - change logging must not break the main operation
|
log.warn(
|
||||||
|
{ err: error, tableName: params.tableName, recordId: params.recordId },
|
||||||
|
"Change log write failed"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import { createId } from "@paralleldrive/cuid2"
|
|||||||
import { db } from "@/shared/db"
|
import { db } from "@/shared/db"
|
||||||
import { loginLogs } from "@/shared/db/schema"
|
import { loginLogs } from "@/shared/db/schema"
|
||||||
import { resolveClientIp, getUserAgent } from "@/shared/lib/http-utils"
|
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 LoginLogAction = "signin" | "signout" | "signup"
|
||||||
export type LoginLogStatus = "success" | "failure"
|
export type LoginLogStatus = "success" | "failure"
|
||||||
@@ -19,7 +22,8 @@ export interface LogLoginEventParams {
|
|||||||
/**
|
/**
|
||||||
* Record a login-related log entry (signin/signout/signup).
|
* Record a login-related log entry (signin/signout/signup).
|
||||||
* Does NOT depend on auth context since it runs during auth events.
|
* 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> {
|
export async function logLoginEvent(params: LogLoginEventParams): Promise<void> {
|
||||||
try {
|
try {
|
||||||
@@ -36,7 +40,10 @@ export async function logLoginEvent(params: LogLoginEventParams): Promise<void>
|
|||||||
userAgent,
|
userAgent,
|
||||||
errorMessage: params.errorMessage ?? null,
|
errorMessage: params.errorMessage ?? null,
|
||||||
})
|
})
|
||||||
} catch {
|
} catch (error) {
|
||||||
// Silently fail - logging should not break the auth flow
|
log.warn(
|
||||||
|
{ err: error, action: params.action, userEmail: params.userEmail },
|
||||||
|
"Login log write failed"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user