tslog/common.ts
2025-02-05 11:24:37 +01:00

26 lines
No EOL
1.3 KiB
TypeScript

import { Dispatch } from "./Dispatch";
import { ILogItem } from "./ILogItem";
import { LogLevel } from "./LogLevel";
let CURRENT_DISPATCH: Dispatch = new class extends Dispatch {
process(item: ILogItem): void {
console.log(item.data);
}
};
export const setDispatch = (dispatch: Dispatch) => CURRENT_DISPATCH = dispatch;
export const LOGT = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Trace, data: args });
export const LOGD = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Debug, data: args });
export const LOGI = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Info, data: args });
export const LOGW = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Warn, data: args });
export const LOGE = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Error, data: args });
export const LOGF = (...args) => CURRENT_DISPATCH.process({ timestamp: new Date(), level: LogLevel.Fatal, data: args });
export const LOG = (item: Partial<ILogItem>) => CURRENT_DISPATCH.process({
flags: item.flags ?? [],
origin: item.origin ?? "unknown",
level: item.level ?? LogLevel.Trace,
timestamp: item.timestamp ?? new Date(),
data: item.data ?? ""
})