UNPKG

@fusionworks/advanced-logger

Version:

Advanced console logger which replace console.log adding a lot more configurability.

382 lines (365 loc) 12.3 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ function __decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } /** * Execute function with target name but from console. * Example( 'log' => 'console.log' ) */ // tslint:disable-next-line: function-name function DoFromConsole() { return function (target, propertyKey, descriptor) { const original = descriptor.value; descriptor.value = function () { const args = arguments; this.consoleKeeper[propertyKey].apply(this, args); original.apply(this); }; return descriptor; }; } class AbstractConsole { constructor() { this.consoleKeeper = console; this.init(); } init() { } assert(condition, message, ...data) { } clear() { } count(label) { } debug(message, ...optionalParams) { } dir(value, ...optionalParams) { } dirxml(value) { } error(message, ...optionalParams) { } exception(message, ...optionalParams) { } group(groupTitle, ...optionalParams) { } groupCollapsed(groupTitle, ...optionalParams) { } groupEnd() { } info(message, ...optionalParams) { } log(message, ...optionalParams) { } profile(reportName) { } profileEnd(reportName) { } table(...tabularData) { } time(label) { } timeEnd(label) { } timeStamp(label) { } timeline(label) { } timelineEnd(label) { } markTimeline(label) { } timeLog(label) { } countReset(label) { } trace(message, ...optionalParams) { } warn(message, ...optionalParams) { } } __decorate([ DoFromConsole() ], AbstractConsole.prototype, "assert", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "clear", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "count", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "debug", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "dir", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "dirxml", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "error", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "exception", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "group", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "groupCollapsed", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "groupEnd", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "info", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "log", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "profile", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "profileEnd", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "table", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "time", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "timeEnd", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "timeStamp", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "timeline", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "timelineEnd", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "markTimeline", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "timeLog", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "countReset", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "trace", null); __decorate([ DoFromConsole() ], AbstractConsole.prototype, "warn", null); /** * Check whatever command should be triggered or not. */ // tslint:disable-next-line: function-name function FilterVisible() { /** * Applies only over logging methods. */ function visible(methodName) { const configuration = this.configuration || {}; const methodConfig = configuration[methodName]; // Take global configuration in case of undefined custom configuration const isObjectButClean = typeof methodConfig === 'object' && JSON.stringify(methodConfig) === '{}'; if (methodConfig === undefined || isObjectButClean) { return !configuration.hide; } if (typeof methodConfig === 'boolean') { return methodConfig; } return !(methodConfig || {}).hide; } return function (target, propertyKey, descriptor) { const originalMethod = descriptor.value; descriptor.value = function () { const args = arguments; if (!visible.apply(this, [propertyKey])) { return; } return originalMethod.apply(this, args); }; return descriptor; }; } function isNode() { return Boolean(typeof window === 'undefined' && typeof process === 'object'); } /** * Convert all given args to colorfull ones. * Example( ['hello world'] => ['%chello world', {given_css}] ) */ // tslint:disable-next-line: function-name function Colorfull() { function transformOutput(css, args) { if (!css) { return args; } const allButFirst = args.slice(1); const colorKeeper = args.length < 2 ? '' : allButFirst.reduce((agg) => { return `${agg} %s`; }, ''); return [`%c${args[0]}${colorKeeper}`, css, ...allButFirst]; } return function (target, propertyKey, descriptor) { const originalMethod = descriptor.value; descriptor.value = function () { const args = [...arguments]; // Node cannot have css. if (isNode()) { return originalMethod.apply(this, args); } const methodConfig = this.configuration[propertyKey] || {}; const css = methodConfig.css || this.configuration.css || undefined; return originalMethod.apply(this, transformOutput(css, args)); }; return descriptor; }; } /** * Convert all given args to colorfull ones. * Example( ['hello world'] => ['%chello world', {given_css}] ) */ // tslint:disable-next-line: function-name function PrefixSufix() { const toArray = (val) => Array.isArray(val) ? val : [val]; return function (target, propertyKey, descriptor) { const originalMethod = descriptor.value; descriptor.value = function () { let args = [...arguments]; const methodConfig = this.configuration[propertyKey] || {}; const prefix = methodConfig.prefix || this.configuration.prefix || undefined; const sufix = methodConfig.sufix || this.configuration.sufix || undefined; if (prefix) { args = [...toArray(prefix), ...args]; } if (sufix) { args = [...args, ...toArray(sufix)]; } return originalMethod.apply(this, args); }; return descriptor; }; } /** * Simple object check. * @param item * @returns {boolean} */ function isObject(item) { return !!(item && typeof item === 'object' && !Array.isArray(item)); } function mergeWith(source, targets, customHandler = (source, target) => undefined) { if (!isObject(source)) { return source; } (Array.isArray(targets) ? targets : [targets]).forEach((target) => { if (!isObject(target)) { return; } Object.keys(target).forEach((key) => { const customResult = customHandler && customHandler(source[key], target[key]); if (customResult !== undefined) { return Object.assign(source, { [key]: customResult }); } if (Array.isArray(target[key])) { if (Array.isArray(source[key])) { return source[key] = [...source[key], ...target[key]]; } return source[key] = [...source[key], target]; } if (isObject(target[key]) && source[key]) { return mergeWith(source[key], target[key], customHandler); } Object.assign(source, { [key]: target[key] }); }); }); return source; } class Console extends AbstractConsole { constructor(configuration = {}) { super(); this.configuration = configuration; } /** * Join old configuration with new one. * If {forced} is activeted it replace instead of join. */ update(configuration = {}, forced) { if (forced) { this.configuration = configuration; this.init(); return; } const allowUndefined = (oldVal, newVal) => { if (newVal === undefined) { return null; } }; mergeWith(this.configuration, configuration, allowUndefined); this.init(); } /** * Returns new instance of console with given configuration. */ standalone(configuration = {}, forced) { const clone = Object.assign({}, this); Object.setPrototypeOf(clone, Console.prototype); clone.update(configuration, forced); return clone; } // LOGGING START log() { } warn() { } error() { } debug() { } info() { } } __decorate([ FilterVisible(), PrefixSufix(), Colorfull(), DoFromConsole() ], Console.prototype, "log", null); __decorate([ FilterVisible(), PrefixSufix(), Colorfull(), DoFromConsole() ], Console.prototype, "warn", null); __decorate([ FilterVisible(), PrefixSufix(), Colorfull(), DoFromConsole() ], Console.prototype, "error", null); __decorate([ FilterVisible(), PrefixSufix(), Colorfull(), DoFromConsole() ], Console.prototype, "debug", null); __decorate([ FilterVisible(), PrefixSufix(), Colorfull(), DoFromConsole() ], Console.prototype, "info", null); class MethodAdvancedConfiguration { constructor(hide, css, prefix, sufix) { this.hide = hide; this.css = css; this.prefix = prefix; this.sufix = sufix; } } class ConfigurationModel extends MethodAdvancedConfiguration { constructor(config) { super(); Object.assign(this, config || {}); } } exports.AbstractConsole = AbstractConsole; exports.Colorfull = Colorfull; exports.ConfigurationModel = ConfigurationModel; exports.Console = Console; exports.DoFromConsole = DoFromConsole; exports.FilterVisible = FilterVisible; exports.MethodAdvancedConfiguration = MethodAdvancedConfiguration;