refactor: changement de la philosophie de logging

This commit is contained in:
Luc SORIGNET
2025-02-22 15:28:20 +01:00
parent 508847940c
commit c7723eceee
28 changed files with 217 additions and 161 deletions

View File

@ -0,0 +1,39 @@
const getCallerInfo = () => {
const stackLine = new Error().stack?.split('\n')[3].trim();
// Regex pour extraire le fichier, la ligne et la colonne
const match = stackLine.match(/\(?([^)]+):(\d+):(\d+)\)?$/);
let callerInfo = '(unknown)';
if (match) {
const [ , filePath, line, column ] = match;
const fileName = filePath.split('/').pop(); // Garde juste le nom du fichier
callerInfo = `[${fileName}:${line}]`;
}
return callerInfo;
}
const logger = {
debug: (...args) => {
if (process.env.NODE_ENV !== 'production') {
console.log.apply(console, ['[DEBUG]',`${getCallerInfo()}`, ...args])
}
},
error: (...args) => {
// Les erreurs sont toujours loguées
console.error.apply(console,['[ERROR]',`${getCallerInfo()}`,...args]);
},
warn: (...args) => {
if (process.env.NODE_ENV !== 'production') {
console.warn.apply(console, ['[WARN]',`${getCallerInfo()}`, ...args]);
}
},
info: (...args) => {
if (process.env.NODE_ENV !== 'production') {
console.info.apply(console, ['[INFO]',`${getCallerInfo()}`, ...args]);
}
}
};
export default logger;