UNPKG

@rockpack/logger

Version:

This module can help you build error tracking & crash reporting system for your React application.

76 lines (75 loc) 2.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.logger = exports.createLogger = void 0; var valid_types_1 = require("valid-types"); var limited_array_1 = __importDefault(require("limited-array")); /** * Types: * log * info * warn * error * debug * */ var Logger = /** @class */ (function () { function Logger() { var _this = this; this.active = true; this.stdout = null; this.ignoreLogging = false; this._count = 0; this.getCounter = function () { return _this._count; }; this.getStackCollection = function () { return _this.stackCollection; }; this.stackCollection = new limited_array_1.default(); } Logger.prototype.log = function (message, important) { this._handler(message, 'log', !!important); }; Logger.prototype.info = function (message, important) { this._handler(message, 'info', !!important); }; Logger.prototype.debug = function (message, important) { this._handler(message, 'debug', !!important); }; Logger.prototype.warn = function (message, important) { this._handler(message, 'warn', !!important); }; Logger.prototype.error = function (message, important) { this._handler(message, 'error', !!important); }; Logger.prototype.setUp = function (props) { if (typeof props.active === 'boolean') { this.active = Boolean(props.active); } if (typeof props.stdout === 'function') { this.stdout = props.stdout; } }; Logger.prototype._handler = function (message, level, important) { if (!this.ignoreLogging && this.active) { if (valid_types_1.isFunction(this.stdout)) { this.stdout(level, message, important); } var stackData = void 0; if (typeof message === 'string') { var temp = {}; temp[level] = message; stackData = temp; } else if (typeof message === 'object') { stackData = message; } if (stackData) { this.stackCollection.add(Object.assign({}, stackData)); } this._count += 1; } }; return Logger; }()); exports.createLogger = function () { return new Logger(); }; exports.logger = exports.createLogger();