loglevel-format
Version:
Plugin for loglevel message format
1 lines • 1.79 kB
JavaScript
!function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof module&&module.exports?module.exports=t():e.format=t(e)}(this,function(e){"use strict";var t,r,o,n,a=function(e){for(var t,r=1,o=arguments.length;r<o;r++)for(t in arguments[r])Object.prototype.hasOwnProperty.call(arguments[r],t)&&(e[t]=arguments[r][t]);return e},i={template:'{"timestamp": "%t", "level": "%l", "message": "%m"}',messageFormatter:function(e){return e},timestampFormatter:function(e){return e.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/,"$1")},levelFormatter:function(e){return e.toUpperCase()},nameFormatter:function(e){return e||"root"}},l={apply:function(e,n){if(!e||!e.getLogger)throw new TypeError("Argument is not a root loglevel object");if(t&&o!==e.methodFactory)throw new Error("You can't reassign a plugin after appling another plugin");return t=e,n=a({},i,n),r=r||e.methodFactory,o=function(e,t,o){var a=r(e,t,o),i=-1!==n.template.indexOf("%t"),l=-1!==n.template.indexOf("%l"),m=-1!==n.template.indexOf("%n"),p=-1!==n.template.indexOf("%m");return function(){for(var t=n.template,r=arguments.length,f=Array(r),u=0;u<r;u++)f[u]=arguments[u];i&&(t=t.replace(/%t/,n.timestampFormatter(new Date))),l&&(t=t.replace(/%l/,n.levelFormatter(e))),m&&(t=t.replace(/%n/,n.nameFormatter(o))),f.length&&"string"==typeof f[0]?(p&&(t=t.replace(/%m/,n.messageFormatter(f[0]))),f[0]=t):f.unshift(t),a.apply(void 0,f)}},e.methodFactory=o,e.setLevel(e.getLevel()),e},disable:function(){if(!t)throw new Error("You can't disable a not appled plugin");if(o!==t.methodFactory)throw new Error("You can't disable a plugin after appling another plugin");t.methodFactory=r,t.setLevel(t.getLevel()),r=void 0,t=void 0}};return e&&(n=e.format,l.noConflict=function(){return e.format===l&&(e.format=n),l}),l});