2 changed files with 45 additions and 13 deletions
@ -1,18 +1,46 @@ |
|||||
|
import { logToDb } from "../db"; |
||||
|
|
||||
|
function safeStringify(obj: unknown) { |
||||
|
const seen = new WeakSet(); |
||||
|
|
||||
|
return JSON.stringify(obj, (key, value) => { |
||||
|
if (typeof value === "object" && value !== null) { |
||||
|
if (seen.has(value)) { |
||||
|
return "[Circular]"; |
||||
|
} |
||||
|
seen.add(value); |
||||
|
} |
||||
|
|
||||
|
if (typeof value === "function") { |
||||
|
return `[Function: ${value.name || "anonymous"}]`; |
||||
|
} |
||||
|
|
||||
|
return value; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
export const logger = { |
export const logger = { |
||||
log: (message: string, ...args: unknown[]) => { |
log: (message: string, ...args: unknown[]) => { |
||||
if (process.env.NODE_ENV !== "production") { |
if (process.env.NODE_ENV !== "production") { |
||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||
console.log(message, ...args); |
console.log(message, ...args); |
||||
|
const argsString = args.length > 0 ? " - " + safeStringify(args) : ""; |
||||
|
logToDb(message + argsString); |
||||
} |
} |
||||
}, |
}, |
||||
warn: (message: string, ...args: unknown[]) => { |
warn: (message: string, ...args: unknown[]) => { |
||||
if (process.env.NODE_ENV !== "production") { |
if (process.env.NODE_ENV !== "production") { |
||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||
console.warn(message, ...args); |
console.warn(message, ...args); |
||||
|
const argsString = args.length > 0 ? " - " + safeStringify(args) : ""; |
||||
|
logToDb(message + argsString); |
||||
} |
} |
||||
}, |
}, |
||||
error: (message: string, ...args: unknown[]) => { |
error: (message: string, ...args: unknown[]) => { |
||||
|
// Errors will always be logged
|
||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||
console.error(message, ...args); // Errors should always be logged
|
console.error(message, ...args); |
||||
|
const argsString = args.length > 0 ? " - " + safeStringify(args) : ""; |
||||
|
logToDb(message + argsString); |
||||
}, |
}, |
||||
}; |
}; |
||||
|
Loading…
Reference in new issue