NovichokJS,
const createLogger = () => {
let type = '';
let message = '';
let arr = [];
function warn(messageOfWarn) {
type = 'warn';
message = messageOfWarn;
arr.push({type, message})
}
function error(messageOfError) {
type = 'error';
message = messageOfError;
arr.push({type, message})
}
function log(messageOfLog) {
type = 'log';
message = messageOfLog;
arr.push({type, message})
}
function getRecords(type) {
let a = arr.filter(({type : e}) => e == type);
a = a.map(o => (o.dateTime = new Date(), o));
return a;
}
return {
warn,
error,
log,
getRecords,
}
};
// examples
const logger1 = createLogger();
logger1.log('User logged in');
logger1.warn('User is tring to ented restricted page');
logger1.log('User logged out');
logger1.error('Unexpected error on the site');
console.log(logger1.getRecords('log')); // ===> [{ message: 'User logged out', type: 'log', dateTime: Date }, { message: 'User logged in', type: 'log', dateTime: Date }]