NovichokJS,
где-то надо хранить данные!!!
const createLogger = () => {
let type = '';
let message = ''
let obj = {}
function warn(messageOfWarn) {
type = 'warn';
message = messageOfWarn;
obj[type] = {type, message}
}
function error(messageOfError) {
type = 'error';
message = messageOfError;
obj[type] = {type, message};
}
function log(messageOfLog) {
type = 'log';
message = messageOfLog;
obj[type] = {type, message}
}
function getRecords(type) {
return [{ ...obj[type]??{type, message : ''}, dateTime: new Date() }];
}
return {
warn,
error,
log,
getRecords,
}
};
// examples
const logger1 = createLogger();
logger1.log('User logged in');
logger1.warn('User is tring to ented restricted page');
logger1.error('Unexpected error on the site');
console.log(logger1.getRecords('log')); // ===> [{ message: 'User logged in', type: 'log', dateTime: Date }]
console.log(logger1.getRecords('error')); // ===> [{ message: 'Unexpected error on the site', type: 'error', dateTime: Date }]
console.log(logger1.getRecords('warn')); // ===> [{ message: 'User is tring to ented restricted page', type: 'warn', dateTime: Date }]