MagicMirror/js/logger.js

55 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-03-24 17:19:32 +01:00
/* Magic Mirror
2020-06-01 16:40:20 +02:00
* Log
*
2020-05-03 18:59:26 +02:00
* This logger is very simple, but needs to be extended.
* This system can eventually be used to push the log messages to an external target.
2016-03-24 17:19:32 +01:00
*
2020-04-28 23:05:28 +02:00
* By Michael Teeuw https://michaelteeuw.nl
2016-03-24 17:19:32 +01:00
* MIT Licensed.
*/
2020-05-11 07:22:40 +02:00
(function (root, factory) {
2020-05-25 18:57:15 +02:00
if (typeof exports === "object") {
2020-05-11 07:23:21 +02:00
// add timestamps in front of log messages
require("console-stamp")(console, {
pattern: "yyyy-mm-dd HH:MM:ss.l",
include: ["debug", "log", "info", "warn", "error"]
});
2020-05-11 07:23:21 +02:00
2020-05-11 07:22:40 +02:00
// Node, CommonJS-like
module.exports = factory(root.config);
} else {
// Browser globals (root is window)
root.Log = factory(root.config);
}
2020-05-25 18:57:15 +02:00
})(this, function (config) {
2021-06-06 23:13:09 +02:00
let logLevel = {
debug: Function.prototype.bind.call(console.debug, console),
log: Function.prototype.bind.call(console.log, console),
info: Function.prototype.bind.call(console.info, console),
warn: Function.prototype.bind.call(console.warn, console),
error: Function.prototype.bind.call(console.error, console),
group: Function.prototype.bind.call(console.group, console),
groupCollapsed: Function.prototype.bind.call(console.groupCollapsed, console),
groupEnd: Function.prototype.bind.call(console.groupEnd, console),
time: Function.prototype.bind.call(console.time, console),
2021-06-06 23:13:09 +02:00
timeEnd: Function.prototype.bind.call(console.timeEnd, console)
};
if (process.env.NODE_ENV.trim() !== "test") {
logLevel.push({timeStamp: Function.prototype.bind.call(console.timeStamp, console)});
2020-05-25 18:57:15 +02:00
};
2020-05-11 07:46:56 +02:00
2020-07-04 22:02:39 +02:00
logLevel.setLogLevel = function (newLevel) {
if (newLevel) {
Object.keys(logLevel).forEach(function (key, index) {
if (!newLevel.includes(key.toLocaleUpperCase())) {
logLevel[key] = function () {};
}
});
}
};
2020-05-11 07:46:56 +02:00
return logLevel;
2020-05-25 18:57:15 +02:00
});