loglevel-plugin-prefix
Advanced tools
Comparing version 0.7.2 to 0.8.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={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,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"},format:void 0},n={},a={reg:function(e){if(!e||!e.getLogger)throw new TypeError("Argument is not a root logger");t=e},apply:function(e,r){if(!e||!e.setLevel)throw new TypeError("Argument is not a logger");var a=e.methodFactory,l=e.name||"",i=n[l]||n[""]||o;return n[l]||(e.methodFactory=function(e,t,r){var o=a(e,t,r),i=n[r]||n[""],f=-1!==i.template.indexOf("%t"),p=-1!==i.template.indexOf("%l"),m=-1!==i.template.indexOf("%n");return function(){for(var t="",a=arguments.length,u=Array(a),c=0;c<a;c++)u[c]=arguments[c];if(l||!n[r]){var g=i.timestampFormatter(new Date),s=i.levelFormatter(e),d=i.nameFormatter(r);i.format?t+=i.format(s,d,g):(t+=i.template,f&&(t=t.replace(/%t/,g)),p&&(t=t.replace(/%l/,s)),m&&(t=t.replace(/%n/,d))),u.length&&"string"==typeof u[0]?u[0]=t+" "+u[0]:u.unshift(t)}o.apply(void 0,u)}}),(r=r||{}).template&&(r.format=void 0),n[l]=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,r),e.setLevel(e.getLevel()),t||e.warn("It is necessary to call the function reg() of loglevel-plugin-prefix before calling apply. From the next release, it will throw an error. See more: https://github.com/kutuluk/loglevel-plugin-prefix/blob/master/README.md"),e}};return e&&(r=e.prefix,a.noConflict=function(){return e.prefix===a&&(e.prefix=r),a}),a}); |
@@ -0,4 +1,6 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
const log = require('loglevel'); | ||
const prefix = require('../lib/loglevel-plugin-prefix'); | ||
prefix.reg(log); | ||
log.enableAll(); | ||
@@ -9,2 +11,4 @@ | ||
const chicken = log.getLogger('chicken'); | ||
chicken.info('chicken'); | ||
prefix.apply(chicken, { template: '%l (%n):' }); | ||
@@ -17,10 +21,5 @@ chicken.info('chicken'); | ||
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}):`; | ||
}; | ||
const fn = (level, logger) => `${level} (${logger}):`; | ||
@@ -31,4 +30,4 @@ prefix.apply(egg, { format: fn }); | ||
prefix.apply(egg, { | ||
timestampFormatter(date) { | ||
return date.toISOString(); | ||
levelFormatter(level) { | ||
return level.toLowerCase(); | ||
}, | ||
@@ -35,0 +34,0 @@ }); |
@@ -36,7 +36,16 @@ (function (root, factory) { | ||
return name || 'root'; | ||
} | ||
}, | ||
format: undefined | ||
}; | ||
var loglevel; | ||
var configs = {}; | ||
var reg = function (rootLogger) { | ||
if (!rootLogger || !rootLogger.getLogger) { | ||
throw new TypeError('Argument is not a root logger'); | ||
} | ||
loglevel = rootLogger; | ||
}; | ||
var apply = function (logger, config) { | ||
@@ -73,11 +82,18 @@ if (!logger || !logger.setLevel) { | ||
if (name || !configs[loggerName]) { | ||
/* eslint-disable vars-on-top */ | ||
var timestamp = options.timestampFormatter(new Date()); | ||
var level = options.levelFormatter(methodName); | ||
var lname = options.nameFormatter(loggerName); | ||
/* eslint-enable vars-on-top */ | ||
if (options.format) { | ||
content += options.format(methodName, loggerName); | ||
// content += options.format(methodName, loggerName); | ||
content += options.format(level, lname, timestamp); | ||
} else { | ||
content += options.template; | ||
if (hasTimestamp) { | ||
content = content.replace(/%t/, options.timestampFormatter(new Date())); | ||
content = content.replace(/%t/, timestamp); | ||
} | ||
if (hasLevel) content = content.replace(/%l/, options.levelFormatter(methodName)); | ||
if (hasName) content = content.replace(/%n/, options.nameFormatter(loggerName)); | ||
if (hasLevel) content = content.replace(/%l/, level); | ||
if (hasName) content = content.replace(/%n/, lname); | ||
} | ||
@@ -102,4 +118,8 @@ | ||
// for remove inherited format option if own format option not preset | ||
// config = config || {}; | ||
// config.format = config.format; | ||
// for remove inherited format option if template option preset | ||
config = config || {}; | ||
config.format = config.format; | ||
if (config.template) config.format = undefined; | ||
@@ -109,2 +129,9 @@ configs[name] = merge({}, parent, config); | ||
logger.setLevel(logger.getLevel()); | ||
if (!loglevel) { | ||
logger.warn( | ||
'It is necessary to call the function reg() of loglevel-plugin-prefix before calling apply. From the next release, it will throw an error. See more: https://github.com/kutuluk/loglevel-plugin-prefix/blob/master/README.md' | ||
); | ||
} | ||
return logger; | ||
@@ -114,2 +141,3 @@ }; | ||
var api = { | ||
reg: reg, | ||
apply: apply | ||
@@ -116,0 +144,0 @@ }; |
@@ -12,5 +12,7 @@ { | ||
"browser", | ||
"node", | ||
"prefix", | ||
"colored", | ||
"loglevel", | ||
"loglevel-plugin" | ||
"plugin" | ||
], | ||
@@ -26,3 +28,3 @@ "homepage": "https://github.com/kutuluk/loglevel-plugin-prefix", | ||
"license": "MIT", | ||
"version": "0.7.2", | ||
"version": "0.8.0", | ||
"main": "./lib/loglevel-plugin-prefix", | ||
@@ -38,2 +40,3 @@ "scripts": { | ||
"chai": "^4.1.2", | ||
"chalk": "^2.3.0", | ||
"eslint": "^3.19.0", | ||
@@ -49,2 +52,2 @@ "eslint-config-airbnb-base": "^11.3.2", | ||
} | ||
} | ||
} |
108
README.md
@@ -17,5 +17,13 @@ # loglevel-plugin-prefix | ||
#### `reg(loglevel)` | ||
This method must be called before any calling the apply method. | ||
#### Parameters | ||
`loglevel` - the root logger, imported from loglevel package | ||
#### `apply(logger, options)` | ||
This method applies the plugin to the logger. | ||
This method applies the plugin to the logger. Before using this method, the `reg` method must be called, otherwise a warning will be logged. **From the next release, the call apply before reg will throw an error.** | ||
@@ -39,3 +47,4 @@ #### Parameters | ||
return name || 'root'; | ||
} | ||
}, | ||
format: undefined | ||
}; | ||
@@ -52,4 +61,5 @@ ``` | ||
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. | ||
Alternatively, you can use **format** option. This is a function that receives formatted values (level, logger, timestamp) and should returns a prefix string. If the format option is present, the template option are ignored. | ||
## Usage | ||
@@ -60,3 +70,3 @@ | ||
<script src="https://unpkg.com/loglevel/dist/loglevel.min.js"></script> | ||
<script src="https://unpkg.com/loglevel-plugin-prefix@^0.7/dist/loglevel-plugin-prefix.min.js"></script> | ||
<script src="https://unpkg.com/loglevel-plugin-prefix@^0.8/dist/loglevel-plugin-prefix.min.js"></script> | ||
@@ -66,2 +76,3 @@ <script> | ||
var prefixer = prefix.noConflict(); | ||
prefixer.reg(logger); | ||
prefixer.apply(logger); | ||
@@ -77,28 +88,44 @@ logger.warn('prefixed message'); | ||
### ES6 | ||
### Node | ||
```javascript | ||
import log from 'loglevel'; | ||
import prefix from 'loglevel-plugin-prefix'; | ||
const chalk = require('chalk'); | ||
const log = require('loglevel'); | ||
const prefix = require('loglevel-plugin-prefix'); | ||
prefix.apply(log); | ||
log.warn('prefixed message'); | ||
``` | ||
const colors = { | ||
TRACE: chalk.magenta, | ||
DEBUG: chalk.cyan, | ||
INFO: chalk.blue, | ||
WARN: chalk.yellow, | ||
ERROR: chalk.red, | ||
}; | ||
### CommonJS | ||
```javascript | ||
var log = require('loglevel'); | ||
var prefix = require('loglevel-plugin-prefix'); | ||
prefix.reg(log); | ||
log.enableAll(); | ||
prefix.apply(log); | ||
log.warn('prefixed message'); | ||
``` | ||
prefix.apply(log, { | ||
format(level, name, timestamp) { | ||
return `${chalk.gray(`[${timestamp}]`)} ${colors[level.toUpperCase()](level)} ${chalk.green(`(${name})`)}`; | ||
}, | ||
}); | ||
### AMD | ||
```javascript | ||
define(['loglevel', 'loglevel-plugin-prefix'], function(log, prefix) { | ||
prefix.apply(log); | ||
log.warn('prefixed message'); | ||
const critical = log.getLogger('critical'); | ||
prefix.apply(critical, { | ||
format(level, name, timestamp) { | ||
return chalk.red(`[${timestamp}] ${level} (${name}):`); | ||
}, | ||
}); | ||
log.trace('trace'); | ||
log.debug('debug'); | ||
critical.info('Something significant happened'); | ||
log.info('info'); | ||
log.warn('warn'); | ||
log.error('error'); | ||
``` | ||
Output | ||
![output](https://raw.githubusercontent.com/kutuluk/loglevel-plugin-prefix/master/colored.png "output") | ||
## Custom options | ||
@@ -121,6 +148,3 @@ | ||
const fn = (level, logger) => { | ||
const timestamp = new Date().toISOString(); | ||
const label = level.toUpperCase(); | ||
const name = logger || 'global'; | ||
const fn = (level, logger, timestamp) => { | ||
return `[${timestamp}] ${label} (${name}) static text:`; | ||
@@ -132,2 +156,6 @@ }; | ||
log.info('%s prefix', 'functional'); | ||
prefix.apply(log, { template: '[%t] %l (%n) static text:' }); | ||
log.info('again %s prefix', 'template'); | ||
``` | ||
@@ -139,2 +167,3 @@ | ||
[2017-05-29T12:53:46.000Z] INFO (global) static text: functional prefix | ||
[2017-05-29T12:53:46.000Z] INFO (global) static text: again template prefix | ||
``` | ||
@@ -145,5 +174,6 @@ | ||
```javascript | ||
import log from 'loglevel'; | ||
import prefix from 'loglevel-plugin-prefix'; | ||
const log = require('loglevel'); | ||
const prefix = require('../lib/loglevel-plugin-prefix'); | ||
prefix.reg(log); | ||
log.enableAll(); | ||
@@ -154,2 +184,4 @@ | ||
const chicken = log.getLogger('chicken'); | ||
chicken.info('chicken'); | ||
prefix.apply(chicken, { template: '%l (%n):' }); | ||
@@ -162,10 +194,5 @@ chicken.info('chicken'); | ||
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}):`; | ||
}; | ||
const fn = (level, logger) => `${level} (${logger}):`; | ||
@@ -176,4 +203,4 @@ prefix.apply(egg, { format: fn }); | ||
prefix.apply(egg, { | ||
timestampFormatter(date) { | ||
return date.toISOString(); | ||
levelFormatter(level) { | ||
return level.toLowerCase(); | ||
}, | ||
@@ -190,9 +217,10 @@ }); | ||
root | ||
chicken | ||
INFO (chicken): chicken | ||
[16:53:46] INFO: root | ||
[16:53:46] INFO: egg | ||
[13:20:24] INFO: root | ||
[13:20:24] INFO: egg | ||
INFO (egg): egg | ||
[2017-05-29T12:53:46.000Z] INFO: egg | ||
info (egg): egg | ||
INFO (chicken): chicken | ||
[16:53:46] INFO: root | ||
[13:20:24] INFO: root | ||
``` |
@@ -50,3 +50,3 @@ const expect = require('chai').expect; | ||
expect(spy.calledWith('info (undefined): test')).to.be.true; | ||
expect(spy.calledWith('INFO (root): test')).to.be.true; | ||
}); | ||
@@ -72,2 +72,5 @@ | ||
// for warn in apply | ||
spy.reset(); | ||
loglevel.trace(); | ||
@@ -74,0 +77,0 @@ loglevel.debug(); |
Sorry, the diff of this file is not supported yet
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
79929
13
296
213
11