46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
// file: Web/game-snake-poc/frontend/ts/logging.ts
|
|
// version: 1
|
|
|
|
export type WebLogLevel = "debug" | "info" | "warn" | "error";
|
|
export type WebLogFields = Readonly<Record<string, boolean | number | string | null>>;
|
|
|
|
type ConsoleMethod = (...items: unknown[]) => void;
|
|
|
|
function consoleMethod(level: WebLogLevel): ConsoleMethod {
|
|
if (level === "debug") {
|
|
return console.debug.bind(console);
|
|
}
|
|
if (level === "info") {
|
|
return console.info.bind(console);
|
|
}
|
|
if (level === "warn") {
|
|
return console.warn.bind(console);
|
|
}
|
|
return console.error.bind(console);
|
|
}
|
|
|
|
function emit(level: WebLogLevel, target: string, action: string, fields: WebLogFields = {}): void {
|
|
const event = {
|
|
target,
|
|
action,
|
|
...fields,
|
|
};
|
|
consoleMethod(level)(`[games.sasedev][${target}] ${action}`, event);
|
|
}
|
|
|
|
export function webDebug(target: string, action: string, fields: WebLogFields = {}): void {
|
|
emit("debug", target, action, fields);
|
|
}
|
|
|
|
export function webInfo(target: string, action: string, fields: WebLogFields = {}): void {
|
|
emit("info", target, action, fields);
|
|
}
|
|
|
|
export function webWarn(target: string, action: string, fields: WebLogFields = {}): void {
|
|
emit("warn", target, action, fields);
|
|
}
|
|
|
|
export function webError(target: string, action: string, fields: WebLogFields = {}): void {
|
|
emit("error", target, action, fields);
|
|
}
|