2023-01-13 05:40:33 +01:00
|
|
|
import cluster from "node:cluster";
|
|
|
|
import chalk from "chalk";
|
|
|
|
import { default as convertColor } from "color-convert";
|
|
|
|
import { format as dateFormat } from "date-fns";
|
|
|
|
import { envOption } from "../env.js";
|
|
|
|
import config from "@/config/index.js";
|
2019-08-30 01:29:46 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
import * as SyslogPro from "syslog-pro";
|
2019-03-02 10:51:59 +01:00
|
|
|
|
|
|
|
type Domain = {
|
|
|
|
name: string;
|
2019-04-12 18:43:22 +02:00
|
|
|
color?: string;
|
2019-03-02 10:51:59 +01:00
|
|
|
};
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
type Level = "error" | "success" | "warning" | "debug" | "info";
|
2019-03-02 10:51:59 +01:00
|
|
|
|
|
|
|
export default class Logger {
|
|
|
|
private domain: Domain;
|
2019-04-12 18:43:22 +02:00
|
|
|
private parentLogger: Logger | null = null;
|
2019-03-02 10:51:59 +01:00
|
|
|
private store: boolean;
|
2019-08-30 01:29:46 +02:00
|
|
|
private syslogClient: any | null = null;
|
2019-03-02 10:51:59 +01:00
|
|
|
|
|
|
|
constructor(domain: string, color?: string, store = true) {
|
|
|
|
this.domain = {
|
|
|
|
name: domain,
|
|
|
|
color: color,
|
|
|
|
};
|
|
|
|
this.store = store;
|
2019-08-30 01:29:46 +02:00
|
|
|
|
|
|
|
if (config.syslog) {
|
|
|
|
this.syslogClient = new SyslogPro.RFC5424({
|
2023-01-13 05:40:33 +01:00
|
|
|
applacationName: "Calckey",
|
2019-08-30 01:29:46 +02:00
|
|
|
timestamp: true,
|
|
|
|
encludeStructuredData: true,
|
|
|
|
color: true,
|
|
|
|
extendedColor: true,
|
|
|
|
server: {
|
|
|
|
target: config.syslog.host,
|
|
|
|
port: config.syslog.port,
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2019-08-30 01:29:46 +02:00
|
|
|
});
|
|
|
|
}
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public createSubLogger(domain: string, color?: string, store = true): Logger {
|
|
|
|
const logger = new Logger(domain, color, store);
|
|
|
|
logger.parentLogger = this;
|
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
private log(
|
|
|
|
level: Level,
|
|
|
|
message: string,
|
|
|
|
data?: Record<string, any> | null,
|
|
|
|
important = false,
|
|
|
|
subDomains: Domain[] = [],
|
|
|
|
store = true,
|
|
|
|
): void {
|
2021-10-08 14:24:05 +02:00
|
|
|
if (envOption.quiet) return;
|
2019-03-02 10:51:59 +01:00
|
|
|
if (!this.store) store = false;
|
2023-01-13 05:40:33 +01:00
|
|
|
if (level === "debug") store = false;
|
2019-03-02 10:51:59 +01:00
|
|
|
|
|
|
|
if (this.parentLogger) {
|
2023-01-13 05:40:33 +01:00
|
|
|
this.parentLogger.log(
|
|
|
|
level,
|
|
|
|
message,
|
|
|
|
data,
|
|
|
|
important,
|
|
|
|
[this.domain].concat(subDomains),
|
|
|
|
store,
|
|
|
|
);
|
2019-03-02 10:51:59 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const time = dateFormat(new Date(), "HH:mm:ss");
|
|
|
|
const worker = cluster.isPrimary ? "*" : cluster.worker.id;
|
2019-03-02 10:51:59 +01:00
|
|
|
const l =
|
2023-01-13 05:40:33 +01:00
|
|
|
level === "error"
|
|
|
|
? important
|
|
|
|
? chalk.bgRed.white("ERR ")
|
|
|
|
: chalk.red("ERR ")
|
|
|
|
: level === "warning"
|
|
|
|
? chalk.yellow("WARN")
|
|
|
|
: level === "success"
|
|
|
|
? important
|
|
|
|
? chalk.bgGreen.white("DONE")
|
|
|
|
: chalk.green("DONE")
|
|
|
|
: level === "debug"
|
|
|
|
? chalk.gray("VERB")
|
|
|
|
: level === "info"
|
|
|
|
? chalk.blue("INFO")
|
|
|
|
: null;
|
|
|
|
const domains = [this.domain]
|
|
|
|
.concat(subDomains)
|
|
|
|
.map((d) =>
|
|
|
|
d.color
|
|
|
|
? chalk.rgb(...convertColor.keyword.rgb(d.color))(d.name)
|
|
|
|
: chalk.white(d.name),
|
|
|
|
);
|
2019-03-02 10:51:59 +01:00
|
|
|
const m =
|
2023-01-13 05:40:33 +01:00
|
|
|
level === "error"
|
|
|
|
? chalk.red(message)
|
|
|
|
: level === "warning"
|
|
|
|
? chalk.yellow(message)
|
|
|
|
: level === "success"
|
|
|
|
? chalk.green(message)
|
|
|
|
: level === "debug"
|
|
|
|
? chalk.gray(message)
|
|
|
|
: level === "info"
|
|
|
|
? message
|
|
|
|
: null;
|
|
|
|
|
|
|
|
let log = `${l} ${worker}\t[${domains.join(" ")}]\t${m}`;
|
|
|
|
if (envOption.withLogTime) log = `${chalk.gray(time)} ${log}`;
|
2019-03-02 10:51:59 +01:00
|
|
|
|
|
|
|
console.log(important ? chalk.bold(log) : log);
|
|
|
|
|
|
|
|
if (store) {
|
2019-08-30 01:29:46 +02:00
|
|
|
if (this.syslogClient) {
|
|
|
|
const send =
|
2023-01-13 05:40:33 +01:00
|
|
|
level === "error"
|
|
|
|
? this.syslogClient.error
|
|
|
|
: level === "warning"
|
|
|
|
? this.syslogClient.warning
|
|
|
|
: level === "success"
|
|
|
|
? this.syslogClient.info
|
|
|
|
: level === "debug"
|
|
|
|
? this.syslogClient.info
|
|
|
|
: level === "info"
|
|
|
|
? this.syslogClient.info
|
|
|
|
: (null as never);
|
|
|
|
|
|
|
|
send
|
|
|
|
.bind(this.syslogClient)(message)
|
|
|
|
.catch(() => {});
|
2019-08-30 01:29:46 +02:00
|
|
|
}
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
public error(
|
|
|
|
x: string | Error,
|
|
|
|
data?: Record<string, any> | null,
|
|
|
|
important = false,
|
|
|
|
): void {
|
|
|
|
// 実行を継続できない状況で使う
|
2019-03-02 10:51:59 +01:00
|
|
|
if (x instanceof Error) {
|
|
|
|
data = data || {};
|
|
|
|
data.e = x;
|
2023-01-13 05:40:33 +01:00
|
|
|
this.log("error", x.toString(), data, important);
|
|
|
|
} else if (typeof x === "object") {
|
|
|
|
this.log(
|
|
|
|
"error",
|
|
|
|
`${(x as any).message || (x as any).name || x}`,
|
|
|
|
data,
|
|
|
|
important,
|
|
|
|
);
|
2019-03-02 10:51:59 +01:00
|
|
|
} else {
|
2023-01-13 05:40:33 +01:00
|
|
|
this.log("error", `${x}`, data, important);
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
public warn(
|
|
|
|
message: string,
|
|
|
|
data?: Record<string, any> | null,
|
|
|
|
important = false,
|
|
|
|
): void {
|
|
|
|
// 実行を継続できるが改善すべき状況で使う
|
|
|
|
this.log("warning", message, data, important);
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
public succ(
|
|
|
|
message: string,
|
|
|
|
data?: Record<string, any> | null,
|
|
|
|
important = false,
|
|
|
|
): void {
|
|
|
|
// 何かに成功した状況で使う
|
|
|
|
this.log("success", message, data, important);
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
public debug(
|
|
|
|
message: string,
|
|
|
|
data?: Record<string, any> | null,
|
|
|
|
important = false,
|
|
|
|
): void {
|
|
|
|
// デバッグ用に使う(開発者に必要だが利用者に不要な情報)
|
|
|
|
if (process.env.NODE_ENV !== "production" || envOption.verbose) {
|
|
|
|
this.log("debug", message, data, important);
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
public info(
|
|
|
|
message: string,
|
|
|
|
data?: Record<string, any> | null,
|
|
|
|
important = false,
|
|
|
|
): void {
|
|
|
|
// それ以外
|
|
|
|
this.log("info", message, data, important);
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
}
|