0.3.0-0-pre.5
This commit is contained in:
45
Web/game-snake-poc/frontend/ts/logging.ts
Normal file
45
Web/game-snake-poc/frontend/ts/logging.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
// 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);
|
||||
}
|
||||
Reference in New Issue
Block a user