UNPKG

@visulima/pail

Version:

Highly configurable Logger for Node.js, Edge and Browser.

201 lines (194 loc) 7.07 kB
'use strict'; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } }); const process = require('node:process'); const colorize = require('@visulima/colorize'); const error = require('@visulima/error/error'); const inspector = require('@visulima/inspector'); const formatLabel = require('./format-label-o4RcL4VF.cjs'); const constants = require('./constants-DHfYGxxG.cjs'); const abstractPrettyReporter = require('./abstract-pretty-reporter-BR_6_JVa.cjs'); const getLongestLabel = require('./get-longest-label-CQ7lRhgD.cjs'); const writeStream = require('./write-stream-CkNf2Ju8.cjs'); const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e; const colorize__default = /*#__PURE__*/_interopDefaultCompat(colorize); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const pailFileFilter = /* @__PURE__ */ __name((line) => !/[\\/]pail[\\/]dist/.test(line), "pailFileFilter"); class PrettyReporter extends abstractPrettyReporter.AbstractPrettyReporter { static { __name(this, "PrettyReporter"); } #stdout; #stderr; #interactiveManager; #interactive = false; #inspectOptions; #errorOptions; constructor(options = {}) { const { error: errorOptions, inspect: inspectOptions, ...rest } = options; super({ uppercase: { label: true, ...rest.uppercase }, ...rest }); this.#inspectOptions = { ...formatLabel.defaultInspectorConfig, ...inspectOptions }; this.#errorOptions = { ...errorOptions, color: { fileLine: colorize.green, hint: colorize.cyan, marker: colorize.red, message: colorize.red, method: colorize.greenBright, title: colorize.red } }; this.#stdout = process.stdout; this.#stderr = process.stderr; } setStdout(stdout_) { this.#stdout = stdout_; } setStderr(stderr_) { this.#stderr = stderr_; } setInteractiveManager(manager) { this.#interactiveManager = manager; } setIsInteractive(interactive) { this.#interactive = interactive; } log(meta) { this._log(this._formatMessage(meta), meta.type.level); } // eslint-disable-next-line sonarjs/cognitive-complexity _formatMessage(data) { const { columns } = formatLabel.terminalSize(); let size = columns; if (typeof this._styles.messageLength === "number") { size = this._styles.messageLength; } const { badge, context, date, error: error$1, file, groups, label, message, prefix, repeated, scope, suffix, traceError, type } = data; const { color } = this._loggerTypes[type.name]; const colorized = color ? colorize__default[color] : colorize.white; const groupSpaces = groups.map(() => " ").join(""); const items = []; if (groups.length > 0) { items.push(groupSpaces + colorize.grey("[" + groups.at(-1) + "]") + " "); } if (date) { items.push(colorize.grey(this._styles.dateFormatter(typeof date === "string" ? new Date(date) : date)) + " "); } if (badge) { items.push(colorized(badge)); } else { const longestBadge = abstractPrettyReporter.getLongestBadge(this._loggerTypes); if (longestBadge.length > 0) { items.push(colorize.grey(".".repeat(longestBadge.length)) + " "); } } const longestLabel = getLongestLabel.getLongestLabel(this._loggerTypes); if (label) { items.push(colorized(formatLabel.formatLabel(label, this._styles)) + " ", colorize.grey(".".repeat(longestLabel.length - formatLabel.stringLength(label)))); } else { items.push(colorize.grey(".".repeat(longestLabel.length + 2))); } if (repeated) { items.push(colorize.bgGrey.white("[" + repeated + "x]") + " "); } if (Array.isArray(scope) && scope.length > 0) { items.push(" " + colorize.grey("[" + scope.join(" > ") + "]") + " "); } if (prefix) { items.push( colorize.grey( (Array.isArray(scope) && scope.length > 0 ? ". " : " ") + "[" + (this._styles.underline.prefix ? colorize.underline(prefix) : prefix) + "]" ) + " " ); } const titleSize = formatLabel.stringLength(items.join(" ")); if (file) { const fileMessage = file.name + (file.line ? ":" + file.line : ""); const fileMessageSize = formatLabel.stringLength(fileMessage); if (fileMessageSize + titleSize + 2 > size) { items.push(colorize.grey(" " + fileMessage)); } else { items.push(colorize.grey(".".repeat(size - titleSize - fileMessageSize - 2) + " " + fileMessage)); } } else { items.push(colorize.grey(".".repeat(size - titleSize - 1))); } if (items.length > 0) { items.push("\n\n"); } if (message !== constants.EMPTY_SYMBOL) { const formattedMessage = typeof message === "string" ? message : inspector.inspect(message, this.#inspectOptions); items.push( groupSpaces + formatLabel.wrapAnsi(formattedMessage, size - 3, { hard: true, trim: false, wordWrap: true }) ); } if (context) { let hasError = false; items.push( ...context.map((value) => { if (value instanceof Error) { hasError = true; return "\n\n" + error.renderError(value, { ...this.#errorOptions, filterStacktrace: pailFileFilter, prefix: groupSpaces }); } if (typeof value === "object") { return " " + inspector.inspect(value, this.#inspectOptions); } const newValue = (hasError ? "\n\n" : " ") + value; hasError = false; return newValue; }) ); } if (error$1) { items.push( error.renderError(error$1, { ...this.#errorOptions, filterStacktrace: pailFileFilter, prefix: groupSpaces }) ); } if (traceError) { items.push( "\n\n" + error.renderError(traceError, { ...this.#errorOptions, filterStacktrace: pailFileFilter, hideErrorCauseCodeView: true, hideErrorCodeView: true, hideErrorErrorsCodeView: true, hideMessage: true, prefix: groupSpaces }) ); } if (suffix) { items.push("\n", groupSpaces + colorize.grey(this._styles.underline.suffix ? colorize.underline(suffix) : suffix)); } return items.join("") + "\n"; } _log(message, logLevel) { const streamType = ["error", "trace", "warn"].includes(logLevel) ? "stderr" : "stdout"; const stream = streamType === "stderr" ? this.#stderr : this.#stdout; if (this.#interactive && this.#interactiveManager !== void 0 && stream.isTTY) { this.#interactiveManager.update(streamType, message.split("\n"), 0); } else { writeStream.writeStream(message + "\n", stream); } } } exports.PrettyReporter = PrettyReporter;