mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
CommandOutput.error takes a human reporter like result, so command errors log their message in human mode and print JSON in json and wire mode. table takes a map applied to each row in human mode, which note list, host plugin list, user list and save list now use. Rename streaming to streamingLog, and print startup errors with errorToJson directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
3.4 KiB
TypeScript
94 lines
3.4 KiB
TypeScript
import winston from "winston";
|
|
|
|
import * as lib from "@clusterio/lib";
|
|
import { ConsoleTransport, levels, logger } from "@clusterio/lib";
|
|
import type { Ctl } from "../ctl.js";
|
|
|
|
export const logCommands = new lib.CommandTree({ name: "log", description: "Log inspection" });
|
|
logCommands.add(new lib.Command({
|
|
definition: ["follow", "follow cluster log", (yargs) => {
|
|
yargs.options({
|
|
"all": { describe: "Follow the whole cluster log", nargs: 0, type: "boolean", default: false },
|
|
"controller": { describe: "Follow log of the controller", nargs: 0, type: "boolean", default: false },
|
|
"host": { describe: "Follow log of given host", nargs: 1, type: "string", default: null },
|
|
"instance": { describe: "Follow log of given instance", nargs: 1, type: "string", default: null },
|
|
});
|
|
}],
|
|
handler: async function(
|
|
args: { all: boolean, controller: boolean, host: string | null, instance: string | null },
|
|
ctl: Ctl
|
|
) {
|
|
if (!args.all && !args.controller && !args.host && !args.instance) {
|
|
logger.error("At least one of --all, --controller, --host and --instance must be passed");
|
|
process.exitCode = 1;
|
|
return;
|
|
}
|
|
let instanceIds = args.instance ? [await lib.resolveInstance(ctl, args.instance)] : [];
|
|
let hostIds = args.host ? [await lib.resolveHost(ctl, args.host)] : [];
|
|
await ctl.setLogSubscriptions({ all: args.all, controller: args.controller, hostIds, instanceIds });
|
|
ctl.keepOpen = true;
|
|
ctl.output.streamingLog = true;
|
|
},
|
|
}));
|
|
|
|
logCommands.add(new lib.Command({
|
|
definition: ["query", "Query cluster log", (yargs) => {
|
|
yargs.options({
|
|
"all": { describe: "Query the whole cluster log", nargs: 0, type: "boolean", default: false },
|
|
"controller": { describe: "Query log of the controller", nargs: 0, type: "boolean", default: false },
|
|
"host": { describe: "Query log of given host", nargs: 1, type: "string", default: null },
|
|
"instance": { describe: "Query log of given instance", nargs: 1, type: "string", default: null },
|
|
"max-level": { describe: "Maximum log level to return", nargs: 1, type: "string", default: undefined },
|
|
"limit": { describe: "Max number of entries to return", nargs: 1, type: "number", default: 1000 },
|
|
"start": { describe: "Limit from the start instead of the end", nargs: 0, type: "boolean", default: false },
|
|
});
|
|
}],
|
|
handler: async function(
|
|
args: {
|
|
all: boolean,
|
|
controller: boolean,
|
|
host: string | null,
|
|
instance: string | null,
|
|
maxLevel?: string,
|
|
limit: number,
|
|
start: boolean,
|
|
},
|
|
ctl: Ctl
|
|
) {
|
|
if (!args.all && !args.controller && !args.host && !args.instance) {
|
|
logger.error("At least one of --all, --controller, --host and --instance must be passed");
|
|
process.exitCode = 1;
|
|
return;
|
|
}
|
|
let instanceIds = args.instance ? [await lib.resolveInstance(ctl, args.instance)] : [];
|
|
let hostIds = args.host ? [await lib.resolveHost(ctl, args.host)] : [];
|
|
let result = await ctl.send(new lib.LogQueryRequest(
|
|
args.all,
|
|
args.controller,
|
|
hostIds,
|
|
instanceIds,
|
|
args.maxLevel as keyof typeof levels,
|
|
args.limit,
|
|
args.start ? "asc" : "desc",
|
|
));
|
|
|
|
if (!args.start) {
|
|
result.log.reverse();
|
|
}
|
|
|
|
ctl.output.result(result.log, () => {
|
|
let stdoutLogger = winston.createLogger({
|
|
level: "verbose",
|
|
levels,
|
|
format: new lib.TerminalFormat({ showTimestamp: true }),
|
|
transports: [
|
|
new ConsoleTransport({ errorLevels: [], warnLevels: [] }),
|
|
],
|
|
});
|
|
for (let info of result.log) {
|
|
stdoutLogger.log(info as any);
|
|
}
|
|
});
|
|
},
|
|
}));
|