Last active
November 23, 2022 06:47
-
-
Save gloriaJun/b3117461bdc4a326d82b40a4658cf98a to your computer and use it in GitHub Desktop.
logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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