keeweb/app/scripts/util/logger.js

85 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const Level = {
2016-04-09 14:55:27 +02:00
Off: 0,
Error: 1,
Warn: 2,
Info: 3,
Debug: 4,
All: 5
};
2017-01-31 07:50:28 +01:00
const MaxLogsToSave = 100;
2016-06-04 10:31:06 +02:00
2017-01-31 07:50:28 +01:00
const lastLogs = [];
2016-06-04 10:31:06 +02:00
2020-06-01 16:53:51 +02:00
const Logger = function (name, id, level = Level.All) {
2019-08-16 23:05:39 +02:00
this.prefix = name ? name + (id ? ':' + id : '') : 'default';
2019-10-12 08:20:44 +02:00
this.level = level;
2015-12-12 09:53:50 +01:00
};
2020-06-01 16:53:51 +02:00
Logger.prototype.ts = function (ts) {
2015-12-12 09:53:50 +01:00
if (ts) {
return Math.round(performance.now() - ts) + 'ms';
} else {
return performance.now();
}
};
2020-06-01 16:53:51 +02:00
Logger.prototype.getPrefix = function () {
2015-12-12 09:53:50 +01:00
return new Date().toISOString() + ' [' + this.prefix + '] ';
};
2020-06-01 16:53:51 +02:00
Logger.prototype.debug = function (...args) {
2019-08-18 10:17:09 +02:00
args[0] = this.getPrefix() + args[0];
2018-03-24 21:17:20 +01:00
if (this.level >= Level.Debug) {
2019-08-18 10:17:09 +02:00
Logger.saveLast('debug', args);
console.log(...args); // eslint-disable-line no-console
2016-04-09 14:55:27 +02:00
}
2015-12-12 09:53:50 +01:00
};
2020-06-01 16:53:51 +02:00
Logger.prototype.info = function (...args) {
2019-08-18 10:17:09 +02:00
args[0] = this.getPrefix() + args[0];
2018-03-24 21:17:20 +01:00
if (this.level >= Level.Info) {
2019-08-18 10:17:09 +02:00
Logger.saveLast('info', args);
console.info(...args); // eslint-disable-line no-console
2016-04-09 14:55:27 +02:00
}
2015-12-12 09:53:50 +01:00
};
2020-06-01 16:53:51 +02:00
Logger.prototype.warn = function (...args) {
2019-08-18 10:17:09 +02:00
args[0] = this.getPrefix() + args[0];
2018-03-24 21:17:20 +01:00
if (this.level >= Level.Warn) {
2019-08-18 10:17:09 +02:00
Logger.saveLast('warn', args);
console.warn(...args); // eslint-disable-line no-console
2016-04-09 14:55:27 +02:00
}
2015-12-12 09:53:50 +01:00
};
2020-06-01 16:53:51 +02:00
Logger.prototype.error = function (...args) {
2019-08-18 10:17:09 +02:00
args[0] = this.getPrefix() + args[0];
2018-03-24 21:17:20 +01:00
if (this.level >= Level.Error) {
2019-08-18 10:17:09 +02:00
Logger.saveLast('error', args);
console.error(...args); // eslint-disable-line no-console
2016-04-09 14:55:27 +02:00
}
2015-12-12 09:53:50 +01:00
};
2020-06-01 16:53:51 +02:00
Logger.prototype.setLevel = function (level) {
2016-04-09 14:55:27 +02:00
this.level = level;
};
2020-06-01 16:53:51 +02:00
Logger.prototype.getLevel = function () {
2016-04-09 14:55:27 +02:00
return this.level;
};
2020-06-01 16:53:51 +02:00
Logger.saveLast = function (level, args) {
2019-08-18 10:17:09 +02:00
lastLogs.push({ level, args: Array.prototype.slice.call(args) });
2016-06-04 10:31:06 +02:00
if (lastLogs.length > MaxLogsToSave) {
lastLogs.shift();
}
};
2020-06-01 16:53:51 +02:00
Logger.getLast = function () {
2016-06-04 10:31:06 +02:00
return lastLogs;
};
2016-04-09 14:55:27 +02:00
Logger.Level = Level;
2019-09-15 14:16:32 +02:00
export { Logger };