Skip to content

Instantly share code, notes, and snippets.

@gloriaJun
Last active November 23, 2022 06:47
Show Gist options
  • Save gloriaJun/b3117461bdc4a326d82b40a4658cf98a to your computer and use it in GitHub Desktop.
Save gloriaJun/b3117461bdc4a326d82b40a4658cf98a to your computer and use it in GitHub Desktop.
logger
function getCallerName(error) {
return error.stack?.split('\n')?.[2].trim().split(' ')[1];
}
function Logger() {
const caller = getCallerName(new Error());
console.log('caller : ', caller);
}
function FuncA() {
Logger();
}
function FuncB() {
Logger();
}
function FuncC() {
return () => {
Logger();
}
}
FuncA(); // caller : FuncA
FuncB(); // caller : FuncB
FuncC()(); // caller : <anonymous>:20:7
function getCallerName(error: Error) {
return error.stack?.split('\n')?.[2].trim().split(' ')[1];
}
export const logger = {
group: (
title: string,
callbacks: Array<() => void>,
{ color = 'green', bgColor = 'yellow' } = {} as {
color?: string;
bgColor?: string;
},
) => {
console.group(
`%c${title}`,
`color: ${color};background: ${bgColor}; font-weight: bold;`,
);
callbacks.map((cb) => cb());
console.groupEnd();
},
table: (data: unknown) => {
console.table(data);
},
debug: (...args: Array<unknown>) => {
const caller = getCallerName(new Error());
const value = [...args].map((v) => JSON.stringify(v)).join(' ');
console.log(`%c[${caller}] ${value}`, 'color: grey');
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment