UNPKG

@ayanaware/logger

Version:

Useful and great looking logging made easy

106 lines 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultFormatter = void 0; const util_1 = require("util"); const Color_1 = require("../Color"); const Formatter_1 = require("../Formatter"); const DefaultFormatterColor_1 = require("./DefaultFormatterColor"); const LogLevel_1 = require("../../constants/LogLevel"); /** * @ignore */ const fecha = require('fecha'); /** * @ignore */ let genericError; try { require.resolve('@ayanaware/errors'); genericError = require('@ayanaware/errors').GenericError; } catch (e) { // Ignore } /** * @ignore */ const ERROR_STACK_LINE_COLOR_REGEX = /^ {4}at (?:(.*?) (\(.*\))|(.*?))$/gm; /** * @ignore */ const ERROR_HEADER_COLOR_REGEX = /^(?:(Caused by:) )?(\w+):* *(.*)$/gm; /** * @ignore */ const NODEJS_SOURCE_MAPPED_LINE_COLOR_REGEX = /^ {8}-> (.*?)$/gm; /** * @ignore */ const { LOG_PACKAGE_NAME, LOG_PACKAGE_PATH, LOG_UNIQUE_MARKER, LOG_TIMESTAMP, ERROR_CAUSED_BY, ERROR_NAME, ERROR_CODE, ERROR_AT, ERROR_TYPE_AND_FUNCTION, ERROR_LOCATION, ERROR_NODEJS_SOURCE_MAP_ARROW, ERROR_NODEJS_SOURCE_MAP_LOCATION, } = DefaultFormatterColor_1.DefaultFormatterColor; class DefaultFormatter extends Formatter_1.Formatter { constructor(options) { super(); options = options || {}; if (typeof options.dateFormat !== 'string') options.dateFormat = 'YYYY-MM-DD HH:mm:ss:SSS'; if (typeof options.disableDefaultColors !== 'boolean') options.disableDefaultColors = false; if (typeof options.disableErrorColors !== 'boolean') options.disableErrorColors = false; options.colorMap = options.colorMap || new Map(); this.colors = Color_1.ColorUtil.createFormatterMap(options.colorMap, options.disableDefaultColors ? DefaultFormatterColor_1.DEFAULT_FORMATTER_COLORS_NOOP : DefaultFormatterColor_1.DEFAULT_FORMATTER_COLORS); this.dateFormat = options.dateFormat; this.disableErrorColors = options.disableErrorColors; } formatMessage(meta, message) { return `${this.formatTimestamp()} ${this.formatLevel(meta.level)}${this.formatOrigin(meta.origin, meta.uniqueMarker)}: ${message}`; } formatTimestamp() { return this.colors.get(LOG_TIMESTAMP)(fecha.format(Date.now(), this.dateFormat)); } formatLevel(level) { let fn = this.colors.get(level); if (!fn) fn = this.colors.get(LogLevel_1.LogLevel.INFO); return fn(level.padEnd(6)); } formatPackageName(packageName) { return `${this.colors.get(LOG_PACKAGE_NAME)(packageName)}`; } formatPackagePath(packagePath, name) { return `${packagePath || name ? ':' : ''}${this.colors.get(LOG_PACKAGE_PATH)(`${packagePath}${name}`)}`; } formatUniqueMarker(uniqueMarker) { return `${uniqueMarker ? `/${this.colors.get(LOG_UNIQUE_MARKER)(`${uniqueMarker}`)}` : ''}`; } formatOrigin(origin, uniqueMarker) { return `[${this.formatPackageName(origin.packageName)}${this.formatPackagePath(origin.packagePath, origin.name)}${this.formatUniqueMarker(uniqueMarker)}]`; } formatError(meta, error) { const anyError = error; const stack = typeof anyError?.[util_1.inspect.custom] === 'function' ? (0, util_1.inspect)(error, false, 0, false) : error.stack; if (!stack) return `${error.message}\n Malformed Error, stack was not included.`; if (this.disableErrorColors) return stack; const formattedStack = stack .replace(ERROR_STACK_LINE_COLOR_REGEX, (_, typeAndFn, location1, location2) => { return ` ${this.colors.get(ERROR_AT)('at')} ${this.colors.get(ERROR_TYPE_AND_FUNCTION)(typeAndFn)}${this.colors.get(ERROR_LOCATION)(location2)} ${this.colors.get(ERROR_LOCATION)(location1)}`; }) .replace(NODEJS_SOURCE_MAPPED_LINE_COLOR_REGEX, (_, location) => { return ` ${this.colors.get(ERROR_NODEJS_SOURCE_MAP_ARROW)('->')} ${this.colors.get(ERROR_NODEJS_SOURCE_MAP_LOCATION)(location)}`; }) .replace(ERROR_HEADER_COLOR_REGEX, (_, causedBy, errorName, message) => { let code = ''; if (message.startsWith('(')) { const lastIndex = message.indexOf(')'); code = message.substr(1, lastIndex - 1); message = message.substr(lastIndex + 1); } return `${this.colors.get(ERROR_CAUSED_BY)(causedBy)}${causedBy ? ' ' : ''}${this.colors.get(ERROR_NAME)(errorName)}: ${code ? '(' : ''}${this.colors.get(ERROR_CODE)(code)}${code ? ')' : ''}${message}`; }); return this.formatMessage(meta, formattedStack); } } exports.DefaultFormatter = DefaultFormatter; //# sourceMappingURL=DefaultFormatter.js.map