tslog/common.ts
2025-02-13 11:35:07 +01:00

26 lines
No EOL
1.4 KiB
TypeScript

import { Dispatch } from "./Dispatch";
import { ILogItem } from "./ILogItem";
import { LogLevel } from "./LogLevel";
let CURRENT_DISPATCH: Dispatch = new class extends Dispatch {
output(item: ILogItem): void {
process.stdout.write(`level{${item.level}} timestamp{${item.timestamp}} origin{${item.origin}} flags{${item.flags}} data{${item.data}}\n`)
}
};
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 ?? ""
})