Add logging system based on winston

Add winston based logger for master, slave, ctl, lib, and web_ui code.
Logging done from slaves and instances are sent to the master server and
tagged with the instance and/or slave it originated from.  This allows
for example error messages to propagate back to the source that iniated
the action.  The master server also stores the log to allow it to be
queried at a later point.  For slaves a copy of the slave's log is also
stored locally.
This commit is contained in:
Hornwitser committed 2020-11-27 17:51:07 +01:00
1 parent 8f176c969b
commit 957983d8a5
47 files changed
+992 -513

No files matched your search

+6 -8
View File
@@ -3,23 +3,21 @@ const libPlugin = require("@clusterio/lib/plugin");
const libLuaTools = require("@clusterio/lib/lua_tools");
function unexpectedError(err) {
console.log("Unexpected error in research_sync");
console.log("---------------------------------");
console.log(err);
}
class InstancePlugin extends libPlugin.BaseInstancePlugin {
unexpectedError(err) {
this.logger.error(`Unexpected error:\n${err.stack}`);
}
async init() {
if (!this.instance.config.get("factorio.enable_save_patching")) {
throw new Error("research_sync plugin requires save patching.");
}
this.instance.server.on("ipc-research_sync:contribution", (tech) => {
this.researchContribution(tech).catch(unexpectedError);
this.researchContribution(tech).catch(err => this.unexpectedError(err));
});
this.instance.server.on("ipc-research_sync:finished", (tech) => {
this.researchFinished(tech).catch(unexpectedError);
this.researchFinished(tech).catch(err => this.unexpectedError(err));
});
}
+7 -7
View File
@@ -7,7 +7,7 @@ const libPlugin = require("@clusterio/lib/plugin");
class MasterPlugin extends libPlugin.BaseMasterPlugin {
async init() {
this.technologies = await loadTechnologies(this.master.config);
this.technologies = await loadTechnologies(this.master.config, this.logger);
this.lastProgressBroadcast = Date.now();
this.progressBroadcastId = null;
this.progressToBroadcast = new Set();
@@ -17,7 +17,7 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
if (this.progressBroadcastId) {
clearTimeout(this.progressBroadcastId);
}
await saveTechnologies(this.master.config, this.technologies);
await saveTechnologies(this.master.config, this.technologies, this.logger);
}
registerProgress() {
@@ -159,15 +159,15 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
}
}
async function loadTechnologies(masterConfig) {
async function loadTechnologies(masterConfig, logger) {
let filePath = path.join(masterConfig.get("master.database_directory"), "technologies.json");
console.log(`Loading ${filePath}`);
logger.verbose(`Loading ${filePath}`);
try {
return new Map(JSON.parse(await fs.readFile(filePath)));
} catch (err) {
if (err.code === "ENOENT") {
console.log("Creating new technologies database");
logger.verbose("Creating new technologies database");
return new Map();
} else {
@@ -176,9 +176,9 @@ async function loadTechnologies(masterConfig) {
}
}
async function saveTechnologies(masterConfig, technologies) {
async function saveTechnologies(masterConfig, technologies, logger) {
let filePath = path.join(masterConfig.get("master.database_directory"), "technologies.json");
console.log(`writing ${filePath}`);
logger.verbose(`writing ${filePath}`);
await fs.outputFile(filePath, JSON.stringify([...technologies.entries()], null, 4));
}