67 lines
1.5 KiB
JavaScript
67 lines
1.5 KiB
JavaScript
/**
|
|
* Production-safe logging utility
|
|
*
|
|
* In production (NODE_ENV=production), logs are disabled by default
|
|
* to prevent exposing sensitive information in browser console.
|
|
*
|
|
* In development, all logs are shown for debugging.
|
|
*
|
|
* Usage:
|
|
* import logger from '../utils/logger';
|
|
* logger.log('[Component]', 'message', data);
|
|
* logger.error('[Component]', 'error message', error);
|
|
* logger.warn('[Component]', 'warning message');
|
|
*/
|
|
|
|
const isDevelopment = process.env.NODE_ENV === 'development';
|
|
|
|
// Force enable logs with REACT_APP_DEBUG_LOGS=true in .env
|
|
const debugEnabled = process.env.REACT_APP_DEBUG_LOGS === 'true';
|
|
|
|
const shouldLog = isDevelopment || debugEnabled;
|
|
|
|
const logger = {
|
|
log: (...args) => {
|
|
if (shouldLog) {
|
|
console.log(...args);
|
|
}
|
|
},
|
|
|
|
error: (...args) => {
|
|
// Always log errors, but sanitize in production
|
|
if (shouldLog) {
|
|
console.error(...args);
|
|
} else {
|
|
// In production, only log error type without details
|
|
console.error('An error occurred. Enable debug logs for details.');
|
|
}
|
|
},
|
|
|
|
warn: (...args) => {
|
|
if (shouldLog) {
|
|
console.warn(...args);
|
|
}
|
|
},
|
|
|
|
info: (...args) => {
|
|
if (shouldLog) {
|
|
console.info(...args);
|
|
}
|
|
},
|
|
|
|
debug: (...args) => {
|
|
if (shouldLog) {
|
|
console.debug(...args);
|
|
}
|
|
},
|
|
|
|
// Special method for sensitive data - NEVER logs in production
|
|
sensitive: (...args) => {
|
|
if (isDevelopment) {
|
|
console.log('[SENSITIVE]', ...args);
|
|
}
|
|
}
|
|
};
|
|
|
|
export default logger;
|