@node-elion/utils
Version:
Super scalable enterprise Node.js server library
113 lines • 3.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.prettyLevel = exports.prettyDate = void 0;
exports.prettyChildLevel = prettyChildLevel;
exports.prettyLogTransport = prettyLogTransport;
const chalk_1 = __importDefault(require("chalk"));
const util_1 = __importDefault(require("util"));
const tab = " ";
const compatibilityLevels = {
error: {
key: "error",
console: console.error,
chalk: chalk_1.default.red,
},
warn: {
key: "warn",
console: console.warn,
chalk: chalk_1.default.yellowBright,
},
info: {
key: "info",
console: console.info,
chalk: chalk_1.default.blue,
},
log: {
key: "log",
console: console.log,
chalk: chalk_1.default.white,
},
http: {
key: "http",
console: console.log,
chalk: chalk_1.default.white,
},
verbose: {
key: "verbose",
console: console.log,
chalk: chalk_1.default.white,
},
debug: {
key: "debug",
console: console.debug,
chalk: chalk_1.default.yellow,
},
silly: {
key: "silly",
console: console.log,
chalk: chalk_1.default.magenta,
},
};
const childColors = [
chalk_1.default.blue,
chalk_1.default.cyan,
chalk_1.default.green,
chalk_1.default.magenta,
chalk_1.default.yellow,
];
const prettyDate = (date) => `${date.getMonth().toString().padStart(2, "0")}-${date
.getDate()
.toString()
.padStart(2, "0")}-${date.getFullYear().toString()} ${date
.getHours()
.toString()
.padStart(2, "0")}:${date
.getMinutes()
.toString()
.padStart(2, "0")}:${date
.getSeconds()
.toString()
.padStart(2, "0")}.${date
.getMilliseconds()
.toString()
.padStart(3, "0")}`;
exports.prettyDate = prettyDate;
const prettyLevel = (level, size = 17) => {
const levelString = level.chalk(level.key);
return `${Array.from({ length: size - levelString.length }, () => " ").join("")}${levelString}`;
};
exports.prettyLevel = prettyLevel;
function prettyChildLevel(steps) {
return steps
.map((step, index) => {
const color = childColors[index % childColors.length];
return color(step);
})
.join(chalk_1.default.gray(" > "));
}
function isShouldBeNotUtilInspect(message) {
return typeof message === "string";
}
function prettyLogTransport(info) {
const prefix = `(${(0, exports.prettyDate)(new Date())}) ${(0, exports.prettyLevel)(compatibilityLevels[info.level])} [${prettyChildLevel(info.meta.level)}]:${tab}`;
if (typeof window === "undefined" && process && !process.env.DEBUG) {
compatibilityLevels[info.level].console(`${prefix}${info.message
.map((message) => isShouldBeNotUtilInspect(message)
? message
: util_1.default.inspect(message, {
colors: true,
showHidden: false,
breakLength: 80,
}))
.join(tab)
.split("\n")
.join(`\n${prefix}`)}`);
}
else {
compatibilityLevels[info.level].console(prefix, ...info.message);
}
}
//# sourceMappingURL=prettyConsoleTrasnport.transport.js.map