UNPKG

@evojs/logger

Version:
70 lines (69 loc) 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Log = void 0; /* eslint-disable lodash/prefer-lodash-typecheck */ const callsite_1 = require("@evojs/callsite"); const utils_1 = require("./utils"); const FORMAT_REPLACE_MASK = /\{\{\s*([a-zA-Z_$][0-9a-zA-Z_$]+)(?:\s*\|\s*([a-zA-Z_$][0-9a-zA-Z_$]+))?\s*\}\}/g; const INTERNAL_CALLSITE_DEPTH = 2; class Log { constructor(name, formats, pipes, level, args, callsiteDepth = 0) { this.name = name; this.formats = formats; this.pipes = pipes; this.level = level; this.args = args; this.callsiteDepth = callsiteDepth; this.date = Date.now(); if (callsiteDepth > 0) { this.callsite = callsite_1.Callsite.get(callsiteDepth + INTERNAL_CALLSITE_DEPTH, 1)[0]; } } /** * Get formatted messages. */ messages() { return this.formats.map((f) => { if (f === 'json') { return (0, utils_1.toJson)(this.toMessage()); } if (typeof f === 'function') { return f.call(this.pipes, this.toMessage()); } Log.lineLength = process.stdout.columns; const stringMessage = f.replace(FORMAT_REPLACE_MASK, (_, propName, pipeName) => { const prop = this[propName]; if (pipeName !== undefined) { const pipe = this.pipes[pipeName]; if (typeof pipe !== 'function') { throw new TypeError(`Pipe property "${pipeName}" is not a function`); } return pipe(prop); } return prop; }); if (!Log.separator || !stringMessage.includes(Log.separator)) { return stringMessage; } if (Log.lineLength === 0) { return stringMessage.split(Log.separator).join('\n'); } return (0, utils_1.resolveSeparators)(stringMessage, Log.separator, Log.lineLength); }); } /** * Get Message object. */ toMessage() { return { date: this.date, level: this.level, name: this.name || undefined, args: this.args, callsite: this.callsite, }; } } exports.Log = Log; Log.lineLength = 0; Log.separator = '<-|->';