NovichokJS,
так логичнее ...
const createLogger = () => {
let type = '';
let message = '';
let dateTime = '';
let arr = [];
function warn(messageOfWarn) {
type = 'warn';
message = messageOfWarn;
dateTime = new Date();
arr.push({type, message, dateTime});
}
function error(messageOfError) {
type = 'error';
message = messageOfError;
dateTime = new Date();
arr.push({type, message, dateTime});
}
function log(messageOfLog) {
type = 'log';
message = messageOfLog;
dateTime = new Date();
arr.push({type, message, dateTime});
}
function getRecords(type) {
return arr.filter(({type : e}) => e == type);
}
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 }]