// src/utils/logger.js import { sendToLoggingService } from '../services/LogService'; //…
- User
// src/utils/logger.js import { sendToLoggingService } from '../services/LogService';
// .environment("/project/.env") const LOG_LEVEL = process.env.REACT_APP_LOG_LEVEL || 'prod';
/**
-
App Logger
-
How to use:
-
import logger from './utils/Logger';
-
// args will be an array: ['User clicked button', userId, { additionalInfo: 'Some info' }].
-
logger.log('User clicked the button', userId);
-
logger.info('User clicked the button', userId);
-
logger.warn('User failed to:', userId);
-
logger.error('Could not save data:, userId);
-
Note: Use lowercase when you are referring to an instance of a logging utility or function that is
-
designed to log messages. This typically represents a regular object or function variable.
-
Example:
-
const logger = {
-
log: (...args) => { console.log(...args); }, -
// other logging methods -
}; */ const logger = {
log: (...args) => {
if (LOG_LEVEL === 'deve') { console.log(getTimestamp(), ...args); }}, info: (...args) => {
if (LOG_LEVEL === 'deve') { console.info(getTimestamp(), ...args); } if (LOG_LEVEL === 'prod') { sendToLoggingService('info', ...args); }}, warn: (...args) => {
if (LOG_LEVEL === 'deve') { console.warn(getTimestamp(), ...args); } if (LOG_LEVEL === 'prod') { sendToLoggingService('warn', ...args); }}, error: (...args) => {
if (LOG_LEVEL === 'deve') { console.error(getTimestamp(), ...args); } sendToLoggingService(level, ...args);}, };
const getTimestamp = () => {
const now = new Date(); return `(${now.toISOString()})`;};
export default logger;
what is the "level"
-
- Assistant
In the context of the provided
loggercode, thelevelvariable is used in theerrormethod when calling thesendToLoggingServicefunction. However, thelevelvariable has not been defined within theerrorfunction or globally, which will lead to a reference error when the function is executed.To fix this, you should replace
levelwith a string representing the log level. Changing the method to specify the level as'error'will make it clear:error: (...args) => { if (LOG_LEVEL === 'deve') { console.error(getTimestamp(), ...args); } sendToLoggingService('error', ...args); // Changed 'level' to 'error' },With this change, each log method now correctly uses the appropriate log level when sending data to the logging service. Here's the corrected portion of the
errormethod:error: (...args) => { if (LOG_LEVEL === 'deve') { console.error(getTimestamp(), ...args); } sendToLoggingService('error', ...args); // Use 'error' as the level },