loglevel-plugin-prefix
Advanced tools
Comparing version 0.7.1 to 0.7.2
@@ -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={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.methodFactory,a=e.name||"",f=n[a]||n[""]||r;return n[a]||(e.methodFactory=function(e,t,r){var f=o(e,t,r),i=n[r||""]||n[""],l=-1!==i.template.indexOf("%t"),m=-1!==i.template.indexOf("%l"),p=-1!==i.template.indexOf("%n");return function(){for(var t="",o=arguments.length,u=Array(o),c=0;c<o;c++)u[c]=arguments[c];!a&&n[r]||(i.format?t+=i.format(e,r):(t+=i.template,l&&(t=t.replace(/%t/,i.timestampFormatter(new Date))),m&&(t=t.replace(/%l/,i.levelFormatter(e))),p&&(t=t.replace(/%n/,i.nameFormatter(r)))),u.length&&"string"==typeof u[0]?u[0]=t+" "+u[0]:u.unshift(t)),f.apply(void 0,u)}}),(t=t||{}).format=t.format,n[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}({},f,t),e.setLevel(e.getLevel()),e}};return e&&(t=e.prefix,o.noConflict=function(){return e.prefix===o&&(e.prefix=t),o}),o}); | ||
!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.methodFactory,a=e.name||"",f=n[a]||n[""]||r;return n[a]||(e.methodFactory=function(e,t,r){var f=o(e,t,r),i=n[r]||n[""],l=-1!==i.template.indexOf("%t"),m=-1!==i.template.indexOf("%l"),p=-1!==i.template.indexOf("%n");return function(){for(var t="",o=arguments.length,u=Array(o),c=0;c<o;c++)u[c]=arguments[c];!a&&n[r]||(i.format?t+=i.format(e,r):(t+=i.template,l&&(t=t.replace(/%t/,i.timestampFormatter(new Date))),m&&(t=t.replace(/%l/,i.levelFormatter(e))),p&&(t=t.replace(/%n/,i.nameFormatter(r)))),u.length&&"string"==typeof u[0]?u[0]=t+" "+u[0]:u.unshift(t)),f.apply(void 0,u)}}),(t=t||{}).format=t.format,n[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}({},f,t),e.setLevel(e.getLevel()),e}};return e&&(t=e.prefix,o.noConflict=function(){return e.prefix===o&&(e.prefix=t),o}),o}); |
@@ -1,4 +0,3 @@ | ||
/* eslint-disable vars-on-top */ | ||
var log = require('loglevel'); | ||
var prefix = require('../lib/loglevel-plugin-prefix'); | ||
const log = require('loglevel'); | ||
const prefix = require('../lib/loglevel-plugin-prefix'); | ||
@@ -9,5 +8,5 @@ log.enableAll(); | ||
var child = log.getLogger('child'); | ||
prefix.apply(child, { template: '%l (%n):' }); | ||
child.info('child'); | ||
const chicken = log.getLogger('chicken'); | ||
prefix.apply(chicken, { template: '%l (%n):' }); | ||
chicken.info('chicken'); | ||
@@ -17,4 +16,23 @@ prefix.apply(log); | ||
var logger = log.getLogger('logger'); | ||
prefix.apply(logger); | ||
logger.info('logger'); | ||
const egg = log.getLogger('egg'); | ||
prefix.apply(egg); | ||
egg.info('egg'); | ||
const fn = (level, logger) => { | ||
const label = level.toUpperCase(); | ||
const name = logger || 'root'; | ||
return `${label} (${name}):`; | ||
}; | ||
prefix.apply(egg, { format: fn }); | ||
egg.info('egg'); | ||
prefix.apply(egg, { | ||
timestampFormatter(date) { | ||
return date.toISOString(); | ||
}, | ||
}); | ||
egg.info('egg'); | ||
chicken.info('chicken'); | ||
log.info('root'); |
@@ -54,3 +54,3 @@ (function (root, factory) { | ||
var originalMethod = originalFactory(methodName, logLevel, loggerName); | ||
var options = configs[loggerName || ''] || configs['']; | ||
var options = configs[loggerName] || configs['']; | ||
@@ -71,2 +71,3 @@ var hasTimestamp = options.template.indexOf('%t') !== -1; | ||
// skip the root method for child loggers to prevent duplicate logic | ||
if (name || !configs[loggerName]) { | ||
@@ -73,0 +74,0 @@ if (options.format) { |
@@ -25,3 +25,3 @@ { | ||
"license": "MIT", | ||
"version": "0.7.1", | ||
"version": "0.7.2", | ||
"main": "./lib/loglevel-plugin-prefix", | ||
@@ -28,0 +28,0 @@ "scripts": { |
111
README.md
@@ -17,10 +17,12 @@ # loglevel-plugin-prefix | ||
#### ```apply(logger, options)``` | ||
#### `apply(logger, options)` | ||
This method applies the plugin to the logger. | ||
**logger** - loglevel logger | ||
#### Parameters | ||
**options** - configuration object | ||
`logger` - a loglevel logger | ||
`options` - an optional configuration object | ||
```javascript | ||
@@ -41,13 +43,10 @@ var defaults = { | ||
Plugin formats the prefix using **template** option as a printf-like format. | ||
Plugin formats the prefix using **template** option as a printf-like format. The **template** is a string containing zero or more placeholder tokens. Each placeholder token is replaced with the value from loglevel messages parameters. Supported placeholders are: | ||
The **template** is a string containing zero or more placeholder tokens. Each placeholder token is replaced with the value from loglevel messages parameters. Supported placeholders are: | ||
- `%t` - timestamp of message | ||
- `%l` - level of message | ||
- `%n` - name of logger | ||
- %t - timestamp of message | ||
- %l - level of message | ||
- %n - name of logger | ||
The **timestampFormatter**, **levelFormatter** and **nameFormatter** is a functions for formatting corresponding values. | ||
The **timestampFormatter**, **levelFormatter** and **nameFormatter** is a functions for formatting corresponding values | ||
Alternatively, you can use **format** option. This is a function with two arguments (level and logger), which should return a prefix string. If the format option is present, the other options are ignored. | ||
@@ -104,4 +103,4 @@ | ||
```javascript | ||
const log = require('loglevel'); | ||
const prefix = require('loglevel-plugin-prefix'); | ||
import log from 'loglevel'; | ||
import prefix from 'loglevel-plugin-prefix'; | ||
@@ -137,59 +136,41 @@ log.enableAll(); | ||
## Example | ||
## Option inheritance | ||
```javascript | ||
// moduleA.js | ||
var log = require('loglevel'); | ||
import log from 'loglevel'; | ||
import prefix from 'loglevel-plugin-prefix'; | ||
module.exports = function () { | ||
log.warn('message from moduleA'); | ||
} | ||
``` | ||
log.enableAll(); | ||
```javascript | ||
// moduleB.js | ||
var log = require('loglevel'); | ||
log.info('root'); | ||
var logger = log.getLogger('moduleB'); | ||
const chicken = log.getLogger('chicken'); | ||
prefix.apply(chicken, { template: '%l (%n):' }); | ||
chicken.info('chicken'); | ||
module.exports = function () { | ||
logger.warn('message from moduleB'); | ||
} | ||
``` | ||
prefix.apply(log); | ||
log.info('root'); | ||
```javascript | ||
// moduleC.js | ||
var log = require('loglevel'); | ||
const egg = log.getLogger('egg'); | ||
prefix.apply(egg); | ||
egg.info('egg'); | ||
module.exports = function () { | ||
var logger = log.getLogger('moduleC'); | ||
logger.warn('message from moduleC'); | ||
} | ||
``` | ||
const fn = (level, logger) => { | ||
const label = level.toUpperCase(); | ||
const name = logger || 'root'; | ||
return `${label} (${name}):`; | ||
}; | ||
```javascript | ||
// main.js | ||
var log = require('loglevel'); | ||
var prefix = require('loglevel-plugin-prefix'); | ||
prefix.apply(egg, { format: fn }); | ||
egg.info('egg'); | ||
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):' }); | ||
log.warn('message from root %s prefixing', 'after'); | ||
a(); | ||
b(); | ||
c(); | ||
prefix.apply(log, { | ||
template: '[%t] %l:', | ||
timestampFormatter: function (date) { return date.toISOString() } | ||
prefix.apply(egg, { | ||
timestampFormatter(date) { | ||
return date.toISOString(); | ||
}, | ||
}); | ||
egg.info('egg'); | ||
log.warn('message from root after reapplying'); | ||
chicken.info('chicken'); | ||
log.info('root'); | ||
``` | ||
@@ -199,8 +180,10 @@ | ||
``` | ||
message from root before prefixing | ||
[16:53:46] WARN (root): message from root after prefixing | ||
[16:53:46] WARN (root): message from moduleA | ||
message from moduleB | ||
[16:53:46] WARN (moduleC): message from moduleC | ||
[2017-05-29T12:53:46.000Z] WARN: message from root after reapplying | ||
root | ||
INFO (chicken): chicken | ||
[16:53:46] INFO: root | ||
[16:53:46] INFO: egg | ||
INFO (egg): egg | ||
[2017-05-29T12:53:46.000Z] INFO: egg | ||
INFO (chicken): chicken | ||
[16:53:46] INFO: root | ||
``` |
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
15762
11
242
185