dsql-admin/dsql-app/package-shared/utils/logging/debug-log.ts
Benjamin Toby e95f4d1087 Updates
2025-02-16 17:12:40 +01:00

61 lines
1.4 KiB
TypeScript

import { ccol } from "../console-colors";
const LogTypes = ["error", "warning"] as const;
type Param = {
/**
* data to be logged.
*/
log: any;
/**
* Log Title. Could be name of function or name of variable
*/
title?: string;
/**
* Label for the log
*/
label?: string;
/**
* Log type. `error` or `warning` or default
*/
type?: (typeof LogTypes)[number];
/**
* Whether to add a time stamp
*/
addTime?: boolean;
};
export default function debugLog({ log, label, title, type, addTime }: Param) {
const logType = (() => {
switch (type) {
case "error":
return ccol.FgRed;
case "warning":
return ccol.FgYellow;
default:
return ccol.FgGreen;
}
})();
let logTxt = `${logType}DEBUG${ccol.Reset}:::`;
const date = new Date();
const time = date.toLocaleTimeString("en-US", {
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: true,
});
const logTime = `${date.toLocaleDateString()}][${time}`;
if (addTime) logTxt = `${ccol.BgWhite}[${logTime}]${ccol.Reset} ` + logTxt;
if (title) logTxt += `${ccol.FgBlue}${title}${ccol.Reset}::`;
if (label)
logTxt += `${ccol.FgWhite}${ccol.Bright}${label}${ccol.Reset} =>`;
console.log(logTxt, log);
}