use better structured logging

This commit is contained in:
Evan
2024-12-17 20:34:15 -08:00
parent 94992e1144
commit 113518a96e
4 changed files with 57 additions and 17 deletions
+27 -11
View File
@@ -1,17 +1,33 @@
import { LogSeverity } from "../core/Schemas";
import { ClientID, GameID, LogSeverity } from "../core/Schemas";
export interface slogMsg {
logKey: string,
msg: string,
data?: any
severity?: LogSeverity
gameID?: GameID
clientID?: ClientID
persistentID?: string
}
export function slog(msg: slogMsg): void {
msg.severity = msg.severity ?? LogSeverity.Info;
export function slog(eventType: string, description, data: any, severity = LogSeverity.Info): void {
const logEntry = {
eventType: eventType,
description: description,
severity: severity,
data: data
};
if (process.env.GAME_ENV == 'dev') {
if (severity != LogSeverity.Debug) {
console.log(description)
// Avoid blowing up the log during development.
if (msg.logKey == 'client_console_log') {
return
}
if (msg.severity != LogSeverity.Debug) {
console.log(msg.msg)
}
} else {
console.log(JSON.stringify(logEntry));
try {
console.log(JSON.stringify(msg));
} catch (error) {
console.error('Failed to stringify log message:', error);
// Fallback to basic logging
console.log(`${msg.severity}: ${msg.msg}`);
}
}
}