loglevel-plugin-prefix
Advanced tools
Comparing version 0.5.3 to 0.6.0
@@ -1,1 +0,1 @@ | ||
!function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof module&&module.exports?module.exports=t():e.prefix=t(e)}(this,function(e){"use strict";var t,r,n,o,a=function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(t in arguments[r])Object.prototype.hasOwnProperty.call(arguments[r],t)&&(e[t]=arguments[r][t]);return e},i={template:"[%t] %l:",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,o){if(!e||!e.getLogger)throw new TypeError("Argument is not a root loglevel object");if(t&&n!==e.methodFactory)throw new Error("You can't reassign a plugin after appling another plugin");return t=e,o=a({},i,o),r=r||e.methodFactory,n=function(e,t,n){var a=r(e,t,n),i=-1!==o.template.indexOf("%t"),l=-1!==o.template.indexOf("%l"),p=-1!==o.template.indexOf("%n");return function(){for(var t=o.template,r=arguments.length,f=Array(r),u=0;u<r;u++)f[u]=arguments[u];i&&(t=t.replace(/%t/,o.timestampFormatter(new Date))),l&&(t=t.replace(/%l/,o.levelFormatter(e))),p&&(t=t.replace(/%n/,o.nameFormatter(n))),f.length&&"string"==typeof f[0]?f[0]=t+" "+f[0]:f.unshift(t),a.apply(void 0,f)}},e.methodFactory=n,e.setLevel(e.getLevel()),e},disable:function(){if(!t)throw new Error("You can't disable a not appled plugin");if(n!==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&&(o=e.prefix,l.noConflict=function(){return e.prefix===l&&(e.prefix=o),l}),l}); | ||
!function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof module&&module.exports?module.exports=t():e.prefix=t(e)}(this,function(e){"use strict";var t,r={template:"[%t] %l:",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"}},n={},o={apply:function(e,t){if(!e||!e.setLevel)throw new TypeError("Argument is not a logger");var o=!!e.getLogger,a=e.methodFactory,i=e.name||"",l=n[i]||n[""]||r;return n[i]||(e.methodFactory=function(e,t,r){var i=a(e,t,r),l=n[r||""],f=-1!==l.template.indexOf("%t"),p=-1!==l.template.indexOf("%l"),m=-1!==l.template.indexOf("%n");return function(){for(var t=l.template,a=arguments.length,u=Array(a),c=0;c<a;c++)u[c]=arguments[c];o&&n[r]||(f&&(t=t.replace(/%t/,l.timestampFormatter(new Date))),p&&(t=t.replace(/%l/,l.levelFormatter(e))),m&&(t=t.replace(/%n/,l.nameFormatter(r))),u.length&&"string"==typeof u[0]?u[0]=t+" "+u[0]:u.unshift(t)),i.apply(void 0,u)}}),n[i]=function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(t in arguments[r])Object.prototype.hasOwnProperty.call(arguments[r],t)&&(e[t]=arguments[r][t]);return e}({},l,t),e.setLevel(e.getLevel()),e}};return e&&(t=e.prefix,o.noConflict=function(){return e.prefix===o&&(e.prefix=t),o}),o}); |
@@ -39,24 +39,20 @@ (function (root, factory) { | ||
var loglevel; | ||
var originalFactory; | ||
var pluginFactory; | ||
var loggers = {}; | ||
var apply = function apply(logger, options) { | ||
if (!logger || !logger.getLogger) { | ||
throw new TypeError('Argument is not a root loglevel object'); | ||
var apply = function apply(logger, config) { | ||
if (!logger || !logger.setLevel) { | ||
throw new TypeError('Argument is not a logger'); | ||
} | ||
if (loglevel && pluginFactory !== logger.methodFactory) { | ||
throw new Error("You can't reassign a plugin after appling another plugin"); | ||
} | ||
/* eslint-disable vars-on-top */ | ||
var isRoot = !!logger.getLogger; | ||
var originalFactory = logger.methodFactory; | ||
var name = logger.name || ''; | ||
var parent = loggers[name] || loggers[''] || defaults; | ||
/* eslint-enable vars-on-top */ | ||
loglevel = logger; | ||
function methodFactory(methodName, logLevel, loggerName) { | ||
var originalMethod = originalFactory(methodName, logLevel, loggerName); | ||
var options = loggers[loggerName || '']; | ||
options = merge({}, defaults, options); | ||
originalFactory = originalFactory || logger.methodFactory; | ||
pluginFactory = function methodFactory(methodName, logLevel, loggerName) { | ||
var rawMethod = originalFactory(methodName, logLevel, loggerName); | ||
var hasTimestamp = options.template.indexOf('%t') !== -1; | ||
@@ -76,40 +72,30 @@ var hasLevel = options.template.indexOf('%l') !== -1; | ||
if (hasTimestamp) content = content.replace(/%t/, options.timestampFormatter(new Date())); | ||
if (hasLevel) content = content.replace(/%l/, options.levelFormatter(methodName)); | ||
if (hasName) content = content.replace(/%n/, options.nameFormatter(loggerName)); | ||
if (!(isRoot && loggers[loggerName])) { | ||
if (hasTimestamp) content = content.replace(/%t/, options.timestampFormatter(new Date())); | ||
if (hasLevel) content = content.replace(/%l/, options.levelFormatter(methodName)); | ||
if (hasName) content = content.replace(/%n/, options.nameFormatter(loggerName)); | ||
if (args.length && typeof args[0] === 'string') { | ||
// concat prefix with first argument to support string substitutions | ||
args[0] = '' + content + ' ' + args[0]; | ||
} else { | ||
args.unshift(content); | ||
if (args.length && typeof args[0] === 'string') { | ||
// concat prefix with first argument to support string substitutions | ||
args[0] = '' + content + ' ' + args[0]; | ||
} else { | ||
args.unshift(content); | ||
} | ||
} | ||
rawMethod.apply(undefined, args); | ||
originalMethod.apply(undefined, args); | ||
}; | ||
}; | ||
logger.methodFactory = pluginFactory; | ||
logger.setLevel(logger.getLevel()); | ||
return logger; | ||
}; | ||
var disable = function disable() { | ||
if (!loglevel) { | ||
throw new Error("You can't disable a not appled plugin"); | ||
} | ||
if (pluginFactory !== loglevel.methodFactory) { | ||
throw new Error("You can't disable a plugin after appling another plugin"); | ||
if (!loggers[name]) { | ||
logger.methodFactory = methodFactory; | ||
} | ||
loggers[name] = merge({}, parent, config); | ||
loglevel.methodFactory = originalFactory; | ||
loglevel.setLevel(loglevel.getLevel()); | ||
originalFactory = undefined; | ||
loglevel = undefined; | ||
logger.setLevel(logger.getLevel()); | ||
return logger; | ||
}; | ||
var api = { | ||
apply: apply, | ||
disable: disable | ||
apply: apply | ||
}; | ||
@@ -116,0 +102,0 @@ |
@@ -25,3 +25,3 @@ { | ||
"license": "MIT", | ||
"version": "0.5.3", | ||
"version": "0.6.0", | ||
"main": "./lib/loglevel-plugin-prefix", | ||
@@ -35,12 +35,12 @@ "scripts": { | ||
"devDependencies": { | ||
"chai": "^4.0.2", | ||
"chai": "^4.1.2", | ||
"eslint": "^3.19.0", | ||
"eslint-config-airbnb-base": "^11.2.0", | ||
"eslint-config-airbnb-base": "^11.3.2", | ||
"eslint-plugin-chai-friendly": "^0.3.6", | ||
"loglevel": "^1.4.1", | ||
"loglevel": "^1.6.0", | ||
"loglevel-plugin-mock": "^0.1.0", | ||
"mocha": "^3.4.2", | ||
"sinon": "^2.3.2", | ||
"uglify-js": "^3.0.11" | ||
"mocha": "^3.5.3", | ||
"sinon": "^2.4.1", | ||
"uglify-js": "^3.3.4" | ||
} | ||
} |
@@ -18,3 +18,3 @@ # loglevel-plugin-prefix | ||
**log** - root logger, imported from loglevel package | ||
**log** - loglevel logger | ||
@@ -48,8 +48,2 @@ **options** - configuration object | ||
```javascript | ||
disable(); | ||
``` | ||
This method cancels the effect of the plugin. | ||
## Base usage | ||
@@ -179,6 +173,2 @@ | ||
log.warn('message from root after reapplying'); | ||
prefix.disable(); | ||
log.warn('message from root after disabling'); | ||
``` | ||
@@ -194,56 +184,2 @@ | ||
[2017-05-29T12:53:46.000Z] WARN: message from root after reapplying | ||
message from root after disabling | ||
``` | ||
## Errors | ||
```javascript | ||
var log = require('loglevel'); | ||
var prefix = require('loglevel-plugin-prefix'); | ||
var mock = require('loglevel-plugin-mock'); | ||
log.enableAll(); | ||
prefix.apply(log); | ||
log.info('message from root after prefixing'); | ||
prefix.apply(log, { timestampFormatter: function (date) { return date.toISOString() } }); | ||
log.info('message from root after pre-prefixing'); | ||
mock.apply(log); | ||
try { | ||
prefix.apply(log, { template: '[%t] %l (%n):' }); | ||
} catch(e) { | ||
log.error(e); | ||
}; | ||
try { | ||
prefix.disable(); | ||
} catch(e) { | ||
log.error(e); | ||
}; | ||
mock.disable(); | ||
var logger = log.getLogger('child'); | ||
try { | ||
prefix.apply(logger, { template: '[%t] %l (%n):' }); | ||
} catch(e) { | ||
logger.error(e); | ||
}; | ||
logger.info('message from child logger'); | ||
``` | ||
Output | ||
``` | ||
[16:53:46] INFO: message from root after prefixing | ||
[2017-05-29T12:53:46.000Z] INFO: message from root after pre-prefixing | ||
[2017-05-29T12:53:46.000Z] ERROR: Error: You can't reassign a plugin after appling another plugin | ||
[2017-05-29T12:53:46.000Z] ERROR: Error: You can't disable a plugin after appling another plugin | ||
[2017-05-29T12:53:46.000Z] ERROR: TypeError: Argument is not a root loglevel object | ||
[2017-05-29T12:53:46.000Z] INFO: message from child logger | ||
``` |
135
test/test.js
@@ -1,105 +0,45 @@ | ||
const expect = require('chai').expect; | ||
const loglevel = require('loglevel'); | ||
const other = require('loglevel-plugin-mock'); | ||
const sinon = require('sinon'); | ||
const prefix = require('../lib/loglevel-plugin-prefix'); | ||
const expect = require("chai").expect; | ||
const loglevel = require("loglevel"); | ||
const other = require("loglevel-plugin-mock"); | ||
const sinon = require("sinon"); | ||
const prefix = require("../lib/loglevel-plugin-prefix"); | ||
const spy = sinon.spy(); | ||
describe('', () => { | ||
describe("", () => { | ||
beforeEach(() => { | ||
/* eslint-disable no-empty */ | ||
try { | ||
prefix.disable(); | ||
} catch (ignore) {} | ||
try { | ||
other.disable(); | ||
} catch (ignore) {} | ||
try { | ||
prefix.disable(); | ||
} catch (ignore) {} | ||
/* eslint-enable no-empty */ | ||
spy.reset(); | ||
}); | ||
describe('API', () => { | ||
it('Methods', () => { | ||
expect(prefix).to.have.property('apply').with.be.a('function'); | ||
expect(prefix).to.have.property('disable').with.be.a('function'); | ||
expect(prefix).not.to.have.property('noConflict'); | ||
describe("API", () => { | ||
it("Methods", () => { | ||
expect(prefix).to.have.property("apply").with.be.a("function"); | ||
expect(prefix).not.to.have.property("noConflict"); | ||
}); | ||
it('Empty arguments', () => { | ||
expect(prefix.apply).to.throw(TypeError, 'Argument is not a root loglevel object'); | ||
it("Empty arguments", () => { | ||
expect(prefix.apply).to.throw(TypeError, "Argument is not a logger"); | ||
}); | ||
it('Not root loglevel argument', () => { | ||
expect(() => prefix.apply(loglevel.getLogger('log'))).to.throw( | ||
TypeError, | ||
'Argument is not a root loglevel object' | ||
); | ||
it("Incorrect argument", () => { | ||
expect(() => prefix.apply("logger")).to.throw(TypeError, "Argument is not a logger"); | ||
}); | ||
it('Disabling a not appled plugin should throw an exception', () => { | ||
expect(prefix.disable).to.throw(Error, "You can't disable a not appled plugin"); | ||
}); | ||
it('Right applying', () => { | ||
it("Applying", () => { | ||
expect(() => prefix.apply(loglevel)).to.not.throw(); | ||
}); | ||
it('Right disabling', () => { | ||
prefix.apply(loglevel); | ||
expect(prefix.disable).to.not.throw(); | ||
}); | ||
it('Reapplying without applying other plugin', () => { | ||
prefix.apply(loglevel); | ||
it("Reapplying", () => { | ||
expect(() => prefix.apply(loglevel)).to.not.throw(); | ||
}); | ||
}); | ||
it('Reapplying after using another plugin should throw an exception', () => { | ||
prefix.apply(loglevel); | ||
other.apply(loglevel); | ||
describe("Prefix", () => { | ||
other.apply(loglevel, { method: spy }); | ||
const child = loglevel.getLogger("child"); | ||
child.enableAll(); | ||
expect(() => prefix.apply(loglevel)).to.throw( | ||
Error, | ||
"You can't reassign a plugin after appling another plugin" | ||
); | ||
}); | ||
it('Disabling after using another plugin should throw an exception', () => { | ||
it("All methods of the previous plugin should be called", () => { | ||
prefix.apply(loglevel); | ||
other.apply(loglevel); | ||
expect(prefix.disable).to.throw( | ||
Error, | ||
"You can't disable a plugin after appling another plugin" | ||
); | ||
}); | ||
it('Reapplying after disabling another plugin should not thrown an exception', () => { | ||
prefix.apply(loglevel); | ||
other.apply(loglevel); | ||
other.disable(); | ||
expect(() => prefix.apply(loglevel)).to.not.throw(); | ||
}); | ||
it('Disabling after disabling another plugin should not thrown an exception', () => { | ||
prefix.apply(loglevel); | ||
other.apply(loglevel); | ||
other.disable(); | ||
expect(prefix.disable).to.not.throw(); | ||
}); | ||
}); | ||
describe('Prefix', () => { | ||
it('All methods of the previous plugin should be called', () => { | ||
other.apply(loglevel, { method: spy }); | ||
prefix.apply(loglevel); | ||
loglevel.enableAll(); | ||
@@ -114,10 +54,31 @@ loglevel.trace(); | ||
it('The prefix must be combined with the first argument, if it is a string', () => { | ||
other.apply(loglevel, { method: spy }); | ||
prefix.apply(loglevel, { template: '%l:' }); | ||
it("Child logger", () => { | ||
prefix.apply(child, { template: "%l (%n):" }); | ||
child.info("test"); | ||
expect(spy.calledWith("INFO (child): test")).to.be.true; | ||
}); | ||
loglevel.warn('foo %s', 'bar'); | ||
expect(spy.calledWith('WARN: foo %s', 'bar')).to.be.true; | ||
it("Child reapplyng", () => { | ||
prefix.apply(child, { | ||
levelFormatter: function(level) { | ||
return level; | ||
} | ||
}); | ||
child.info("test"); | ||
expect(spy.calledWith("info (child): test")).to.be.true; | ||
}); | ||
it("Root reapplyng", () => { | ||
prefix.apply(loglevel, { template: "%l (%n):" }); | ||
loglevel.info("test"); | ||
expect(spy.calledWith("INFO (root): test")).to.be.true; | ||
}); | ||
it("The prefix must be combined with the first argument, if it is a string", () => { | ||
prefix.apply(loglevel, { template: "%l:" }); | ||
loglevel.warn("foo %s", "bar"); | ||
expect(spy.calledWith("WARN: foo %s", "bar")).to.be.true; | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13650
8
192
182