Comparing version 8.17.2 to 8.18.0
@@ -68,2 +68,28 @@ # API | ||
<a id=opt-customlevels></a> | ||
#### `levelComparison` ("ASC", "DESC", Function) | ||
Default: `ASC` | ||
Use this option to customize levels order. | ||
In order to be able to define custom levels ordering pass a function which will accept `current` and `expected` values and return `boolean` which shows should `current` level to be shown or not. | ||
```js | ||
const logger = pino({ | ||
levelComparison: 'DESC', | ||
customLevels: { | ||
foo: 20, // `foo` is more valuable than `bar` | ||
bar: 10 | ||
}, | ||
}) | ||
// OR | ||
const logger = pino({ | ||
levelComparison: function(current, expected) { | ||
return current >= expected; | ||
} | ||
}) | ||
``` | ||
#### `customLevels` (Object) | ||
@@ -579,9 +605,11 @@ | ||
<a id="destination"></a> | ||
### `destination` (SonicBoom | WritableStream | String | Object) | ||
### `destination` (Number | String | Object | DestinationStream | SonicBoomOpts | WritableStream) | ||
Default: `pino.destination(1)` (STDOUT) | ||
The `destination` parameter, at a minimum must be an object with a `write` method. | ||
An ordinary Node.js `stream` can be passed as the destination (such as the result | ||
of `fs.createWriteStream`) but for peak log writing performance it is strongly | ||
The `destination` parameter can be a file descriptor, a file path, or an | ||
object with `dest` property pointing to a fd or path. | ||
An ordinary Node.js `stream` file descriptor can be passed as the | ||
destination (such as the result | ||
of `fs.createWriteStream`) but for peak log writing performance, it is strongly | ||
recommended to use `pino.destination` to create the destination stream. | ||
@@ -588,0 +616,0 @@ Note that the `destination` parameter can be the result of `pino.transport()`. |
@@ -9,17 +9,11 @@ 'use strict' | ||
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) { | ||
@@ -37,11 +31,11 @@ const stream = this[streamSym] | ||
}, | ||
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 | ||
@@ -124,5 +118,37 @@ }, {}) | ||
const logLevelVal = values[logLevel] | ||
return logLevelVal !== undefined && (logLevelVal >= this[levelValSym]) | ||
return logLevelVal !== undefined && this[levelCompSym](logLevelVal, this[levelValSym]) | ||
} | ||
/** | ||
* Determine if the given `current` level is enabled by comparing it | ||
* against the current threshold (`expected`). | ||
* | ||
* @param {SORTING_ORDER} direction comparison direction "ASC" or "DESC" | ||
* @param {number} current current log level number representatiton | ||
* @param {number} expected threshold value to compare with | ||
* @returns {boolean} | ||
*/ | ||
function compareLevel (direction, current, expected) { | ||
if (direction === SORTING_ORDER.DESC) { | ||
return current <= expected | ||
} | ||
return current >= expected | ||
} | ||
/** | ||
* Create a level comparison function based on `levelComparison` | ||
* it could a default function which compares levels either in "ascending" or "descending" order or custom comparison function | ||
* | ||
* @param {SORTING_ORDER | Function} levelComparison sort levels order direction or custom comparison function | ||
* @returns Function | ||
*/ | ||
function genLevelComparison (levelComparison) { | ||
if (typeof levelComparison === 'string') { | ||
return compareLevel.bind(null, levelComparison) | ||
} | ||
return levelComparison | ||
} | ||
function mappings (customLevels = null, useOnlyCustomLevels = false) { | ||
@@ -145,3 +171,3 @@ const customNums = customLevels | ||
Object.create(Object.prototype, { silent: { value: Infinity } }), | ||
useOnlyCustomLevels ? null : levels, | ||
useOnlyCustomLevels ? null : DEFAULT_LEVELS, | ||
customLevels | ||
@@ -167,3 +193,3 @@ ) | ||
Object.create(Object.prototype, { silent: { value: Infinity } }), | ||
useOnlyCustomLevels ? null : levels, | ||
useOnlyCustomLevels ? null : DEFAULT_LEVELS, | ||
customLevels | ||
@@ -188,2 +214,21 @@ ) | ||
/** | ||
* Validates whether `levelComparison` is correct | ||
* | ||
* @throws Error | ||
* @param {SORTING_ORDER | Function} levelComparison - value to validate | ||
* @returns | ||
*/ | ||
function assertLevelComparison (levelComparison) { | ||
if (typeof levelComparison === 'function') { | ||
return | ||
} | ||
if (typeof levelComparison === 'string' && Object.values(SORTING_ORDER).includes(levelComparison)) { | ||
return | ||
} | ||
throw new Error('Levels comparison should be one of "ASC", "DESC" or "function" type') | ||
} | ||
module.exports = { | ||
@@ -197,5 +242,6 @@ initialLsCache, | ||
mappings, | ||
levels, | ||
assertNoLevelCollisions, | ||
assertDefaultLevelFound | ||
assertDefaultLevelFound, | ||
genLevelComparison, | ||
assertLevelComparison | ||
} |
'use strict' | ||
module.exports = { version: '8.17.2' } | ||
module.exports = { version: '8.18.0' } |
'use strict' | ||
const metadata = Symbol.for('pino.metadata') | ||
const { levels } = require('./levels') | ||
const { DEFAULT_LEVELS } = require('./constants') | ||
const DEFAULT_INFO_LEVEL = levels.info | ||
const DEFAULT_INFO_LEVEL = DEFAULT_LEVELS.info | ||
@@ -13,3 +13,3 @@ function multistream (streamsArray, opts) { | ||
const streamLevels = Object.create(levels) | ||
const streamLevels = Object.create(DEFAULT_LEVELS) | ||
streamLevels.silent = Infinity | ||
@@ -16,0 +16,0 @@ if (opts.levels && typeof opts.levels === 'object') { |
@@ -6,2 +6,3 @@ 'use strict' | ||
const levelValSym = Symbol('pino.levelVal') | ||
const levelCompSym = Symbol('pino.levelComp') | ||
const useLevelLabelsSym = Symbol('pino.useLevelLabels') | ||
@@ -46,2 +47,3 @@ const useOnlyCustomLevelsSym = Symbol('pino.useOnlyCustomLevels') | ||
levelValSym, | ||
levelCompSym, | ||
useLevelLabelsSym, | ||
@@ -48,0 +50,0 @@ mixinSym, |
{ | ||
"name": "pino", | ||
"version": "8.17.2", | ||
"version": "8.18.0", | ||
"description": "super fast, all natural json logger", | ||
@@ -99,3 +99,3 @@ "main": "pino.js", | ||
"ts-node": "^10.9.1", | ||
"tsd": "^0.29.0", | ||
"tsd": "^0.30.4", | ||
"typescript": "^5.1.3", | ||
@@ -102,0 +102,0 @@ "winston": "^3.7.2" |
@@ -358,2 +358,8 @@ // Project: https://github.com/pinojs/pino.git, http://getpino.io | ||
/** | ||
* Use this option to define custom comparison of log levels. | ||
* Usefull to compare custom log levels or non-standard level values. | ||
* Default: "ASC" | ||
*/ | ||
levelComparison?: "ASC" | "DESC" | ((current: number, expected: number) => boolean); | ||
/** | ||
* Use this option to only use defined `customLevels` and omit Pino's levels. | ||
@@ -769,9 +775,9 @@ * Logger's default `level` must be changed to a value in `customLevels` in order to use `useOnlyCustomLevels` | ||
* Create a Pino Destination instance: a stream-like object with significantly more throughput (over 30%) than a standard Node.js stream. | ||
* @param [dest]: The `destination` parameter, at a minimum must be an object with a `write` method. An ordinary Node.js | ||
* `stream` can be passed as the destination (such as the result of `fs.createWriteStream`) but for peak log | ||
* writing performance it is strongly recommended to use `pino.destination` to create the destination stream. | ||
* @param [dest]: The `destination` parameter, can be a file descriptor, a file path, or an object with `dest` property pointing to a fd or path. | ||
* An ordinary Node.js `stream` file descriptor can be passed as the destination (such as the result of `fs.createWriteStream`) | ||
* but for peak log writing performance, it is strongly recommended to use `pino.destination` to create the destination stream. | ||
* @returns A Sonic-Boom stream to be used as destination for the pino function | ||
*/ | ||
export function destination( | ||
dest?: string | number | SonicBoomOpts | DestinationStream | NodeJS.WritableStream, | ||
dest?: number | object | string | DestinationStream | NodeJS.WritableStream | SonicBoomOpts, | ||
): SonicBoom; | ||
@@ -804,3 +810,3 @@ | ||
*/ | ||
declare function pino<CustomLevels extends string = never>(options: LoggerOptions<CustomLevels>, stream: DestinationStream): Logger<CustomLevels>; | ||
declare function pino<CustomLevels extends string = never>(options: LoggerOptions<CustomLevels>, stream?: DestinationStream | undefined): Logger<CustomLevels>; | ||
@@ -859,2 +865,3 @@ | ||
// (Legacy support for early 7.x releases, remove in 8.x.) | ||
export type { pino as P }; | ||
export type { pino as P }; | ||
12
pino.js
@@ -11,3 +11,4 @@ 'use strict' | ||
const { configure } = require('safe-stable-stringify') | ||
const { assertDefaultLevelFound, mappings, genLsCache, levels } = require('./lib/levels') | ||
const { assertDefaultLevelFound, mappings, genLsCache, genLevelComparison, assertLevelComparison } = require('./lib/levels') | ||
const { DEFAULT_LEVELS, SORTING_ORDER } = require('./lib/constants') | ||
const { | ||
@@ -40,2 +41,3 @@ createArgsNormalizer, | ||
mixinSym, | ||
levelCompSym, | ||
useOnlyCustomLevelsSym, | ||
@@ -54,3 +56,4 @@ formattersSym, | ||
level: 'info', | ||
levels, | ||
levelComparison: SORTING_ORDER.ASC, | ||
levels: DEFAULT_LEVELS, | ||
messageKey: 'msg', | ||
@@ -103,2 +106,3 @@ errorKey: 'err', | ||
customLevels, | ||
levelComparison, | ||
mixin, | ||
@@ -164,4 +168,8 @@ mixinMergeStrategy, | ||
assertLevelComparison(levelComparison) | ||
const levelCompFunc = genLevelComparison(levelComparison) | ||
Object.assign(instance, { | ||
levels, | ||
[levelCompSym]: levelCompFunc, | ||
[useOnlyCustomLevelsSym]: useOnlyCustomLevels, | ||
@@ -168,0 +176,0 @@ [streamSym]: stream, |
@@ -6,39 +6,181 @@ 'use strict' | ||
test('can check if current level enabled', async ({ equal }) => { | ||
const log = pino({ level: 'debug' }) | ||
equal(true, log.isLevelEnabled('debug')) | ||
}) | ||
const descLevels = { | ||
trace: 60, | ||
debug: 50, | ||
info: 40, | ||
warn: 30, | ||
error: 20, | ||
fatal: 10 | ||
} | ||
test('can check if level enabled after level set', async ({ equal }) => { | ||
const log = pino() | ||
equal(false, log.isLevelEnabled('debug')) | ||
log.level = 'debug' | ||
equal(true, log.isLevelEnabled('debug')) | ||
}) | ||
const ascLevels = { | ||
trace: 10, | ||
debug: 20, | ||
info: 30, | ||
warn: 40, | ||
error: 50, | ||
fatal: 60 | ||
} | ||
test('can check if higher level enabled', async ({ equal }) => { | ||
const log = pino({ level: 'debug' }) | ||
equal(true, log.isLevelEnabled('error')) | ||
test('Default levels suite', ({ test, end }) => { | ||
test('can check if current level enabled', async ({ equal }) => { | ||
const log = pino({ level: 'debug' }) | ||
equal(true, log.isLevelEnabled('debug')) | ||
}) | ||
test('can check if level enabled after level set', async ({ equal }) => { | ||
const log = pino() | ||
equal(false, log.isLevelEnabled('debug')) | ||
log.level = 'debug' | ||
equal(true, log.isLevelEnabled('debug')) | ||
}) | ||
test('can check if higher level enabled', async ({ equal }) => { | ||
const log = pino({ level: 'debug' }) | ||
equal(true, log.isLevelEnabled('error')) | ||
}) | ||
test('can check if lower level is disabled', async ({ equal }) => { | ||
const log = pino({ level: 'error' }) | ||
equal(false, log.isLevelEnabled('trace')) | ||
}) | ||
test('ASC: can check if child has current level enabled', async ({ equal }) => { | ||
const log = pino().child({}, { level: 'debug' }) | ||
equal(true, log.isLevelEnabled('debug')) | ||
equal(true, log.isLevelEnabled('error')) | ||
equal(false, log.isLevelEnabled('trace')) | ||
}) | ||
test('can check if custom level is enabled', async ({ equal }) => { | ||
const log = pino({ | ||
customLevels: { foo: 35 }, | ||
level: 'debug' | ||
}) | ||
equal(true, log.isLevelEnabled('foo')) | ||
equal(true, log.isLevelEnabled('error')) | ||
equal(false, log.isLevelEnabled('trace')) | ||
}) | ||
end() | ||
}) | ||
test('can check if lower level is disabled', async ({ equal }) => { | ||
const log = pino({ level: 'error' }) | ||
equal(false, log.isLevelEnabled('trace')) | ||
test('Ascending levels suite', ({ test, end }) => { | ||
const customLevels = ascLevels | ||
const levelComparison = 'ASC' | ||
test('can check if current level enabled', async ({ equal }) => { | ||
const log = pino({ level: 'debug', levelComparison, customLevels, useOnlyCustomLevels: true }) | ||
equal(true, log.isLevelEnabled('debug')) | ||
}) | ||
test('can check if level enabled after level set', async ({ equal }) => { | ||
const log = pino({ levelComparison, customLevels, useOnlyCustomLevels: true }) | ||
equal(false, log.isLevelEnabled('debug')) | ||
log.level = 'debug' | ||
equal(true, log.isLevelEnabled('debug')) | ||
}) | ||
test('can check if higher level enabled', async ({ equal }) => { | ||
const log = pino({ level: 'debug', levelComparison, customLevels, useOnlyCustomLevels: true }) | ||
equal(true, log.isLevelEnabled('error')) | ||
}) | ||
test('can check if lower level is disabled', async ({ equal }) => { | ||
const log = pino({ level: 'error', customLevels, useOnlyCustomLevels: true }) | ||
equal(false, log.isLevelEnabled('trace')) | ||
}) | ||
test('can check if child has current level enabled', async ({ equal }) => { | ||
const log = pino().child({ levelComparison, customLevels, useOnlyCustomLevels: true }, { level: 'debug' }) | ||
equal(true, log.isLevelEnabled('debug')) | ||
equal(true, log.isLevelEnabled('error')) | ||
equal(false, log.isLevelEnabled('trace')) | ||
}) | ||
test('can check if custom level is enabled', async ({ equal }) => { | ||
const log = pino({ | ||
levelComparison, | ||
useOnlyCustomLevels: true, | ||
customLevels: { foo: 35, ...customLevels }, | ||
level: 'debug' | ||
}) | ||
equal(true, log.isLevelEnabled('foo')) | ||
equal(true, log.isLevelEnabled('error')) | ||
equal(false, log.isLevelEnabled('trace')) | ||
}) | ||
end() | ||
}) | ||
test('can check if child has current level enabled', async ({ equal }) => { | ||
const log = pino().child({}, { level: 'debug' }) | ||
equal(true, log.isLevelEnabled('debug')) | ||
equal(true, log.isLevelEnabled('error')) | ||
equal(false, log.isLevelEnabled('trace')) | ||
test('Descending levels suite', ({ test, end }) => { | ||
const customLevels = descLevels | ||
const levelComparison = 'DESC' | ||
test('can check if current level enabled', async ({ equal }) => { | ||
const log = pino({ level: 'debug', levelComparison, customLevels, useOnlyCustomLevels: true }) | ||
equal(true, log.isLevelEnabled('debug')) | ||
}) | ||
test('can check if level enabled after level set', async ({ equal }) => { | ||
const log = pino({ levelComparison, customLevels, useOnlyCustomLevels: true }) | ||
equal(false, log.isLevelEnabled('debug')) | ||
log.level = 'debug' | ||
equal(true, log.isLevelEnabled('debug')) | ||
}) | ||
test('can check if higher level enabled', async ({ equal }) => { | ||
const log = pino({ level: 'debug', levelComparison, customLevels, useOnlyCustomLevels: true }) | ||
equal(true, log.isLevelEnabled('error')) | ||
}) | ||
test('can check if lower level is disabled', async ({ equal }) => { | ||
const log = pino({ level: 'error', levelComparison, customLevels, useOnlyCustomLevels: true }) | ||
equal(false, log.isLevelEnabled('trace')) | ||
}) | ||
test('can check if child has current level enabled', async ({ equal }) => { | ||
const log = pino({ levelComparison, customLevels, useOnlyCustomLevels: true }).child({}, { level: 'debug' }) | ||
equal(true, log.isLevelEnabled('debug')) | ||
equal(true, log.isLevelEnabled('error')) | ||
equal(false, log.isLevelEnabled('trace')) | ||
}) | ||
test('can check if custom level is enabled', async ({ equal }) => { | ||
const log = pino({ | ||
levelComparison, | ||
customLevels: { foo: 35, ...customLevels }, | ||
useOnlyCustomLevels: true, | ||
level: 'debug' | ||
}) | ||
equal(true, log.isLevelEnabled('foo')) | ||
equal(true, log.isLevelEnabled('error')) | ||
equal(false, log.isLevelEnabled('trace')) | ||
}) | ||
end() | ||
}) | ||
test('can check if custom level is enabled', async ({ equal }) => { | ||
const log = pino({ | ||
customLevels: { foo: 35 }, | ||
level: 'debug' | ||
test('Custom levels comparison', async ({ test, end }) => { | ||
test('Custom comparison returns true cause level is enabled', async ({ equal }) => { | ||
const log = pino({ level: 'error', levelComparison: () => true }) | ||
equal(true, log.isLevelEnabled('debug')) | ||
}) | ||
equal(true, log.isLevelEnabled('foo')) | ||
equal(true, log.isLevelEnabled('error')) | ||
equal(false, log.isLevelEnabled('trace')) | ||
test('Custom comparison returns false cause level is disabled', async ({ equal }) => { | ||
const log = pino({ level: 'error', levelComparison: () => false }) | ||
equal(false, log.isLevelEnabled('debug')) | ||
}) | ||
test('Custom comparison returns true cause child level is enabled', async ({ equal }) => { | ||
const log = pino({ levelComparison: () => true }).child({ level: 'error' }) | ||
equal(true, log.isLevelEnabled('debug')) | ||
}) | ||
test('Custom comparison returns false cause child level is disabled', async ({ equal }) => { | ||
const log = pino({ levelComparison: () => false }).child({ level: 'error' }) | ||
equal(false, log.isLevelEnabled('debug')) | ||
}) | ||
end() | ||
}) |
@@ -1,5 +0,5 @@ | ||
import P, { pino } from "../../"; | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { Socket } from "net"; | ||
import { expectError, expectType } from 'tsd' | ||
import { expectError, expectType } from 'tsd'; | ||
import P, { pino } from "../../"; | ||
import Logger = P.Logger; | ||
@@ -110,2 +110,4 @@ | ||
pino({}, undefined); | ||
pino({ base: null }); | ||
@@ -428,1 +430,14 @@ if ("pino" in log) console.log(`pino version: ${log.pino}`); | ||
}, process.stdout)); | ||
const pinoWithoutLevelsSorting = pino({}); | ||
const pinoWithDescSortingLevels = pino({ levelComparison: 'DESC' }); | ||
const pinoWithAscSortingLevels = pino({ levelComparison: 'ASC' }); | ||
const pinoWithCustomSortingLevels = pino({ levelComparison: () => false }); | ||
// with wrong level comparison direction | ||
expectError(pino({ levelComparison: 'SOME'}), process.stdout); | ||
// with wrong level comparison type | ||
expectError(pino({ levelComparison: 123}), process.stdout); | ||
// with wrong custom level comparison return type | ||
expectError(pino({ levelComparison: () => null }), process.stdout); | ||
expectError(pino({ levelComparison: () => 1 }), process.stdout); | ||
expectError(pino({ levelComparison: () => 'string' }), process.stdout); |
Sorry, the diff of this file is not supported yet
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
698231
191
13731