UNPKG

movex-core-util

Version:

Movex Core Util is the library of utilities for Movex

78 lines 2.9 kB
"use strict"; // const movexLogsy = Logger.get('Movex'); Object.defineProperty(exports, "__esModule", { value: true }); exports.globalLogsy = void 0; const ts_pubsy_1 = require("ts-pubsy"); class Logsy { constructor(prefix = '') { this.prefix = prefix; this.pubsy = new ts_pubsy_1.Pubsy(); this.onLog = (fn) => this.pubsy.subscribe('onLog', fn); // To be overriden // public onLog: (event: LoggingEvent) => void = (event: LoggingEvent) => { // this.pubsy.publish('onLog', { // ...event, // }); // }; this.handler = (event) => { const prefix = this.hasGroupOpen() ? '' : this.prefix; this.pubsy.publish('onLog', Object.assign(Object.assign({}, event), { prefix })); // this.onLog({ ...event, prefix }); }; // public ON: boolean = globalDisabled || true; this.activeGroups = 0; this.log = (message, payload) => { this.handler({ method: 'log', message, payload }); }; this.info = (message, payload) => { this.handler({ method: 'info', message, payload }); }; this.warn = (message, payload) => { this.handler({ method: 'warn', message, payload }); }; this.error = (message, payload) => { this.handler({ method: 'error', message, payload }); }; this.group = (message, payload) => { this.handler({ method: 'group', message, payload }); this.openGroup(); }; this.groupEnd = (message, payload) => { if (message) { this.handler({ method: 'log', message, payload }); } this.handler({ method: 'groupEnd' }); this.closeGroup(); }; this.openGroup = () => { this.activeGroups = this.activeGroups + 1; }; this.closeGroup = () => { if (this.activeGroups > 0) { this.activeGroups = this.activeGroups - 1; } else { this.activeGroups = 0; } }; this.hasGroupOpen = () => this.activeGroups > 0; this.debug = (message, payload) => { this.handler({ method: 'debug', message, payload }); }; this.withNamespace = (s) => { const next = new Logsy(this.prefix + s); // next.onLog((...args) => this.onLog(...args)); // // this.onLog(); // this.onLog((event) => { // this.pubsy.publish('onLog', event); // }); // next.onLog = (event) => this.pubsy.publish('onLog', event); next.onLog((event) => { this.pubsy.publish('onLog', event); }); return next; }; } } exports.globalLogsy = new Logsy(); //# sourceMappingURL=Logsy.js.map