UNPKG

caterpillar

Version:

Caterpillar is the ultimate logging system for Deno, Node.js, and Web Browsers. Log levels are implemented to the RFC standard. Log entries can be filtered and piped to various streams, including coloured output to the terminal, the browser's console, and

162 lines (161 loc) 5.99 kB
"use strict"; // this cannot be located at source/brower.ts as otherwise it would become the browser entry var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Browser = void 0; // Imports var transform_js_1 = require("../transform.js"); /** * Convert human readable Caterpillar entries into browser compatible entries. * @example * ``` javascript * import { Logger, Human, Browser } from 'caterpillar' * const logger = new Logger() * logger.pipe(new Human()).pipe(new Browser()) * logger.log('info', 'some', {data: 'oh yeah'}, 42) * ``` */ var Browser = /** @class */ (function (_super) { __extends(Browser, _super); /** Create our instance and apply our configuraiton options. */ function Browser(opts) { var _this = _super.call(this) || this; /** Whether or not we should use color. */ _this.color = true; /** Whether or not we should write to the browser console. */ _this.output = true; /** The objects that tell our browser transform how to convert terminal colours into console colours. */ _this.styles = { red: { start: '31', end: '39', value: 'color:red', }, yellow: { start: '33', end: '39', value: 'color:orange', }, green: { start: '32', end: '39', value: 'color:green', }, bright: { start: '1', end: '22', value: 'font-weight:bold', }, dim: { start: '2', end: '22', value: 'color:lightGray', }, italic: { start: '3', end: '23', value: 'font-style:italic', }, underline: { start: '4', end: '24', value: 'text-decoration:underline', }, }; // options if ((opts === null || opts === void 0 ? void 0 : opts.color) != null) _this.color = opts.color; if ((opts === null || opts === void 0 ? void 0 : opts.output) != null) _this.output = opts.output; if ((opts === null || opts === void 0 ? void 0 : opts.styles) != null) _this.styles = opts.styles; return _this; } /** * Convert a human readable Caterpillar entry into a format that browser consoles can understand. * And if the `write` config property is `true` (it is by default), write the result to the browser console. */ Browser.prototype.format = function (message) { // Prepare var _a = this, color = _a.color, styles = _a.styles, output = _a.output; // Replace caterpillar-human formatted entry /* eslint no-control-regex:0 */ var args = []; var result = message.replace(/\u001b\[([0-9]+)m(.+?)\u001b\[([0-9]+)m/g, function (match, start, content, end) { // Check if (color === false) return content; // Prepare var matchedStyle, style; // Find the matcing style for this combination for (var key in styles) { if (styles.hasOwnProperty(key)) { style = styles[key]; if (String(style.start) === String(start) && String(style.end) === String(end)) { matchedStyle = style; break; } } } // Check if (!matchedStyle) return content; // Push the style args.push(matchedStyle.value); args.push(content); args.push('color:default; font:default; text-decoration:default'); return '%c%s%c'; }); // Final format var parts = [result.trim()].concat(args); // Write /* eslint no-console:0 */ if (output) console.log.apply(console, __spreadArray([], __read(parts), false)); // Return return parts; }; return Browser; }(transform_js_1.Transform)); exports.Browser = Browser; exports.default = Browser;