loglevel-mixin
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -5,28 +5,2 @@ 'use strict'; | ||
/** | ||
* @module loglevel-mixin | ||
*/ | ||
/** | ||
* @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 | ||
*/ | ||
const defaultLogLevels = declareLevels([ | ||
@@ -42,12 +16,5 @@ 'trace', | ||
]); | ||
/** | ||
* Generate the loglevel objects out of a list of log level names. | ||
* @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. | ||
*/ | ||
function declareLevels(list) { | ||
const levels = {}; | ||
let priority = list.length; | ||
list.forEach(name => { | ||
@@ -60,22 +27,4 @@ levels[name] = { | ||
}); | ||
return levels; | ||
} | ||
/** | ||
* <!-- 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 {Logger} theFunction 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 | ||
* @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 | ||
*/ | ||
function defineLoggerMethods( | ||
@@ -87,3 +36,2 @@ object, | ||
const properties = {}; | ||
Object.keys(logLevels).forEach(level => { | ||
@@ -119,25 +67,5 @@ const myLevel = logLevels[level].priority; | ||
}); | ||
Object.defineProperties(object, properties); | ||
} | ||
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 | ||
* @param {string} defaultLogLevel the default value for the logLevel property | ||
* @return {class} newly created class ready to be further extendet/used | ||
* @example | ||
* import { LogLevelMixin } = from 'loglevel-mixin'; | ||
* class BaseClass { | ||
* log(level, message) { console.log(`${level} ${message}`); } | ||
* } | ||
* class LoggingEnabledClass extends LogLevelMixin(BaseClass) { | ||
* } | ||
* @mixin | ||
*/ | ||
function LogLevelMixin( | ||
@@ -149,5 +77,2 @@ superclass, | ||
const newClass = class extends superclass { | ||
/** | ||
* set the log level to the default | ||
*/ | ||
constructor(...args) { | ||
@@ -157,22 +82,8 @@ super(...args); | ||
} | ||
/** | ||
* @return {string} name of the current log level | ||
*/ | ||
get logLevel() { | ||
return this[LOGLEVEL].name; | ||
} | ||
/** | ||
* Set the logging level. | ||
* if an unknown logLevel is given the default logLevel will be used. | ||
* @param {string} level | ||
*/ | ||
set logLevel(level) { | ||
this[LOGLEVEL] = logLevels[level] || defaultLogLevel; | ||
} | ||
/** | ||
* @return {number} priority of the current log level | ||
*/ | ||
get logLevelPriority() { | ||
@@ -182,16 +93,5 @@ return this[LOGLEVEL].priority; | ||
}; | ||
defineLoggerMethods(newClass.prototype, logLevels); | ||
return newClass; | ||
} | ||
/** | ||
* Declares two properties: | ||
* logLevel {string} `info`,`error`,... | ||
* logLevelPriority {number} | ||
* | ||
* @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 | ||
*/ | ||
function defineLogLevelProperties( | ||
@@ -203,3 +103,2 @@ object, | ||
let logLevel = defaultLogLevel; | ||
Object.defineProperties(object, { | ||
@@ -214,3 +113,2 @@ logLevel: { | ||
}, | ||
logLevelPriority: { | ||
@@ -223,16 +121,7 @@ get() { | ||
} | ||
/** | ||
* Helper function to aggregate values into a log event | ||
* @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 | ||
*/ | ||
function makeLogEvent(level, arg, args) { | ||
const logevent = { | ||
timestamp: Date.now(), | ||
level: level | ||
level | ||
}; | ||
if (typeof arg === 'string') { | ||
@@ -245,3 +134,2 @@ logevent.message = arg; | ||
} | ||
if (arg instanceof Error) { | ||
@@ -252,7 +140,5 @@ mapError(logevent, arg); | ||
} | ||
return Object.assign(logevent, arg, args); | ||
} | ||
} | ||
function mapError(logevent, error) { | ||
@@ -259,0 +145,0 @@ if (error.stack) { |
{ | ||
"name": "loglevel-mixin", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "dist/loglevel-mixin.js", | ||
@@ -32,16 +35,22 @@ "module": "src/loglevel-mixin.js", | ||
"devDependencies": { | ||
"ava": "^0.24.0", | ||
"documentation": "^5.3.5", | ||
"ava": "^1.0.0-beta.7", | ||
"documentation": "^8.1.1", | ||
"markdown-doctest": "^0.9.1", | ||
"nyc": "^11.4.1", | ||
"rollup": "^0.54.0", | ||
"semantic-release": "^12.2.0", | ||
"travis-deploy-once": "^4.3.1" | ||
"nyc": "^13.0.1", | ||
"rollup": "^0.64.1", | ||
"rollup-plugin-cleanup": "^3.0.0", | ||
"rollup-plugin-commonjs": "^9.1.5", | ||
"rollup-plugin-executable": "^1.3.0", | ||
"rollup-plugin-istanbul": "^2.0.1", | ||
"rollup-plugin-multi-entry": "^2.0.2", | ||
"rollup-plugin-node-resolve": "^3.3.0", | ||
"semantic-release": "^15.9.8", | ||
"travis-deploy-once": "^5.0.2" | ||
}, | ||
"engines": { | ||
"node": ">=8.9.4" | ||
"node": ">=8.11" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/arlac77/loglevel-mixin.git" | ||
"url": "https://github.com/arlac77/loglevel-mixin.git" | ||
}, | ||
@@ -56,5 +65,2 @@ "bugs": { | ||
], | ||
"presets": [ | ||
"stage-3" | ||
], | ||
"require": [ | ||
@@ -61,0 +67,0 @@ "babel-register" |
@@ -6,3 +6,2 @@ [![npm](https://img.shields.io/npm/v/loglevel-mixin.svg)](https://www.npmjs.com/package/loglevel-mixin) | ||
[![Build Status](https://secure.travis-ci.org/arlac77/loglevel-mixin.png)](http://travis-ci.org/arlac77/loglevel-mixin) | ||
[![bithound](https://www.bithound.io/github/arlac77/loglevel-mixin/badges/score.svg)](https://www.bithound.io/github/arlac77/loglevel-mixin) | ||
[![codecov.io](http://codecov.io/github/arlac77/loglevel-mixin/coverage.svg?branch=master)](http://codecov.io/github/arlac77/loglevel-mixin?branch=master) | ||
@@ -87,23 +86,29 @@ [![Coverage Status](https://coveralls.io/repos/arlac77/loglevel-mixin/badge.svg)](https://coveralls.io/r/arlac77/loglevel-mixin) | ||
- [loglevel-mixin](#loglevel-mixin) | ||
- [Logger](#logger) | ||
- [Properties](#properties) | ||
- [defaultLogLevels](#defaultloglevels) | ||
- [Loglevel](#loglevel) | ||
- [defaultLogLevels](#defaultloglevels) | ||
- [Logger](#logger) | ||
- [Properties](#properties-1) | ||
- [declareLevels](#declarelevels) | ||
- [Parameters](#parameters) | ||
- [defineLoggerMethods](#defineloggermethods) | ||
- [Parameters](#parameters-1) | ||
- [Examples](#examples) | ||
- [LogLevelMixin](#loglevelmixin) | ||
- [Parameters](#parameters-2) | ||
- [Examples](#examples-1) | ||
- [defineLogLevelProperties](#defineloglevelproperties) | ||
- [Parameters](#parameters-3) | ||
- [Properties](#properties-2) | ||
- [makeLogEvent](#makelogevent) | ||
- [Parameters](#parameters-4) | ||
## loglevel-mixin | ||
## Logger | ||
## Loglevel | ||
Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function) | ||
Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | ||
### Properties | ||
**Properties** | ||
- `entry` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** | ||
- `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 | ||
@@ -122,9 +127,10 @@ | ||
## Logger | ||
## Loglevel | ||
Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function) | ||
Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | ||
**Properties** | ||
### Properties | ||
- `entry` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** | ||
- `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)** | ||
@@ -135,7 +141,7 @@ ## declareLevels | ||
**Parameters** | ||
### Parameters | ||
- `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. | ||
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. | ||
@@ -149,3 +155,3 @@ ## defineLoggerMethods | ||
**Parameters** | ||
### Parameters | ||
@@ -159,3 +165,3 @@ - `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** target where to assign properties to | ||
**Examples** | ||
### Examples | ||
@@ -174,3 +180,3 @@ ```javascript | ||
**Parameters** | ||
### Parameters | ||
@@ -181,3 +187,3 @@ - `superclass` **class** class to be extendet | ||
**Examples** | ||
### Examples | ||
@@ -198,6 +204,4 @@ ```javascript | ||
Declares two properties: | ||
logLevel {string} `info`,`error`,... | ||
logLevelPriority {number} | ||
**Parameters** | ||
### Parameters | ||
@@ -208,2 +212,7 @@ - `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** target where the properties will be written into | ||
### Properties | ||
- `logLevel` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** `info`,`error`,... | ||
- `logLevelPriority` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** | ||
## makeLogEvent | ||
@@ -213,3 +222,3 @@ | ||
**Parameters** | ||
### Parameters | ||
@@ -216,0 +225,0 @@ - `level` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** log level |
/** | ||
* @module loglevel-mixin | ||
*/ | ||
/** | ||
* @callback Logger | ||
@@ -41,3 +37,3 @@ * @property {Object} entry | ||
* @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. | ||
*/ | ||
@@ -179,4 +175,4 @@ export function declareLevels(list) { | ||
* Declares two properties: | ||
* logLevel {string} `info`,`error`,... | ||
* logLevelPriority {number} | ||
* @property {string} logLevel `info`,`error`,... | ||
* @property {number} logLevelPriority | ||
* | ||
@@ -222,3 +218,3 @@ * @param {Object} object target where the properties will be written into | ||
timestamp: Date.now(), | ||
level: level | ||
level | ||
}; | ||
@@ -225,0 +221,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
223
22538
13
360