UNPKG

@autorest/common

Version:
108 lines 3.89 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonLogFormatter = exports.PrettyLogFormatter = void 0; exports.createLogFormatter = createLogFormatter; const json_1 = require("@azure-tools/json"); const chalk_1 = __importDefault(require("chalk")); const utils_1 = require("../utils"); const defaultOptions = { color: true, timestamp: true, }; function createLogFormatter(format, options = {}) { return format === "json" ? new JsonLogFormatter(options) : new PrettyLogFormatter(options); } const LEVEL_STR = { debug: "debug".padEnd(7), verbose: "verbose".padEnd(7), information: "info".padEnd(7), warning: "warning".padEnd(7), error: "error".padEnd(7), fatal: "fatal".padEnd(7), }; const LEVEL_COLORED_STR = { debug: chalk_1.default.blue(LEVEL_STR.debug), verbose: chalk_1.default.gray(LEVEL_STR.verbose), information: chalk_1.default.green(LEVEL_STR.information), warning: chalk_1.default.yellow.bold(LEVEL_STR.warning), error: chalk_1.default.red.bold(LEVEL_STR.error), fatal: chalk_1.default.redBright.bold(LEVEL_STR.fatal), }; class PrettyLogFormatter { constructor(options = {}) { this.options = { ...defaultOptions, ...options }; } log(log) { var _a; const useColor = this.options.color; const t = this.formatTimestamp(log.level); const level = useColor ? LEVEL_COLORED_STR[log.level] : LEVEL_STR[log.level]; const message = useColor ? (0, utils_1.color)(log.message) : log.message; let text = `${level} |${this.formatCode(log.code)}${t} ${message}`; for (const source of (_a = log.source) !== null && _a !== void 0 ? _a : []) { text += this.formatSource(source); } return text; } formatCode(code) { if (!code) { return ""; } return ` ${this.color(code, chalk_1.default.green)} |`; } formatTimestamp(level) { if (!(this.options.timestamp && (level === "debug" || level === "verbose"))) { return ""; } const colored = this.color(`[${getUpTime()} s]`, chalk_1.default.yellow); return ` ${colored}`; } color(text, color) { return this.options.color ? color(text) : text; } formatSource(source) { if (!source.position) { return ""; } try { return `\n - ${this.color(source.document, chalk_1.default.cyan)}${this.formatPosition(source.position)}`; } catch (e) { // no friendly name, so nothing more specific to show return ""; } } formatPosition(position) { let text = ""; if (position.line !== undefined) { text += `:${this.color(position.line.toString(), chalk_1.default.cyan.bold)}`; if (position.column !== undefined) { text += `:${this.color(position.column.toString(), chalk_1.default.cyan.bold)}`; } } const path = position.path ? ` (${(0, json_1.serializeJsonPointer)(position.path)})` : ""; return `${text}${path}`; } } exports.PrettyLogFormatter = PrettyLogFormatter; class JsonLogFormatter { constructor(options) { this.options = { timestamp: true, ...options }; } log(log) { const addTimestamp = this.options.timestamp && (log.level === "debug" || log.level === "verbose"); const data = addTimestamp ? { ...log, uptime: getUpTime() } : log; return JSON.stringify(data); } } exports.JsonLogFormatter = JsonLogFormatter; /** * @returns uptime of process in seconds */ function getUpTime() { return process.uptime().toFixed(2); } //# sourceMappingURL=formatter.js.map