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

+31 -23
View File
@@ -1,7 +1,8 @@
const libLink = require("@clusterio/lib/link");
const libPlugin = require("@clusterio/lib/plugin");
const libErrors = require("@clusterio/lib/errors");
const version = require("../../package.json").version;
import libLink from "@clusterio/lib/link";
import libPlugin from "@clusterio/lib/plugin";
import libErrors from "@clusterio/lib/errors";
import { logger } from "@clusterio/lib/logging";
import packageJson from "../../package.json";
/**
@@ -19,11 +20,11 @@ export class ControlConnector extends libLink.WebSocketClientConnector {
throw new Error("Token not set");
}
console.log("SOCKET | registering control");
logger.verbose("SOCKET | registering control");
this.sendHandshake("register_control", {
token: this.token,
agent: "Clusterio Web UI",
version,
version: packageJson.version,
});
}
}
@@ -48,41 +49,46 @@ export class Control extends libLink.Link {
libPlugin.attachPluginMessages(this, controlPlugin.info, controlPlugin);
}
this.instanceOutputHandlers = new Map();
this.instanceLogHandlers = new Map();
this.connector.on("connect", () => {
this.updateInstanceOutputSubscriptions().catch(console.error);
this.updateLogSubscriptions().catch(err => logger.error(
`Unexpected error updating log subscriptions:\n${err.stack}`
));
});
}
async instanceOutputEventHandler(message) {
let { instance_id, output } = message.data;
let handlers = this.instanceOutputHandlers.get(instance_id);
for (let handler of handlers || []) {
handler(output);
async logMessageEventHandler(message) {
let info = message.data.info;
if (info.instance_id !== undefined) {
let instanceHandlers = this.instanceLogHandlers.get(info.instance_id);
for (let handler of instanceHandlers || []) {
handler(info);
}
}
}
async onInstanceOutput(id, handler) {
async onInstanceLog(id, handler) {
if (!Number.isInteger(id)) {
throw new Error("Invalid instance id");
}
let handlers = this.instanceOutputHandlers.get(id);
let handlers = this.instanceLogHandlers.get(id);
if (!handlers) {
handlers = [];
this.instanceOutputHandlers.set(id, handlers);
this.instanceLogHandlers.set(id, handlers);
}
handlers.push(handler);
if (handlers.length === 1) {
await this.updateInstanceOutputSubscriptions();
await this.updateLogSubscriptions();
}
}
async offInstanceOutput(id, handler) {
let handlers = this.instanceOutputHandlers.get(id);
async offInstanceLog(id, handler) {
let handlers = this.instanceLogHandlers.get(id);
if (!handlers || !handlers.length) {
throw new Error(`No handlers for instance ${id} exist`);
}
@@ -94,17 +100,19 @@ export class Control extends libLink.Link {
handlers.splice(index, 1);
if (!handlers.length) {
await this.updateInstanceOutputSubscriptions();
await this.updateLogSubscriptions();
}
}
async updateInstanceOutputSubscriptions() {
await libLink.messages.setInstanceOutputSubscriptions.send(this, {
instance_ids: [...this.instanceOutputHandlers.keys()],
async updateLogSubscriptions() {
await libLink.messages.setLogSubscriptions.send(this, {
all: false,
instance_ids: [...this.instanceLogHandlers.keys()],
});
}
async debugWsMessageEventHandler(message) {
// eslint-disable-next-line no-console
console.log("WS", message.data.direction, message.data.content);
}