headplane/server/utils/log.ts

62 lines
1.6 KiB
TypeScript
Raw Normal View History

import {
hp_getSingleton,
hp_getSingletonUnsafe,
hp_setSingleton,
} from '~server/context/global';
export interface Logger {
info: (category: string, message: string, ...args: unknown[]) => void;
warn: (category: string, message: string, ...args: unknown[]) => void;
error: (category: string, message: string, ...args: unknown[]) => void;
debug: (category: string, message: string, ...args: unknown[]) => void;
}
export function hp_loadLogger(debug: boolean) {
const newLog = { ...log };
2025-02-19 18:09:42 -05:00
if (debug) {
newLog.debug = (category: string, message: string, ...args: unknown[]) => {
2025-02-19 18:09:42 -05:00
defaultLog('DEBG', category, message, ...args);
};
newLog.info('CFGX', 'Debug logging enabled');
newLog.info(
2025-02-19 18:09:42 -05:00
'CFGX',
'This is very verbose and should only be used for debugging purposes',
);
newLog.info(
2025-02-19 18:09:42 -05:00
'CFGX',
'If you run this in production, your storage COULD fill up quickly',
);
}
hp_setSingleton('logger', newLog);
}
function defaultLog(
level: string,
category: string,
message: string,
...args: unknown[]
) {
const date = new Date().toISOString();
console.log(`${date} (${level}) [${category}] ${message}`, ...args);
2025-02-19 18:09:42 -05:00
}
const log = {
2025-01-17 08:33:22 +00:00
info: (category: string, message: string, ...args: unknown[]) => {
defaultLog('INFO', category, message, ...args);
},
warn: (category: string, message: string, ...args: unknown[]) => {
defaultLog('WARN', category, message, ...args);
},
error: (category: string, message: string, ...args: unknown[]) => {
defaultLog('ERRO', category, message, ...args);
},
2025-02-19 18:09:42 -05:00
debug: (category: string, message: string, ...args: unknown[]) => {},
2025-01-17 08:33:22 +00:00
};
export default hp_getSingletonUnsafe('logger') ?? log;