telebot
Version:
The easy way to write Telegram bots.
69 lines (68 loc) • 2.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
var Levels;
(function (Levels) {
Levels[Levels["fatal"] = 0] = "fatal";
Levels[Levels["error"] = 1] = "error";
Levels[Levels["warn"] = 2] = "warn";
Levels[Levels["log"] = 3] = "log";
Levels[Levels["info"] = 4] = "info";
Levels[Levels["debug"] = 5] = "debug";
})(Levels = exports.Levels || (exports.Levels = {}));
// export const Types = {
// fatal: "#",
// error: "!",
// warn: "%",
// log: "",
// info: "?",
// debug: "@"
// };
class TeleBotDev {
constructor(id, instance, options) {
this.logs = [];
this.logLimit = 1024;
this.debug = this.createProcessor(Levels.debug);
this.info = this.createProcessor(Levels.info);
this.log = this.createProcessor(Levels.log);
this.warn = this.createProcessor(Levels.warn);
this.fatal = this.createProcessor(Levels.fatal);
this.error = this.createProcessor(Levels.error);
this.id = id;
this.instance = instance;
this.options = options;
}
logger(log) {
if (!this.options) {
return;
}
const { id, data, code, level = Levels.debug, message = utils_1.toString(data), error } = log;
const { ids, levels } = this.options;
if ((ids && !ids.includes(id.toString())) ||
(levels && !levels.includes(level))) {
return;
}
if (this.logs.length > this.logLimit) {
this.logs.shift();
}
this.logs.push(log);
const logId = [id, level, code].filter(i => !!i && i !== 0).join(":");
const timestamp = Date.now();
const text = [message, error].filter(i => !!i).join(" ");
// eslint-disable-next-line no-console
console.log(`[${timestamp}] <${logId}> ${text}`);
}
createProcessor(level) {
return (id, props) => {
if (typeof props === "string" || typeof props === "number") {
props = { message: props };
}
else if (props === null || props === void 0 ? void 0 : props.message) {
props.message = utils_1.toString(props.message);
}
this.logger(Object.assign(Object.assign({}, props), { id,
level }));
};
}
}
exports.TeleBotDev = TeleBotDev;