User logger in node files

This commit is contained in:
rejas
2020-05-11 07:25:42 +02:00
parent 367233c318
commit f2d03a511e
5 changed files with 60 additions and 54 deletions

View File

@@ -10,12 +10,13 @@ var Utils = require(__dirname + "/utils.js");
var defaultModules = require(__dirname + "/../modules/default/defaultmodules.js");
var path = require("path");
var Log = require("./logger.js");
// Alias modules mentioned in package.js under _moduleAliases.
require("module-alias/register");
// Get version number.
global.version = JSON.parse(fs.readFileSync("package.json", "utf8")).version;
console.log("Starting MagicMirror: v" + global.version);
Log.log("Starting MagicMirror: v" + global.version);
// global absolute root path
global.root_path = path.resolve(__dirname + "/../");
@@ -33,10 +34,10 @@ if (process.env.MM_PORT) {
// The next part is here to prevent a major exception when there
// is no internet connection. This could probable be solved better.
process.on("uncaughtException", function (err) {
console.log("Whoops! There was an uncaught exception...");
console.error(err);
console.log("MagicMirror will not quit, but it might be a good idea to check why this happened. Maybe no internet connection?");
console.log("If you think this really is an issue, please open an issue on GitHub: https://github.com/MichMich/MagicMirror/issues");
Log.log("Whoops! There was an uncaught exception...");
Log.error(err);
Log.log("MagicMirror will not quit, but it might be a good idea to check why this happened. Maybe no internet connection?");
Log.log("If you think this really is an issue, please open an issue on GitHub: https://github.com/MichMich/MagicMirror/issues");
});
/* App - The core app.
@@ -50,8 +51,8 @@ var App = function () {
*
* argument callback function - The callback function.
*/
var loadConfig = function (callback) {
console.log("Loading config ...");
var loadConfig = function(callback) {
Log.log("Loading config ...");
var defaults = require(__dirname + "/defaults.js");
// For this check proposed to TestSuite
@@ -69,11 +70,11 @@ var App = function () {
callback(config);
} catch (e) {
if (e.code === "ENOENT") {
console.error(Utils.colors.error("WARNING! Could not find config file. Please create one. Starting with default configuration."));
Log.error(Utils.colors.error("WARNING! Could not find config file. Please create one. Starting with default configuration."));
} else if (e instanceof ReferenceError || e instanceof SyntaxError) {
console.error(Utils.colors.error("WARNING! Could not validate config file. Starting with default configuration. Please correct syntax errors at or above this line: " + e.stack));
Log.error(Utils.colors.error("WARNING! Could not validate config file. Starting with default configuration. Please correct syntax errors at or above this line: " + e.stack));
} else {
console.error(Utils.colors.error("WARNING! Could not load config file. Starting with default configuration. Error found: " + e));
Log.error(Utils.colors.error("WARNING! Could not load config file. Starting with default configuration. Error found: " + e));
}
callback(defaults);
}
@@ -91,7 +92,11 @@ var App = function () {
}
});
if (usedDeprecated.length > 0) {
console.warn(Utils.colors.warn("WARNING! Your config is using deprecated options: " + usedDeprecated.join(", ") + ". Check README and CHANGELOG for more up-to-date ways of getting the same functionality."));
Log.warn(Utils.colors.warn(
"WARNING! Your config is using deprecated options: " +
usedDeprecated.join(", ") +
". Check README and CHANGELOG for more up-to-date ways of getting the same functionality.")
);
}
};
@@ -116,7 +121,7 @@ var App = function () {
fs.accessSync(helperPath, fs.R_OK);
} catch (e) {
loadModule = false;
console.log("No helper found for module: " + moduleName + ".");
Log.log("No helper found for module: " + moduleName + ".");
}
if (loadModule) {
@@ -124,11 +129,11 @@ var App = function () {
var m = new Module();
if (m.requiresVersion) {
console.log("Check MagicMirror version for node helper '" + moduleName + "' - Minimum version: " + m.requiresVersion + " - Current version: " + global.version);
Log.log("Check MagicMirror version for node helper '" + moduleName + "' - Minimum version: " + m.requiresVersion + " - Current version: " + global.version);
if (cmpVersions(global.version, m.requiresVersion) >= 0) {
console.log("Version is ok!");
Log.log("Version is ok!");
} else {
console.log("Version is incorrect. Skip module: '" + moduleName + "'");
Log.log("Version is incorrect. Skip module: '" + moduleName + "'");
return;
}
}
@@ -148,8 +153,8 @@ var App = function () {
*
* argument module string - The name of the module (including subpath).
*/
var loadModules = function (modules, callback) {
console.log("Loading module helpers ...");
var loadModules = function(modules, callback) {
Log.log("Loading module helpers ...");
var loadNextModule = function () {
if (modules.length > 0) {
@@ -160,7 +165,7 @@ var App = function () {
});
} else {
// All modules are loaded
console.log("All module helpers loaded.");
Log.log("All module helpers loaded.");
callback();
}
};
@@ -210,9 +215,9 @@ var App = function () {
}
}
loadModules(modules, function () {
var server = new Server(config, function (app, io) {
console.log("Server started ...");
loadModules(modules, function() {
var server = new Server(config, function(app, io) {
Log.log("Server started ...");
for (var h in nodeHelpers) {
var nodeHelper = nodeHelpers[h];
@@ -221,7 +226,7 @@ var App = function () {
nodeHelper.start();
}
console.log("Sockets connected & modules started ...");
Log.log("Sockets connected & modules started ...");
if (typeof callback === "function") {
callback(config);
@@ -252,10 +257,8 @@ var App = function () {
* this.stop() is called by app.on("before-quit"... in `electron.js`
*/
process.on("SIGINT", () => {
console.log("[SIGINT] Received. Shutting down server...");
setTimeout(() => {
process.exit(0);
}, 3000); // Force quit after 3 seconds
Log.log("[SIGINT] Received. Shutting down server...");
setTimeout(() => { process.exit(0); }, 3000); // Force quit after 3 seconds
this.stop();
process.exit(0);
});
@@ -263,10 +266,8 @@ var App = function () {
/* We also need to listen to SIGTERM signals so we stop everything when we are asked to stop by the OS.
*/
process.on("SIGTERM", () => {
console.log("[SIGTERM] Received. Shutting down server...");
setTimeout(() => {
process.exit(0);
}, 3000); // Force quit after 3 seconds
Log.log("[SIGTERM] Received. Shutting down server...");
setTimeout(() => { process.exit(0); }, 3000); // Force quit after 3 seconds
this.stop();
process.exit(0);
});