mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
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.
95 lines
1.9 KiB
JavaScript
95 lines
1.9 KiB
JavaScript
"use strict";
|
|
const winston = require("winston");
|
|
const Transport = require("winston-transport");
|
|
const { LEVEL, MESSAGE } = require("triple-beam");
|
|
|
|
const levels = Object.freeze({
|
|
fatal: 0,
|
|
error: 1,
|
|
warn: 2,
|
|
audit: 3,
|
|
info: 4,
|
|
server: 5,
|
|
verbose: 6,
|
|
});
|
|
|
|
const colors = {
|
|
fatal: "inverse red",
|
|
error: "bold brightRed",
|
|
warn: "bold brightYellow",
|
|
audit: "bold brightGreen",
|
|
info: "bold brightBlue",
|
|
server: "bold",
|
|
verbose: "bold grey",
|
|
};
|
|
winston.addColors(colors);
|
|
|
|
// The Console Transport is replicated here because it's overly complicated
|
|
// in winston, writes to stdout/err instead of console and doesn't work in
|
|
// the browser.
|
|
class ConsoleTransport extends Transport {
|
|
constructor(options = {}) {
|
|
super(options);
|
|
this.errorLevels = new Set(options.errorLevels || ["fatal", "error"]);
|
|
this.warnLevels = new Set(options.warnLevels || ["warn"]);
|
|
}
|
|
|
|
log(info, callback) {
|
|
/* eslint-disable no-console */
|
|
if (this.errorLevels.has(info[LEVEL])) {
|
|
console.error(info[MESSAGE]);
|
|
} else if (this.warnLevels.has(info[LEVEL])) {
|
|
console.warn(info[MESSAGE]);
|
|
} else {
|
|
console.log(info[MESSAGE]);
|
|
}
|
|
/* eslint-enable no-console */
|
|
callback();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Formats winston log messages for the web console
|
|
*/
|
|
class WebConsoleFormat {
|
|
constructor(options = {}) {
|
|
this.options = options;
|
|
}
|
|
|
|
transform(info, options) {
|
|
let src = " ";
|
|
if (info.slave_id) {
|
|
let slaveName = info.slave_id;
|
|
src += `s:${slaveName} - `;
|
|
}
|
|
if (info.instance_id) {
|
|
let instanceName = info.instance_id;
|
|
src += `i:${instanceName}`;
|
|
}
|
|
if (info.plugin) {
|
|
src += `${info.plugin}: `;
|
|
}
|
|
|
|
if (info.stack) {
|
|
info[MESSAGE] = `[${info.level}]${src}${info.stack}`;
|
|
} else {
|
|
info[MESSAGE] = `[${info.level}]${src}${info.message}`;
|
|
}
|
|
return info;
|
|
}
|
|
}
|
|
|
|
const logger = winston.createLogger({
|
|
level: "verbose",
|
|
levels,
|
|
format: winston.format.timestamp(),
|
|
});
|
|
|
|
module.exports = {
|
|
ConsoleTransport,
|
|
WebConsoleFormat,
|
|
colors,
|
|
levels,
|
|
logger,
|
|
};
|