loglevel-plugin-prefix
Advanced tools
Comparing version 0.4.2 to 0.5.0
@@ -1,1 +0,1 @@ | ||
!function(e,t){if("function"==typeof define&&define.amd)define(["module","exports"],t);else if("undefined"!=typeof exports)t(module,exports);else{var r={exports:{}};t(r,r.exports),e.prefix=r.exports}}(this,function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=!1,n=function(e){for(var t=1;t<arguments.length;t+=1)for(var r in arguments[t])Object.prototype.hasOwnProperty.call(arguments[t],r)&&(e[r]=arguments[t][r]);return e},o={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"}},i=function(e,t){if(!e||!e.getLogger)throw new TypeError("Argument is not a root loglevel object");if(r)throw new TypeError("You can assign a prefix only one time");r=!0,t=n({},o,t);var i=e.methodFactory;return e.methodFactory=function(e,r,n){var o=i(e,r,n),a=-1!==t.template.indexOf("%t"),l=-1!==t.template.indexOf("%l"),p=-1!==t.template.indexOf("%n");return function(){for(var r=arguments.length,i=Array(r),f=0;f<r;f++)i[f]=arguments[f];var u=t.template;a&&(u=u.replace(/%t/,t.timestampFormatter(new Date))),l&&(u=u.replace(/%l/,t.levelFormatter(e))),p&&(u=u.replace(/%n/,t.nameFormatter(n))),i.length&&"string"==typeof i[0]?i[0]=u+" "+i[0]:i.unshift(u),o.apply(void 0,i)}},e.setLevel(e.getLevel()),e},prefix={};prefix.apply=i,prefix.name="loglevel-plugin-prefix";var a=window?window.prefix:void 0;prefix.noConflict=function(){return window&&window.prefix===prefix&&(window.prefix=a),prefix},t.default=prefix,e.exports=t.default}); | ||
!function(e,t){if("function"==typeof define&&define.amd)define(["module","exports"],t);else if("undefined"!=typeof exports)t(module,exports);else{var r={exports:{}};t(r,r.exports),e.prefix=r.exports}}(this,function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=function(e){for(var t=1;t<arguments.length;t+=1)for(var r in arguments[t])Object.prototype.hasOwnProperty.call(arguments[t],r)&&(e[r]=arguments[t][r]);return e},o={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=void 0,i=void 0,a=void 0,l=function(e,t){if(!e||!e.getLogger)throw new TypeError("Argument is not a root loglevel object");if(n&&a!==e.methodFactory)throw new Error("You can't reassign a plugin after appling another plugin");return n=e,t=r({},o,t),i=i||e.methodFactory,a=function(e,r,o){var n=i(e,r,o),a=-1!==t.template.indexOf("%t"),l=-1!==t.template.indexOf("%l"),p=-1!==t.template.indexOf("%n");return function(){for(var r=arguments.length,i=Array(r),f=0;f<r;f++)i[f]=arguments[f];var d=t.template;a&&(d=d.replace(/%t/,t.timestampFormatter(new Date))),l&&(d=d.replace(/%l/,t.levelFormatter(e))),p&&(d=d.replace(/%n/,t.nameFormatter(o))),i.length&&"string"==typeof i[0]?i[0]=d+" "+i[0]:i.unshift(d),n.apply(void 0,i)}},e.methodFactory=a,e.setLevel(e.getLevel()),e},p=function(){if(!n)throw new Error("You can't disable a not appled plugin");if(a!==n.methodFactory)throw new Error("You can't disable a plugin after appling another plugin");n.methodFactory=i,n.setLevel(n.getLevel()),i=void 0,n=void 0},prefix={};prefix.apply=l,prefix.disable=p;var f="undefined"!=typeof window?window.prefix:void 0;prefix.noConflict=function(){return"undefined"!=typeof window&&window.prefix===prefix&&(window.prefix=f),prefix},t.default=prefix,e.exports=t.default}); |
@@ -19,4 +19,2 @@ (function (global, factory) { | ||
}); | ||
var isAssigned = false; | ||
var merge = function merge(target) { | ||
@@ -46,2 +44,6 @@ for (var i = 1; i < arguments.length; i += 1) { | ||
var loglevel = void 0; | ||
var originalFactory = void 0; | ||
var pluginFactory = void 0; | ||
var apply = function apply(logger, options) { | ||
@@ -52,12 +54,13 @@ if (!logger || !logger.getLogger) { | ||
if (isAssigned) { | ||
throw new TypeError('You can assign a prefix only one time'); | ||
if (loglevel && pluginFactory !== logger.methodFactory) { | ||
throw new Error("You can't reassign a plugin after appling another plugin"); | ||
} | ||
isAssigned = true; | ||
loglevel = logger; | ||
options = merge({}, defaults, options); | ||
var originalFactory = logger.methodFactory; | ||
logger.methodFactory = function methodFactory(methodName, logLevel, loggerName) { | ||
originalFactory = originalFactory || logger.methodFactory; | ||
pluginFactory = function methodFactory(methodName, logLevel, loggerName) { | ||
var rawMethod = originalFactory(methodName, logLevel, loggerName); | ||
@@ -89,2 +92,3 @@ | ||
logger.methodFactory = pluginFactory; | ||
logger.setLevel(logger.getLevel()); | ||
@@ -94,9 +98,25 @@ 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"); | ||
} | ||
loglevel.methodFactory = originalFactory; | ||
loglevel.setLevel(loglevel.getLevel()); | ||
originalFactory = undefined; | ||
loglevel = undefined; | ||
}; | ||
var prefix = {}; | ||
prefix.apply = apply; | ||
prefix.name = 'loglevel-plugin-prefix'; | ||
prefix.disable = disable; | ||
var save = window ? window.prefix : undefined; | ||
var save = typeof window !== 'undefined' ? window.prefix : undefined; | ||
prefix.noConflict = function () { | ||
if (window && window.prefix === prefix) { | ||
if (typeof window !== 'undefined' && window.prefix === prefix) { | ||
window.prefix = save; | ||
@@ -103,0 +123,0 @@ } |
@@ -24,3 +24,3 @@ { | ||
"license": "MIT", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"main": "./lib/loglevel-plugin-prefix", | ||
@@ -38,2 +38,3 @@ "babel": { | ||
"eslint": "eslint src/prefix.js", | ||
"test": "mocha", | ||
"babel": "babel ./src/prefix.js --out-file ./lib/loglevel-plugin-prefix.js", | ||
@@ -49,7 +50,13 @@ "uglifyjs": "uglifyjs ./lib/loglevel-plugin-prefix.js -o ./dist/loglevel-plugin-prefix.min.js -c -m reserved=[prefix]", | ||
"babel-preset-env": "^1.5.1", | ||
"chai": "^4.0.2", | ||
"eslint": "^3.19.0", | ||
"eslint-config-airbnb-base": "^11.2.0", | ||
"eslint-plugin-chai-friendly": "^0.3.6", | ||
"eslint-plugin-import": "^2.3.0", | ||
"loglevel": "^1.4.1", | ||
"loglevel-plugin-mock": "^0.1.0", | ||
"mocha": "^3.4.2", | ||
"sinon": "^2.3.2", | ||
"uglify-js": "^3.0.11" | ||
} | ||
} | ||
} |
106
README.md
@@ -16,2 +16,4 @@ # loglevel-plugin-prefix | ||
This method applies the plugin to the logger. | ||
**log** - root logger, imported from loglevel package | ||
@@ -40,2 +42,8 @@ | ||
```javascript | ||
disable(); | ||
``` | ||
This method cancels the effect of the plugin. | ||
## Base usage | ||
@@ -53,3 +61,4 @@ | ||
var logger = log.noConflict(); | ||
prefix.noConflict().apply(logger); | ||
var prefixer = prefix.noConflict(); | ||
prefixer.apply(logger); | ||
logger.warn('prefixed message'); | ||
@@ -61,3 +70,3 @@ </script> | ||
``` | ||
[12:53:46] WARN: prefixed message | ||
[16:53:46] WARN: prefixed message | ||
``` | ||
@@ -67,3 +76,2 @@ | ||
```javascript | ||
import log from 'loglevel'; | ||
@@ -78,3 +86,2 @@ import prefix from 'loglevel-plugin-prefix'; | ||
```javascript | ||
var log = require('loglevel'); | ||
@@ -98,10 +105,10 @@ var prefix = require('loglevel-plugin-prefix'); | ||
```javascript | ||
import log from 'loglevel'; | ||
import prefix from 'loglevel-plugin-prefix'; | ||
var log = require('loglevel'); | ||
var prefix = require('loglevel-plugin-prefix'); | ||
prefix.apply(log, { | ||
template: '[%t] %l (%n) static text:', | ||
timestampFormatter: date => date.toISOString(), | ||
levelFormatter: level => level.charAt(0).toUpperCase() + level.substr(1), | ||
nameFormatter: name => name || 'global' | ||
timestampFormatter: function (date) { return date.toISOString() }, | ||
levelFormatter: function (level) { return level.charAt(0).toUpperCase() + level.substr(1) }, | ||
nameFormatter: function (name) { return name || 'global' } | ||
}); | ||
@@ -114,3 +121,3 @@ | ||
``` | ||
[2017-05-29T16:53:46.000Z] Warn (global) static text: prefixed message | ||
[2017-05-29T12:53:46.000Z] Warn (global) static text: prefixed message | ||
``` | ||
@@ -122,5 +129,5 @@ | ||
// moduleA.js | ||
import log from 'loglevel'; | ||
var log = require('loglevel'); | ||
export default function () { | ||
module.exports = function () { | ||
log.warn('message from moduleA'); | ||
@@ -132,7 +139,7 @@ } | ||
// moduleB.js | ||
import log from 'loglevel'; | ||
var log = require('loglevel'); | ||
const logger = log.getLogger('moduleB'); | ||
var logger = log.getLogger('moduleB'); | ||
export default function () { | ||
module.exports = function () { | ||
logger.warn('message from moduleB'); | ||
@@ -144,6 +151,6 @@ } | ||
// moduleC.js | ||
import log from 'loglevel'; | ||
var log = require('loglevel'); | ||
export default function () { | ||
const logger = log.getLogger('moduleC'); | ||
module.exports = function () { | ||
var logger = log.getLogger('moduleC'); | ||
logger.warn('message from moduleC'); | ||
@@ -155,14 +162,12 @@ } | ||
// main.js | ||
import log from 'loglevel'; | ||
import prefix from 'loglevel-plugin-prefix'; | ||
var log = require('loglevel'); | ||
var prefix = require('loglevel-plugin-prefix'); | ||
import a from './moduleA'; | ||
import b from './moduleB'; | ||
import c from './moduleC'; | ||
var a = require('./moduleA'); | ||
var b = require('./moduleB'); | ||
var c = require('./moduleC'); | ||
log.warn('message from root %s prefixing', 'before'); | ||
prefix.apply(log, { | ||
template: '[%t] %l (%n):', | ||
}); | ||
prefix.apply(log, { template: '[%t] %l (%n):' }); | ||
@@ -174,2 +179,13 @@ log.warn('message from root %s prefixing', 'after'); | ||
c(); | ||
prefix.apply(log, { | ||
template: '[%t] %l:', | ||
timestampFormatter: function (date) { return date.toISOString() } | ||
}); | ||
log.warn('message from root after reapplying'); | ||
prefix.disable(); | ||
log.warn('message from root after disabling'); | ||
``` | ||
@@ -184,2 +200,4 @@ | ||
[16:53:46] WARN (moduleC): message from moduleC | ||
[2017-05-29T12:53:46.000Z] WARN: message from root after reapplying | ||
message from root after disabling | ||
``` | ||
@@ -190,12 +208,18 @@ | ||
```javascript | ||
import log from 'loglevel'; | ||
import prefix from 'loglevel-plugin-prefix'; | ||
var log = require('loglevel'); | ||
var prefix = require('loglevel-plugin-prefix'); | ||
var mock = require('loglevel-plugin-mock'); | ||
log.setLevel('info'); | ||
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, { timestampFormatter: date => date.toISOString() }); | ||
prefix.apply(log, { template: '[%t] %l (%n):' }); | ||
} catch(e) { | ||
@@ -205,6 +229,12 @@ log.error(e); | ||
log.info('message from root after pre-prefixing'); | ||
try { | ||
prefix.disable(); | ||
} catch(e) { | ||
log.error(e); | ||
}; | ||
const logger = log.getLogger('child'); | ||
mock.disable(); | ||
var logger = log.getLogger('child'); | ||
try { | ||
@@ -217,2 +247,3 @@ prefix.apply(logger, { template: '[%t] %l (%n):' }); | ||
logger.info('message from child logger'); | ||
``` | ||
@@ -223,6 +254,7 @@ | ||
[16:53:46] INFO: message from root after prefixing | ||
[16:53:46] ERROR: TypeError: You can assign a prefix only one time | ||
[16:53:46] INFO: message from root after pre-prefixing | ||
[16:53:46] ERROR: TypeError: Argument is not a root loglevel object | ||
[16:53:46] INFO: message from child logger | ||
[2017-05-29T12:53:46.000Z] INFO: message from root after pre-prefixing | ||
[2017-05-29T12:53:46.000Z] ERROR: You can't reassign a plugin after appling another plugin | ||
[2017-05-29T12:53:46.000Z] 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 | ||
``` |
@@ -1,3 +0,1 @@ | ||
let isAssigned = false; | ||
const merge = function merge(target) { | ||
@@ -21,2 +19,6 @@ for (let i = 1; i < arguments.length; i += 1) { | ||
let loglevel; | ||
let originalFactory; | ||
let pluginFactory; | ||
const apply = function apply(logger, options) { | ||
@@ -27,12 +29,13 @@ if (!logger || !logger.getLogger) { | ||
if (isAssigned) { | ||
throw new TypeError('You can assign a prefix only one time'); | ||
if (loglevel && pluginFactory !== logger.methodFactory) { | ||
throw new Error("You can't reassign a plugin after appling another plugin"); | ||
} | ||
isAssigned = true; | ||
loglevel = logger; | ||
options = merge({}, defaults, options); | ||
const originalFactory = logger.methodFactory; | ||
logger.methodFactory = function methodFactory(methodName, logLevel, loggerName) { | ||
originalFactory = originalFactory || logger.methodFactory; | ||
pluginFactory = function methodFactory(methodName, logLevel, loggerName) { | ||
const rawMethod = originalFactory(methodName, logLevel, loggerName); | ||
@@ -60,2 +63,3 @@ | ||
logger.methodFactory = pluginFactory; | ||
logger.setLevel(logger.getLevel()); | ||
@@ -65,9 +69,25 @@ return logger; | ||
const 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"); | ||
} | ||
loglevel.methodFactory = originalFactory; | ||
loglevel.setLevel(loglevel.getLevel()); | ||
originalFactory = undefined; | ||
loglevel = undefined; | ||
}; | ||
const prefix = {}; | ||
prefix.apply = apply; | ||
prefix.name = 'loglevel-plugin-prefix'; | ||
prefix.disable = disable; | ||
const save = window ? window.prefix : undefined; | ||
const save = typeof window !== 'undefined' ? window.prefix : undefined; | ||
prefix.noConflict = () => { | ||
if (window && window.prefix === prefix) { | ||
if (typeof window !== 'undefined' && window.prefix === prefix) { | ||
window.prefix = save; | ||
@@ -74,0 +94,0 @@ } |
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
20538
10
302
243
14