loglevel-mixin
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -10,3 +10,22 @@ 'use strict'; | ||
/** | ||
* @callback Logger | ||
* @property {Object} entry | ||
*/ | ||
/** | ||
* @typedef {Object} Loglevel | ||
* @property {string} name | ||
* @property {number} priority | ||
*/ | ||
/** | ||
* default log levels | ||
* - trace | ||
* - debug | ||
* - info | ||
* - notice | ||
* - warn | ||
* - error | ||
* - crit | ||
* - alert | ||
*/ | ||
@@ -27,3 +46,3 @@ const defaultLogLevels = declareLevels([ | ||
* @param {string[]} list A list of log level names. The last name in the list will become the one with the highest priority. | ||
* @return {object} levels object A hash with all the loglevels. Stored by there name. | ||
* @return {Object} levels object A hash with all the loglevels. Stored by there name. | ||
*/ | ||
@@ -36,4 +55,4 @@ function declareLevels(list) { | ||
levels[name] = { | ||
name: name, | ||
priority: priority | ||
name, | ||
priority | ||
}; | ||
@@ -47,7 +66,8 @@ priority -= 1; | ||
/** | ||
* <!-- skip-example --> | ||
* Adds logging methods to an existing object. | ||
* For each loglevel a method with the name of the log level will be created. | ||
* @param {object} object target where to assign properties to | ||
* @param {object} logLevels Hash with all the available loglevels. Stored by there name | ||
* @param {function} [theFunction] The function to be added under the loglevel name. | ||
* @param {Object} object target where to assign properties to | ||
* @param {Object} logLevels Hash with all the available loglevels. Stored by there name | ||
* @param {Logger} theFunction to be added under the loglevel name. | ||
* This function will only be called if the current loglevel is greater equal | ||
@@ -57,2 +77,6 @@ * the log level of the called logging function. | ||
* @return {undefined} | ||
* @example | ||
* defineLoggerMethods( obj) | ||
* obj.info('info entry'); // will redirect to theFunction if obj.loglevel is at least info | ||
* obj.error('error entry'); // will redirect to theFunction if obj.loglevel is at least error | ||
*/ | ||
@@ -71,22 +95,23 @@ function defineLoggerMethods( | ||
value: | ||
theFunction !== undefined | ||
theFunction === undefined | ||
? function(providerFunction) { | ||
if (this.logLevelPriority >= myLevel) | ||
if (typeof providerFunction === 'function') { | ||
theFunction.call( | ||
this, | ||
levelName, | ||
providerFunction(levelName) | ||
); | ||
} else { | ||
theFunction.call(this, levelName, providerFunction); | ||
} | ||
if (this.logLevelPriority >= myLevel) { | ||
this.log( | ||
levelName, | ||
typeof providerFunction === 'function' | ||
? providerFunction(levelName) | ||
: providerFunction | ||
); | ||
} | ||
} | ||
: function(providerFunction) { | ||
if (this.logLevelPriority >= myLevel) | ||
if (typeof providerFunction === 'function') { | ||
this.log(levelName, providerFunction(levelName)); | ||
} else { | ||
this.log(levelName, providerFunction); | ||
} | ||
if (this.logLevelPriority >= myLevel) { | ||
theFunction.call( | ||
this, | ||
levelName, | ||
typeof providerFunction === 'function' | ||
? providerFunction(levelName) | ||
: providerFunction | ||
); | ||
} | ||
}, | ||
@@ -100,11 +125,13 @@ enumerable: true | ||
const LOGLEVEL = Symbol('loglevel'); | ||
/** | ||
* <!-- skip-example --> | ||
* @class | ||
* @classdesc This function is a mixin for ES2015 classes. | ||
* @param {class} superclass class to be extendet | ||
* @param {object} [logLevels] Object with all the available loglevels. Stored by their name; defaults to defaultLogLevels | ||
* @param {string} [defaultLogLevel] the default value for the logLevel property; defaults to `info` | ||
* @param {Object} logLevels Object with all the available loglevels. Stored by their name | ||
* @param {string} defaultLogLevel the default value for the logLevel property | ||
* @return {class} newly created class ready to be further extendet/used | ||
* @example | ||
* ```js | ||
* import { LogLevelMixin } = from 'loglevel-mixin'; | ||
@@ -116,3 +143,2 @@ * class BaseClass { | ||
* } | ||
* ``` | ||
* @mixin | ||
@@ -131,3 +157,3 @@ */ | ||
super(...args); | ||
this._logLevel = defaultLogLevel; | ||
this[LOGLEVEL] = defaultLogLevel; | ||
} | ||
@@ -139,3 +165,3 @@ | ||
get logLevel() { | ||
return this._logLevel.name; | ||
return this[LOGLEVEL].name; | ||
} | ||
@@ -145,7 +171,7 @@ | ||
* Set the logging level. | ||
* if an unknown logLEvel is given the default logLevel will be used. | ||
* if an unknown logLevel is given the default logLevel will be used. | ||
* @param {string} level | ||
*/ | ||
set logLevel(level) { | ||
this._logLevel = logLevels[level] || defaultLogLevel; | ||
this[LOGLEVEL] = logLevels[level] || defaultLogLevel; | ||
} | ||
@@ -157,3 +183,3 @@ | ||
get logLevelPriority() { | ||
return this._logLevel.priority; | ||
return this[LOGLEVEL].priority; | ||
} | ||
@@ -171,5 +197,5 @@ }; | ||
* | ||
* @param {object} object target where the properties will be written into | ||
* @param {object} [logLevels=defaultLogLevels] Hash with all the available loglevels. Stored by there name; defaults to defaultLogLevels | ||
* @param {string} [defaultLogLevel=info] the default value for the properties; defaults to `info` | ||
* @param {Object} object target where the properties will be written into | ||
* @param {Object} logLevels Hash with all the available loglevels. Stored by there name | ||
* @param {string} defaultLogLevel the default value for the properties | ||
*/ | ||
@@ -181,22 +207,20 @@ function defineLogLevelProperties( | ||
) { | ||
const properties = {}; | ||
let logLevel = defaultLogLevel; | ||
properties.logLevel = { | ||
get() { | ||
return logLevel.name; | ||
Object.defineProperties(object, { | ||
logLevel: { | ||
get() { | ||
return logLevel.name; | ||
}, | ||
set(level) { | ||
logLevel = logLevels[level] || defaultLogLevel; | ||
} | ||
}, | ||
set(level) { | ||
logLevel = logLevels[level] || defaultLogLevel; | ||
} | ||
}; | ||
properties.logLevelPriority = { | ||
get() { | ||
return logLevel.priority; | ||
logLevelPriority: { | ||
get() { | ||
return logLevel.priority; | ||
} | ||
} | ||
}; | ||
Object.defineProperties(object, properties); | ||
}); | ||
} | ||
@@ -207,5 +231,5 @@ | ||
* @param {string} level log level | ||
* @param {string|object} arg original log message - level and timestamp may be overwritten | ||
* @param {object} [args] additional values to be merged into the final log event - values have precedence | ||
* @return {object} suitable for log event processing | ||
* @param {string|Object} arg original log message - level and timestamp may be overwritten | ||
* @param {Object} [args] additional values to be merged into the final log event - values have precedence | ||
* @return {Object} suitable for log event processing | ||
*/ | ||
@@ -212,0 +236,0 @@ function makeLogEvent(level, arg, args) { |
{ | ||
"name": "loglevel-mixin", | ||
"version": "2.0.1", | ||
"description": "mixin to declare logging methods named after a set of log levels", | ||
"version": "2.0.2", | ||
"main": "dist/loglevel-mixin.js", | ||
"module": "src/loglevel-mixin.js", | ||
"description": "mixin to declare logging methods named after a set of log levels", | ||
"keywords": [ | ||
"loglevel", | ||
"logging" | ||
], | ||
"contributors": [ | ||
{ | ||
"name": "Markus Felten", | ||
"email": "markus.felten@gmx.de" | ||
} | ||
], | ||
"license": "BSD-2-Clause", | ||
"scripts": { | ||
"cover": "nyc npm test", | ||
"docs": "jsdoc2md -l off -t doc/README.hbs -f src/*.js >README.md", | ||
"test": "mocha tests/*-test.js", | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post", | ||
"pretest": "rollup -c", | ||
"posttest": "markdown-doctest", | ||
"precover": "rollup -c", | ||
"prepare": "rollup -c" | ||
"cover": "nyc --temp-directory build/nyc ava", | ||
"docs": "documentation readme src/loglevel-mixin.js --section=API", | ||
"lint": "documentation lint src/loglevel-mixin.js", | ||
"test": "ava", | ||
"semantic-release": "semantic-release", | ||
"pretest": "rollup -c tests/rollup.config.js", | ||
"posttest": "npm run prepare && markdown-doctest", | ||
"precover": "rollup -c tests/rollup.config.js", | ||
"prepare": "rollup -c", | ||
"travis-deploy-once": "travis-deploy-once" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"ava": "^0.24.0", | ||
"documentation": "^5.3.5", | ||
"markdown-doctest": "^0.9.1", | ||
"nyc": "^11.4.1", | ||
"rollup": "^0.54.0", | ||
"semantic-release": "^12.2.0", | ||
"travis-deploy-once": "^4.3.1" | ||
}, | ||
"engines": { | ||
"node": ">=8.9.4" | ||
}, | ||
"repository": { | ||
@@ -21,23 +47,2 @@ "type": "git", | ||
}, | ||
"engines": { | ||
"node": ">=8.6.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"cracks": "3.1.2", | ||
"jsdoc-to-markdown": "^3.0.0", | ||
"markdown-doctest": "^0.9.1", | ||
"mocha": "^4.0.0", | ||
"nyc": "^11.2.1", | ||
"rollup": "^0.50.0", | ||
"semantic-release": "^8.0.4" | ||
}, | ||
"release": { | ||
"verifyRelease": "cracks" | ||
}, | ||
"keywords": [ | ||
"loglevel", | ||
"logging" | ||
], | ||
"license": "BSD-2-Clause", | ||
"bugs": { | ||
@@ -47,12 +52,16 @@ "url": "https://github.com/arlac77/loglevel-mixin/issues" | ||
"homepage": "https://github.com/arlac77/loglevel-mixin#readme", | ||
"dependencies": {}, | ||
"contributors": [ | ||
{ | ||
"name": "Markus Felten", | ||
"email": "markus.felten@gmx.de" | ||
} | ||
], | ||
"ava": { | ||
"files": [ | ||
"build/*-test.js" | ||
], | ||
"presets": [ | ||
"stage-3" | ||
], | ||
"require": [ | ||
"babel-register" | ||
] | ||
}, | ||
"nyc": { | ||
"include": [ | ||
"dist/**/*.js" | ||
"build/*-test.js" | ||
], | ||
@@ -62,12 +71,15 @@ "reporter": [ | ||
], | ||
"report-dir": "./coverage" | ||
"report-dir": "./build/coverage" | ||
}, | ||
"xo": { | ||
"space": true | ||
}, | ||
"template": { | ||
"repository": { | ||
"url": "https://github.com/Kronos-Tools/npm-package-template.git" | ||
"url": "https://github.com/arlac77/npm-package-template.git" | ||
} | ||
}, | ||
"xo": { | ||
"space": true | ||
"_release": { | ||
"verifyRelease": "cracks" | ||
} | ||
} |
196
README.md
[![npm](https://img.shields.io/npm/v/loglevel-mixin.svg)](https://www.npmjs.com/package/loglevel-mixin) | ||
[![Greenkeeper](https://badges.greenkeeper.io/arlac77/loglevel-mixin.svg)](https://greenkeeper.io/) | ||
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/arlac77/loglevel-mixin) | ||
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier) | ||
[![Build Status](https://secure.travis-ci.org/arlac77/loglevel-mixin.png)](http://travis-ci.org/arlac77/loglevel-mixin) | ||
@@ -8,3 +9,2 @@ [![bithound](https://www.bithound.io/github/arlac77/loglevel-mixin/badges/score.svg)](https://www.bithound.io/github/arlac77/loglevel-mixin) | ||
[![Coverage Status](https://coveralls.io/repos/arlac77/loglevel-mixin/badge.svg)](https://coveralls.io/r/arlac77/loglevel-mixin) | ||
[![Code Climate](https://codeclimate.com/github/arlac77/loglevel-mixin/badges/gpa.svg)](https://codeclimate.com/github/arlac77/loglevel-mixin) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/arlac77/loglevel-mixin/badge.svg)](https://snyk.io/test/github/arlac77/loglevel-mixin) | ||
@@ -16,19 +16,20 @@ [![GitHub Issues](https://img.shields.io/github/issues/arlac77/loglevel-mixin.svg?style=flat-square)](https://github.com/arlac77/loglevel-mixin/issues) | ||
[![docs](http://inch-ci.org/github/arlac77/loglevel-mixin.svg?branch=master)](http://inch-ci.org/github/arlac77/loglevel-mixin) | ||
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) | ||
[![downloads](http://img.shields.io/npm/dm/loglevel-mixin.svg?style=flat-square)](https://npmjs.org/package/loglevel-mixin) | ||
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) | ||
loglevel-mixin | ||
============== | ||
# loglevel-mixin | ||
Injects methods named after a set of logLevels which are only forwarding messages. | ||
If the current logLevel is higher or equal to the logLevel the name of the called method reflects. | ||
Injects methods named after a set of logLevels which are only forwarding messages. If the current logLevel is higher or equal to the logLevel the name of the called method reflects. | ||
# usage | ||
usage | ||
===== | ||
```javascript | ||
const llm = require('loglevel-mixin'); | ||
let someObject = { log(level,message) { console.log(`${level} ${message}`); } }; | ||
let someObject = { | ||
log(level, message) { | ||
console.log(`${level} ${message}`); | ||
} | ||
}; | ||
@@ -39,9 +40,12 @@ llm.defineLoggerMethods(someObject); | ||
someObject.logLevel = 'error'; | ||
someObject.info( level => 'my info message (not reported since logLevel is error)') | ||
someObject.info( | ||
level => 'my info message (not reported since logLevel is error)' | ||
); | ||
someObject.logLevel = 'info'; | ||
someObject.info( level => 'my info message (reported since logLevel is now info)') | ||
someObject.info( | ||
level => 'my info message (reported since logLevel is now info)' | ||
); | ||
``` | ||
works for ES2015 classes to | ||
--------------------------- | ||
## works for ES2015 classes to | ||
@@ -51,6 +55,9 @@ ```javascript | ||
const LoggingEnabledClass = llm.LogLevelMixin(class BaseClass | ||
{ | ||
log(level, message) { console.log(`${level} ${message}`); } | ||
}); | ||
const LoggingEnabledClass = llm.LogLevelMixin( | ||
class BaseClass { | ||
log(level, message) { | ||
console.log(`${level} ${message}`); | ||
} | ||
} | ||
); | ||
@@ -60,9 +67,12 @@ const someObject = new LoggingEnabledClass(); | ||
someObject.logLevel = 'error'; | ||
someObject.info( level => 'my info message (not reported since logLevel is error)') | ||
someObject.info( | ||
level => 'my info message (not reported since logLevel is error)' | ||
); | ||
someObject.logLevel = 'info'; | ||
someObject.info( level => 'my info message (reported since logLevel is now info)') | ||
someObject.info( | ||
level => 'my info message (reported since logLevel is now info)' | ||
); | ||
``` | ||
install | ||
======= | ||
# install | ||
@@ -75,36 +85,111 @@ With [npm](http://npmjs.org) do: | ||
# API Reference | ||
- loglevel-mixin | ||
# API | ||
* <a name="module_loglevel-mixin.declareLevels"></a> | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
## loglevel-mixin.declareLevels(list) ⇒ <code>object</code> | ||
### Table of Contents | ||
- [loglevel-mixin](#loglevel-mixin) | ||
- [Loglevel](#loglevel) | ||
- [defaultLogLevels](#defaultloglevels) | ||
- [Logger](#logger) | ||
- [declareLevels](#declarelevels) | ||
- [defineLoggerMethods](#defineloggermethods) | ||
- [LogLevelMixin](#loglevelmixin) | ||
- [defineLogLevelProperties](#defineloglevelproperties) | ||
- [makeLogEvent](#makelogevent) | ||
## loglevel-mixin | ||
## Loglevel | ||
Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | ||
**Properties** | ||
- `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** | ||
- `priority` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** | ||
## defaultLogLevels | ||
default log levels | ||
- trace | ||
- debug | ||
- info | ||
- notice | ||
- warn | ||
- error | ||
- crit | ||
- alert | ||
## Logger | ||
Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function) | ||
**Properties** | ||
- `entry` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** | ||
## declareLevels | ||
Generate the loglevel objects out of a list of log level names. | ||
**Kind**: static method of [<code>loglevel-mixin</code>](#module_loglevel-mixin) | ||
**Returns**: <code>object</code> - levels object A hash with all the loglevels. Stored by there name. | ||
**Parameters** | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| list | <code>Array.<string></code> | A list of log level names. The last name in the list will become the one with the highest priority. | | ||
- `list` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** A list of log level names. The last name in the list will become the one with the highest priority. | ||
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** levels object A hash with all the loglevels. Stored by there name. | ||
* <a name="module_loglevel-mixin.defineLoggerMethods"></a> | ||
## defineLoggerMethods | ||
## loglevel-mixin.defineLoggerMethods(object, logLevels, [theFunction]) ⇒ <code>undefined</code> | ||
<!-- skip-example --> | ||
Adds logging methods to an existing object. | ||
For each loglevel a method with the name of the log level will be created. | ||
**Kind**: static method of [<code>loglevel-mixin</code>](#module_loglevel-mixin) | ||
**Parameters** | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| object | <code>object</code> | target where to assign properties to | | ||
| logLevels | <code>object</code> | Hash with all the available loglevels. Stored by there name | | ||
| [theFunction] | <code>function</code> | The function to be added under the loglevel name. This function will only be called if the current loglevel is greater equal the log level of the called logging function. By default a method log(level,message) will be used | | ||
- `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** target where to assign properties to | ||
- `logLevels` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Hash with all the available loglevels. Stored by there name (optional, default `defaultLogLevels`) | ||
- `theFunction` **[Logger](#logger)** to be added under the loglevel name. | ||
This function will only be called if the current loglevel is greater equal | ||
the log level of the called logging function. | ||
By default a method log(level,message) will be used (optional, default `undefined`) | ||
**Examples** | ||
* <a name="module_loglevel-mixin.defineLogLevelProperties"></a> | ||
```javascript | ||
defineLoggerMethods( obj) | ||
obj.info('info entry'); // will redirect to theFunction if obj.loglevel is at least info | ||
obj.error('error entry'); // will redirect to theFunction if obj.loglevel is at least error | ||
``` | ||
## loglevel-mixin.defineLogLevelProperties(object, [logLevels], [defaultLogLevel]) | ||
Returns **[undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined)** | ||
## LogLevelMixin | ||
<!-- skip-example --> | ||
**Parameters** | ||
- `superclass` **class** class to be extendet | ||
- `logLevels` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Object with all the available loglevels. Stored by their name (optional, default `defaultLogLevels`) | ||
- `defaultLogLevel` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the default value for the logLevel property (optional, default `defaultLogLevels.info`) | ||
**Examples** | ||
```javascript | ||
import { LogLevelMixin } = from 'loglevel-mixin'; | ||
class BaseClass { | ||
log(level, message) { console.log(`${level} ${message}`); } | ||
} | ||
class LoggingEnabledClass extends LogLevelMixin(BaseClass) { | ||
} | ||
``` | ||
Returns **class** newly created class ready to be further extendet/used | ||
## defineLogLevelProperties | ||
Declares two properties: | ||
@@ -114,31 +199,22 @@ logLevel {string} `info`,`error`,... | ||
**Kind**: static method of [<code>loglevel-mixin</code>](#module_loglevel-mixin) | ||
**Parameters** | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| object | <code>object</code> | | target where the properties will be written into | | ||
| [logLevels] | <code>object</code> | <code>defaultLogLevels</code> | Hash with all the available loglevels. Stored by there name; defaults to defaultLogLevels | | ||
| [defaultLogLevel] | <code>string</code> | <code>"info"</code> | the default value for the properties; defaults to `info` | | ||
- `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** target where the properties will be written into | ||
- `logLevels` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Hash with all the available loglevels. Stored by there name (optional, default `defaultLogLevels`) | ||
- `defaultLogLevel` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the default value for the properties (optional, default `defaultLogLevels.info`) | ||
## makeLogEvent | ||
* <a name="module_loglevel-mixin.makeLogEvent"></a> | ||
## loglevel-mixin.makeLogEvent(level, arg, [args]) ⇒ <code>object</code> | ||
Helper function to aggregate values into a log event | ||
**Kind**: static method of [<code>loglevel-mixin</code>](#module_loglevel-mixin) | ||
**Returns**: <code>object</code> - suitable for log event processing | ||
**Parameters** | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| level | <code>string</code> | log level | | ||
| arg | <code>string</code> \| <code>object</code> | original log message - level and timestamp may be overwritten | | ||
| [args] | <code>object</code> | additional values to be merged into the final log event - values have precedence | | ||
- `level` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** log level | ||
- `arg` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))** original log message - level and timestamp may be overwritten | ||
- `args` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** additional values to be merged into the final log event - values have precedence | ||
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** suitable for log event processing | ||
* * * | ||
# license | ||
license | ||
======= | ||
BSD-2-Clause |
@@ -6,3 +6,22 @@ /** | ||
/** | ||
* @callback Logger | ||
* @property {Object} entry | ||
*/ | ||
/** | ||
* @typedef {Object} Loglevel | ||
* @property {string} name | ||
* @property {number} priority | ||
*/ | ||
/** | ||
* default log levels | ||
* - trace | ||
* - debug | ||
* - info | ||
* - notice | ||
* - warn | ||
* - error | ||
* - crit | ||
* - alert | ||
*/ | ||
@@ -23,3 +42,3 @@ export const defaultLogLevels = declareLevels([ | ||
* @param {string[]} list A list of log level names. The last name in the list will become the one with the highest priority. | ||
* @return {object} levels object A hash with all the loglevels. Stored by there name. | ||
* @return {Object} levels object A hash with all the loglevels. Stored by there name. | ||
*/ | ||
@@ -32,4 +51,4 @@ export function declareLevels(list) { | ||
levels[name] = { | ||
name: name, | ||
priority: priority | ||
name, | ||
priority | ||
}; | ||
@@ -43,7 +62,8 @@ priority -= 1; | ||
/** | ||
* <!-- skip-example --> | ||
* Adds logging methods to an existing object. | ||
* For each loglevel a method with the name of the log level will be created. | ||
* @param {object} object target where to assign properties to | ||
* @param {object} logLevels Hash with all the available loglevels. Stored by there name | ||
* @param {function} [theFunction] The function to be added under the loglevel name. | ||
* @param {Object} object target where to assign properties to | ||
* @param {Object} logLevels Hash with all the available loglevels. Stored by there name | ||
* @param {Logger} theFunction to be added under the loglevel name. | ||
* This function will only be called if the current loglevel is greater equal | ||
@@ -53,2 +73,6 @@ * the log level of the called logging function. | ||
* @return {undefined} | ||
* @example | ||
* defineLoggerMethods( obj) | ||
* obj.info('info entry'); // will redirect to theFunction if obj.loglevel is at least info | ||
* obj.error('error entry'); // will redirect to theFunction if obj.loglevel is at least error | ||
*/ | ||
@@ -67,22 +91,23 @@ export function defineLoggerMethods( | ||
value: | ||
theFunction !== undefined | ||
theFunction === undefined | ||
? function(providerFunction) { | ||
if (this.logLevelPriority >= myLevel) | ||
if (typeof providerFunction === 'function') { | ||
theFunction.call( | ||
this, | ||
levelName, | ||
providerFunction(levelName) | ||
); | ||
} else { | ||
theFunction.call(this, levelName, providerFunction); | ||
} | ||
if (this.logLevelPriority >= myLevel) { | ||
this.log( | ||
levelName, | ||
typeof providerFunction === 'function' | ||
? providerFunction(levelName) | ||
: providerFunction | ||
); | ||
} | ||
} | ||
: function(providerFunction) { | ||
if (this.logLevelPriority >= myLevel) | ||
if (typeof providerFunction === 'function') { | ||
this.log(levelName, providerFunction(levelName)); | ||
} else { | ||
this.log(levelName, providerFunction); | ||
} | ||
if (this.logLevelPriority >= myLevel) { | ||
theFunction.call( | ||
this, | ||
levelName, | ||
typeof providerFunction === 'function' | ||
? providerFunction(levelName) | ||
: providerFunction | ||
); | ||
} | ||
}, | ||
@@ -96,11 +121,13 @@ enumerable: true | ||
const LOGLEVEL = Symbol('loglevel'); | ||
/** | ||
* <!-- skip-example --> | ||
* @class | ||
* @classdesc This function is a mixin for ES2015 classes. | ||
* @param {class} superclass class to be extendet | ||
* @param {object} [logLevels] Object with all the available loglevels. Stored by their name; defaults to defaultLogLevels | ||
* @param {string} [defaultLogLevel] the default value for the logLevel property; defaults to `info` | ||
* @param {Object} logLevels Object with all the available loglevels. Stored by their name | ||
* @param {string} defaultLogLevel the default value for the logLevel property | ||
* @return {class} newly created class ready to be further extendet/used | ||
* @example | ||
* ```js | ||
* import { LogLevelMixin } = from 'loglevel-mixin'; | ||
@@ -112,3 +139,2 @@ * class BaseClass { | ||
* } | ||
* ``` | ||
* @mixin | ||
@@ -127,3 +153,3 @@ */ | ||
super(...args); | ||
this._logLevel = defaultLogLevel; | ||
this[LOGLEVEL] = defaultLogLevel; | ||
} | ||
@@ -135,3 +161,3 @@ | ||
get logLevel() { | ||
return this._logLevel.name; | ||
return this[LOGLEVEL].name; | ||
} | ||
@@ -141,7 +167,7 @@ | ||
* Set the logging level. | ||
* if an unknown logLEvel is given the default logLevel will be used. | ||
* if an unknown logLevel is given the default logLevel will be used. | ||
* @param {string} level | ||
*/ | ||
set logLevel(level) { | ||
this._logLevel = logLevels[level] || defaultLogLevel; | ||
this[LOGLEVEL] = logLevels[level] || defaultLogLevel; | ||
} | ||
@@ -153,3 +179,3 @@ | ||
get logLevelPriority() { | ||
return this._logLevel.priority; | ||
return this[LOGLEVEL].priority; | ||
} | ||
@@ -167,5 +193,5 @@ }; | ||
* | ||
* @param {object} object target where the properties will be written into | ||
* @param {object} [logLevels=defaultLogLevels] Hash with all the available loglevels. Stored by there name; defaults to defaultLogLevels | ||
* @param {string} [defaultLogLevel=info] the default value for the properties; defaults to `info` | ||
* @param {Object} object target where the properties will be written into | ||
* @param {Object} logLevels Hash with all the available loglevels. Stored by there name | ||
* @param {string} defaultLogLevel the default value for the properties | ||
*/ | ||
@@ -177,22 +203,20 @@ export function defineLogLevelProperties( | ||
) { | ||
const properties = {}; | ||
let logLevel = defaultLogLevel; | ||
properties.logLevel = { | ||
get() { | ||
return logLevel.name; | ||
Object.defineProperties(object, { | ||
logLevel: { | ||
get() { | ||
return logLevel.name; | ||
}, | ||
set(level) { | ||
logLevel = logLevels[level] || defaultLogLevel; | ||
} | ||
}, | ||
set(level) { | ||
logLevel = logLevels[level] || defaultLogLevel; | ||
} | ||
}; | ||
properties.logLevelPriority = { | ||
get() { | ||
return logLevel.priority; | ||
logLevelPriority: { | ||
get() { | ||
return logLevel.priority; | ||
} | ||
} | ||
}; | ||
Object.defineProperties(object, properties); | ||
}); | ||
} | ||
@@ -203,5 +227,5 @@ | ||
* @param {string} level log level | ||
* @param {string|object} arg original log message - level and timestamp may be overwritten | ||
* @param {object} [args] additional values to be merged into the final log event - values have precedence | ||
* @return {object} suitable for log event processing | ||
* @param {string|Object} arg original log message - level and timestamp may be overwritten | ||
* @param {Object} [args] additional values to be merged into the final log event - values have precedence | ||
* @return {Object} suitable for log event processing | ||
*/ | ||
@@ -208,0 +232,0 @@ export function makeLogEvent(level, arg, args) { |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7
214
0
25083
5
454