-
Notifications
You must be signed in to change notification settings - Fork 938
Add ability to override custom levels comparison #1883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
3bb58e5
bd953e1
8b74f04
fac044e
00c9828
476b1f4
66a83f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * @enum {number} | ||
| */ | ||
| const DEFAULT_LEVELS = { | ||
| trace: 10, | ||
| debug: 20, | ||
| info: 30, | ||
| warn: 40, | ||
| error: 50, | ||
| fatal: 60 | ||
| } | ||
|
|
||
| /** | ||
| * @enum {"ASC" | "DESC"} | ||
| */ | ||
| const SORTING_ORDER = { | ||
obrus-corcentric marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ASC: 'ASC', | ||
| DESC: 'DESC' | ||
| } | ||
|
|
||
| module.exports = { | ||
| DEFAULT_LEVELS, | ||
| SORTING_ORDER | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,21 +6,15 @@ const { | |||||
| useOnlyCustomLevelsSym, | ||||||
| streamSym, | ||||||
| formattersSym, | ||||||
| hooksSym | ||||||
| hooksSym, | ||||||
| levelCompSym | ||||||
| } = require('./symbols') | ||||||
| const { noop, genLog } = require('./tools') | ||||||
| const { DEFAULT_LEVELS, SORTING_ORDER } = require('./constants') | ||||||
|
|
||||||
| const levels = { | ||||||
| trace: 10, | ||||||
| debug: 20, | ||||||
| info: 30, | ||||||
| warn: 40, | ||||||
| error: 50, | ||||||
| fatal: 60 | ||||||
| } | ||||||
| const levelMethods = { | ||||||
| fatal: (hook) => { | ||||||
| const logFatal = genLog(levels.fatal, hook) | ||||||
| const logFatal = genLog(DEFAULT_LEVELS.fatal, hook) | ||||||
| return function (...args) { | ||||||
| const stream = this[streamSym] | ||||||
| logFatal.call(this, ...args) | ||||||
|
|
@@ -33,15 +27,15 @@ const levelMethods = { | |||||
| } | ||||||
| } | ||||||
| }, | ||||||
| error: (hook) => genLog(levels.error, hook), | ||||||
| warn: (hook) => genLog(levels.warn, hook), | ||||||
| info: (hook) => genLog(levels.info, hook), | ||||||
| debug: (hook) => genLog(levels.debug, hook), | ||||||
| trace: (hook) => genLog(levels.trace, hook) | ||||||
| error: (hook) => genLog(DEFAULT_LEVELS.error, hook), | ||||||
| warn: (hook) => genLog(DEFAULT_LEVELS.warn, hook), | ||||||
| info: (hook) => genLog(DEFAULT_LEVELS.info, hook), | ||||||
| debug: (hook) => genLog(DEFAULT_LEVELS.debug, hook), | ||||||
| trace: (hook) => genLog(DEFAULT_LEVELS.trace, hook) | ||||||
| } | ||||||
|
|
||||||
| const nums = Object.keys(levels).reduce((o, k) => { | ||||||
| o[levels[k]] = k | ||||||
| const nums = Object.keys(DEFAULT_LEVELS).reduce((o, k) => { | ||||||
| o[DEFAULT_LEVELS[k]] = k | ||||||
| return o | ||||||
| }, {}) | ||||||
|
|
||||||
|
|
@@ -119,7 +113,36 @@ function getLevel (level) { | |||||
| function isLevelEnabled (logLevel) { | ||||||
| const { values } = this.levels | ||||||
| const logLevelVal = values[logLevel] | ||||||
| return logLevelVal !== undefined && (logLevelVal >= this[levelValSym]) | ||||||
| return logLevelVal !== undefined && this[levelCompSym](logLevelVal, this[levelValSym]) | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * | ||||||
| * @description compare `current` level with `expected` to make sure `current` level is enabled or not | ||||||
| * @param {SORTING_ORDER} direction | ||||||
| * @param {number} current | ||||||
| * @param {number} expected | ||||||
|
||||||
| * @returns {boolean} | ||||||
| */ | ||||||
| function compareLevel (direction, current, expected) { | ||||||
obrus-corcentric marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this be a clearer signature?
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The signature of this function starts from order to be able to create a single sorting function using |
||||||
| if (direction === SORTING_ORDER.DESC) { | ||||||
| return current <= expected | ||||||
| } | ||||||
|
|
||||||
| return current >= expected | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @description create a level comparison function based on `levelComparison` | ||||||
obrus-corcentric marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| * @param {SORTING_ORDER | Function} levelComparison | ||||||
| * @returns Function | ||||||
| */ | ||||||
| function genLevelComparison (levelComparison) { | ||||||
| if (typeof levelComparison === 'string') { | ||||||
| return compareLevel.bind(null, levelComparison) | ||||||
| } | ||||||
obrus-corcentric marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| return levelComparison | ||||||
| } | ||||||
|
|
||||||
| function mappings (customLevels = null, useOnlyCustomLevels = false) { | ||||||
|
|
@@ -139,7 +162,7 @@ function mappings (customLevels = null, useOnlyCustomLevels = false) { | |||||
| ) | ||||||
| const values = Object.assign( | ||||||
| Object.create(Object.prototype, { silent: { value: Infinity } }), | ||||||
| useOnlyCustomLevels ? null : levels, | ||||||
| useOnlyCustomLevels ? null : DEFAULT_LEVELS, | ||||||
| customLevels | ||||||
| ) | ||||||
| return { labels, values } | ||||||
|
|
@@ -160,7 +183,7 @@ function assertDefaultLevelFound (defaultLevel, customLevels, useOnlyCustomLevel | |||||
|
|
||||||
| const labels = Object.assign( | ||||||
| Object.create(Object.prototype, { silent: { value: Infinity } }), | ||||||
| useOnlyCustomLevels ? null : levels, | ||||||
| useOnlyCustomLevels ? null : DEFAULT_LEVELS, | ||||||
| customLevels | ||||||
| ) | ||||||
| if (!(defaultLevel in labels)) { | ||||||
|
|
@@ -180,6 +203,20 @@ function assertNoLevelCollisions (levels, customLevels) { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @description Validates whether levelComparison is correct | ||||||
| * @throws Error | ||||||
| * @param {SORTING_ORDER | Function} levelComparison | ||||||
| * @returns | ||||||
| */ | ||||||
| function assertLevelComparison (levelComparison) { | ||||||
obrus-corcentric marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| if (Object.values(SORTING_ORDER).includes(levelComparison) || typeof levelComparison === 'function') { | ||||||
| return | ||||||
| } | ||||||
obrus-corcentric marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| throw new Error('Levels comparison should be one of "ASC", "DESC" or "function" type') | ||||||
jsumners marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| module.exports = { | ||||||
| initialLsCache, | ||||||
| genLsCache, | ||||||
|
|
@@ -188,7 +225,8 @@ module.exports = { | |||||
| setLevel, | ||||||
| isLevelEnabled, | ||||||
| mappings, | ||||||
jsumners marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| levels, | ||||||
| assertNoLevelCollisions, | ||||||
| assertDefaultLevelFound | ||||||
| assertDefaultLevelFound, | ||||||
| genLevelComparison, | ||||||
| assertLevelComparison | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.