@kotori-bot/logger
Version:
Logger For Kotori
114 lines (111 loc) • 4.97 kB
JavaScript
/**
* @Package @kotori-bot/logger
* @Version 1.3.3
* @Author Hotaru <me@hotaru.icu>
* @Copyright 2024 Hotaru. All rights reserved.
* @License GPL-3.0
* @Link https://github.com/kotorijs/kotori
* @Date 2025/2/15 18:31:34
*/
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var console_exports = {};
__export(console_exports, {
ConsoleTransport: () => ConsoleTransport,
default: () => console_default
});
module.exports = __toCommonJS(console_exports);
var import_fast_safe_stringify = __toESM(require("fast-safe-stringify"));
var import_dayjs = __toESM(require("dayjs"));
var import_tools = require("@kotori-bot/tools");
var import_transport = __toESM(require("../utils/transport"));
var import_escaper = require("../utils/escaper");
var import_common = require("../types/common");
const DEFAULT_OPTIONS = {
template: "<blue>%time%</blue> %level% (<bold>%pid%</bold>) %labels%: %msg%",
label: "[<cyan>%name%</cyan>]",
time: "YY/M/D H:m:s",
// Reference: https://day.js.org/docs/en/display/format
useColor: true,
levels: {
FATAL: ["<redBright><bold>FATAL</bold></redBright> ", "redBright"],
ERROR: ["<red>ERROR</red>", "red"],
WARN: ["<yellow>WARN</yellow>", "yellowBright"],
INFO: ["<green>INFO</green>"],
RECORD: ["<cyan>LOG</cyan>"],
DEBUG: ["<magenta>DEBUG</magenta>", "magentaBright"],
TRACE: ["<gray>TRACE</gray>", "gray"]
},
indent: 2
};
class ConsoleTransport extends import_transport.default {
cs;
print = typeof process === "undefined" ? console.log : process.stdout.write.bind(process.stdout);
printErr = typeof process === "undefined" ? console.error : process.stderr.write.bind(process.stderr);
render({ label, level, msg, time, pid }) {
const { options } = this;
const levels = (options.levels ?? DEFAULT_OPTIONS.levels)[import_common.LoggerLevel[level]];
return (0, import_tools.stringTemp)(options.template ?? DEFAULT_OPTIONS.template, {
time: (0, import_dayjs.default)(time).format(options.time ?? DEFAULT_OPTIONS.time),
pid: String(pid),
level: levels?.[0] ?? DEFAULT_OPTIONS.levels[import_common.LoggerLevel[level]][0],
labels: label.map((name) => (0, import_tools.stringTemp)(options.label ?? DEFAULT_OPTIONS.label, { name })).join(" "),
msg: levels?.[1] ? `<${levels[1]}>${msg}</${levels[1]}>` : msg
});
}
constructor(options) {
super(Object.assign(DEFAULT_OPTIONS, options ?? {}));
this.cs = new import_tools.Colors(new import_tools.TerminalAdapter());
}
escaper = (args) => {
const result = args.map((arg) => {
if (arg && typeof arg === "object") {
const result2 = (0, import_fast_safe_stringify.default)(arg, void 0, this.options.indent ?? DEFAULT_OPTIONS.indent);
return result2 === "{}" ? String(arg) : result2;
}
return (0, import_escaper.escaperSingle)(arg);
}).join(" ");
return result.replace(/([0-9]+)/g, "<yellow>$1</yellow>").replaceAll("undefined", "<dim>undefined</dim>").replaceAll("null", "<bold>null</bold>").replaceAll("true", "<green>true</green>").replaceAll("false", "<green>false</green>");
};
handle(data) {
const result = typeof this.options.template === "function" ? this.options.template(data) : this.render(data);
if (data.level === import_common.LoggerLevel.FATAL || data.level === import_common.LoggerLevel.ERROR) {
this.printErr(`${this.cs.parse(result)}
`);
return;
}
this.print(`${this.cs.parse(result)}
`);
}
}
var console_default = ConsoleTransport;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ConsoleTransport
});