This commit is contained in:
1 parent
8efd032cc6
commit
df38dccbf1
735 files changed
+128321
-120870
No files matched your search
+36
-26
@@ -1,41 +1,51 @@
|
||||
type LogScalar = string | number | boolean | null;
|
||||
type LogContext = Record<string, unknown>;
|
||||
|
||||
const SENSITIVE_KEY = /(authorization|cookie|password|secret|token|api[-_]?key)/i;
|
||||
const SENSITIVE_KEY =
|
||||
/(authorization|cookie|password|secret|token|api[-_]?key)/i;
|
||||
|
||||
export interface ServerErrorRecord {
|
||||
level: "error";
|
||||
event: string;
|
||||
message: string;
|
||||
context: Record<string, LogScalar>;
|
||||
level: "error";
|
||||
event: string;
|
||||
message: string;
|
||||
context: Record<string, LogScalar>;
|
||||
}
|
||||
|
||||
export function serverErrorRecord(
|
||||
event: string,
|
||||
error: unknown,
|
||||
context: LogContext = {},
|
||||
event: string,
|
||||
error: unknown,
|
||||
context: LogContext = {},
|
||||
): ServerErrorRecord {
|
||||
return {
|
||||
level: "error",
|
||||
event,
|
||||
message: error instanceof Error ? error.message.slice(0, 500) : "Unknown server error",
|
||||
context: Object.fromEntries(
|
||||
Object.entries(context).map(([key, value]) => [
|
||||
key,
|
||||
SENSITIVE_KEY.test(key) ? "[REDACTED]" : logScalar(value),
|
||||
]),
|
||||
),
|
||||
};
|
||||
return {
|
||||
level: "error",
|
||||
event,
|
||||
message:
|
||||
error instanceof Error
|
||||
? error.message.slice(0, 500)
|
||||
: "Unknown server error",
|
||||
context: Object.fromEntries(
|
||||
Object.entries(context).map(([key, value]) => [
|
||||
key,
|
||||
SENSITIVE_KEY.test(key) ? "[REDACTED]" : logScalar(value),
|
||||
]),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
export function logServerError(event: string, error: unknown, context: LogContext = {}): void {
|
||||
console.error(JSON.stringify(serverErrorRecord(event, error, context)));
|
||||
export function logServerError(
|
||||
event: string,
|
||||
error: unknown,
|
||||
context: LogContext = {},
|
||||
): void {
|
||||
console.error(JSON.stringify(serverErrorRecord(event, error, context)));
|
||||
}
|
||||
|
||||
function logScalar(value: unknown): LogScalar {
|
||||
return typeof value === "string" || typeof value === "number" || typeof value === "boolean"
|
||||
? value
|
||||
: value === null
|
||||
? null
|
||||
: "[NON_SCALAR]";
|
||||
return typeof value === "string" ||
|
||||
typeof value === "number" ||
|
||||
typeof value === "boolean"
|
||||
? value
|
||||
: value === null
|
||||
? null
|
||||
: "[NON_SCALAR]";
|
||||
}
|
||||
Reference in new issue
Block a user