go-logs
Version:
A configurable logger with message filtering, formatting and handling.
1 lines • 7.68 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Logs=t():e.Logs=t()}(this,()=>{return n=[(e,t,r)=>{var n=r(1);e.exports.Logger=n,e.exports.Level=r(2),e.exports.log=n.log,e.exports.trace=n.trace,e.exports.debug=n.debug,e.exports.info=n.info,e.exports.warn=n.warn,e.exports.error=n.error,e.exports.fatal=n.fatal},(e,t,r)=>{"use strict";var a=r(2);function n(e){if("function"!=typeof e)throw new TypeError("Expected a function, but found: ",e);return e}var l,o={all:console.log,trace:console.trace,debug:console.debug,info:console.info,warn:console.warn,error:console.error,fatal:console.error};function i(e){if(!(this instanceof i))return e instanceof i?e:new i(e);if(null!=(e=null!=e?e:{})&&e.constructor===Object){if(this._name=null,this._level=a.ALL,this._filters=[],this._handlers=[],(this._formatter=null)!=e.name&&this.setName(e.name),void 0!==e.level&&this.setLevel(e.level),null!=e.filters)for(var t=0;t<e.filters.length;t++)this.addFilter(e.filters[t]);if(null!=e.handlers)for(t=0;t<e.handlers.length;t++)this.addHandler(e.handlers[t]);null!=e.formatter&&this.setFormatter(e.formatter)}else{if(!(e instanceof i))throw new TypeError("Expected a plain object or an instance of Logger, but found: "+e);var r=e;this._name=r.getName(),this._level=r.getLevel(),this._filters=r.getAllFilters().slice(),this._handlers=r.getAllHandlers().slice(),this._formatter=r.getFormatter()}}Object.assign(i.prototype,{getName:function(){return this._name},setName:function(e){return this._name=null!=e?String(e):null,this},getLevel:function(){return this._level},setLevel:function(e){return this._level=a.get(e)||a(e),this},getAllFilters:function(){return this._filters.slice()},addFilter:function(e){return this._filters.push(n(e)),this},removeFilter:function(e){e=this._filters.indexOf(e);return 0<=e&&this._filters.splice(e,1),this},clearFilters:function(){return this._filters=[],this},getAllHandlers:function(){return this._handlers.slice()},addHandler:function(e){return this._handlers.push(n(e)),this},removeHandler:function(e){e=this._handlers.indexOf(e);return 0<=e&&this._handlers.splice(e,1),this},clearHandlers:function(){return this._handlers=[],this},getFormatter:function(){return this._formatter||null},setFormatter:function(e){return this._formatter=null!=e?n(e):null,this},log:function(e,t){if(!e||this._level.value===a.OFF.value)return!1;e=a.get(e)||a(e);if(!e.value||this._level.value>e.value)return!1;var r=Array.prototype.slice.call(arguments,1);r.unshift(e);for(var n,l=0;l<this._filters.length;l++)if(!this._filters[l].apply(this,r.slice()))return!1;null!=this._formatter&&void 0!==(n=this._formatter.apply(this,r.slice()))&&((n=n instanceof Array?n:[n]).unshift(e),r=n);for(l=0;l<this._handlers.length;l++)this._handlers[l].apply(this,r.slice());return 0<this._handlers.length},trace:function(e){return this.log.apply(this,[a.TRACE].concat(Array.prototype.slice.call(arguments)))},debug:function(e){return this.log.apply(this,[a.DEBUG].concat(Array.prototype.slice.call(arguments)))},info:function(e){return this.log.apply(this,[a.INFO].concat(Array.prototype.slice.call(arguments)))},warn:function(e){return this.log.apply(this,[a.WARN].concat(Array.prototype.slice.call(arguments)))},error:function(e){return this.log.apply(this,[a.ERROR].concat(Array.prototype.slice.call(arguments)))},fatal:function(e){return this.log.apply(this,[a.FATAL].concat(Array.prototype.slice.call(arguments)))}}),Object.assign(i,{getDefault:function(){return(l=null==l?new i({name:"default"}):l).addHandler(function(e){var t,r=null!=e.name?e.name:null,r=o[r]||o.all;r&&((t=Array.prototype.slice.call(arguments,1)).unshift(e),r.apply(this,t))}),l},Level:a,getName:function(){return i.prototype.getName.apply(this.getDefault(),arguments)},setName:function(){return i.prototype.setName.apply(this.getDefault(),arguments)},getLevel:function(){return i.prototype.getLevel.apply(this.getDefault(),arguments)},setLevel:function(){return i.prototype.setLevel.apply(this.getDefault(),arguments)},getAllFilters:function(){return i.prototype.getAllFilters.apply(this.getDefault(),arguments)},addFilter:function(){return i.prototype.addFilter.apply(this.getDefault(),arguments)},removeFilter:function(){return i.prototype.removeFilter.apply(this.getDefault(),arguments)},clearFilters:function(){return i.prototype.clearFilters.apply(this.getDefault(),arguments)},getAllHandlers:function(){return i.prototype.getAllHandlers.apply(this.getDefault(),arguments)},addHandler:function(){return i.prototype.addHandler.apply(this.getDefault(),arguments)},removeHandler:function(){return i.prototype.removeHandler.apply(this.getDefault(),arguments)},clearHandlers:function(){return i.prototype.clearHandlers.apply(this.getDefault(),arguments)},getFormatter:function(){return i.prototype.getFormatter.apply(this.getDefault(),arguments)},setFormatter:function(){return i.prototype.setFormatter.apply(this.getDefault(),arguments)},log:function(){return i.prototype.log.apply(this.getDefault(),arguments)},trace:function(){return i.prototype.trace.apply(this.getDefault(),arguments)},debug:function(){return i.prototype.debug.apply(this.getDefault(),arguments)},info:function(){return i.prototype.info.apply(this.getDefault(),arguments)},warn:function(){return i.prototype.warn.apply(this.getDefault(),arguments)},error:function(){return i.prototype.error.apply(this.getDefault(),arguments)},fatal:function(){return i.prototype.fatal.apply(this.getDefault(),arguments)}}),e.exports=i},(e,t,r)=>{"use strict";var n=r(3).newType("Level",["value","name"],{valueOf:"value"});n.OFF=n(0,"off"),n.ALL=n(1,"all"),n.TRACE=n(100,"trace"),n.DEBUG=n(200,"debug"),n.INFO=n(300,"info"),n.WARN=n(400,"warn"),n.ERROR=n(500,"error"),n.FATAL=n(600,"fatal"),n.values=[n.OFF,n.ALL,n.TRACE,n.DEBUG,n.INFO,n.WARN,n.ERROR,n.FATAL],n.get=function(e){if("string"==typeof e){for(var t=0;t<n.values.length;t++)if((r=n.values[t]).name===e)return r}else if("number"==typeof e){for(t=0;t<n.values.length;t++)if((r=n.values[t]).value===e)return r}else for(var r,t=0;t<n.values.length;t++)if((r=n.values[t])===e)return r},n.below=function(e){var t="number"==typeof e?e:n.get(e).value;if(null!=t)for(var r=n.values.length-1;0<=r;r--)if(null!=n.values[r]&&n.values[r].value<t)return n.values[r]},n.above=function(e){var t="number"==typeof e?e:n.get(e).value;if(null!=t)for(var r=0;r<n.values.length;r++)if(null!=n.values[r]&&n.values[r].value>t)return n.values[r]},e.exports=n},(e,t,r)=>{e.exports=r(4)},(e,t,r)=>{var r=r(5),n=r("Constant",["value","name"],{validate:function(e,t){return[e,null!=t?String(t):null]},valueOf:"value"});n.newType=r,e.exports=n},e=>{e.exports=function(e,n,l){if(l&&null!=l.validate&&"function"!=typeof l.validate)throw new TypeError("Expected a function for validate, but found: ",l.validate);function a(e){if(!(this instanceof a))return e instanceof a?e:new a(...arguments);var t=void 0===(t=l&&null!=l.validate?l.validate.apply(this,arguments):t)?arguments:t;if(null!=n)for(var r=0;r<n.length;r++)Object.defineProperty(this,n[r],{enumerable:!0,configurable:!1,writable:!1,value:t[r]});l&&l.saveInstances&&a.instances.push(this)}return l&&l.saveInstances&&Object.defineProperty(a,"instances",{enumerable:!0,configurable:!1,writable:!1,value:[]}),l&&("string"==typeof l.valueOf?a.prototype.valueOf=function(){return this[l.valueOf]}:"function"==typeof l.valueOf&&(a.prototype.valueOf=l.valueOf)),null!=e&&Object.defineProperty(a,"name",{configurable:!1,writable:!1,value:e}),a}}],l={},function e(t){var r=l[t];return void 0===r&&(r=l[t]={exports:{}},n[t](r,r.exports,e)),r.exports}(0);var n,l});