Comparing version 8.12.1 to 8.14.0
@@ -51,7 +51,9 @@ 'use strict' | ||
const levels = ['error', 'fatal', 'warn', 'info', 'debug', 'trace'] | ||
const customLevels = Object.keys(opts.customLevels || {}) | ||
const levels = ['error', 'fatal', 'warn', 'info', 'debug', 'trace'].concat(customLevels) | ||
if (typeof proto === 'function') { | ||
proto.error = proto.fatal = proto.warn = | ||
proto.info = proto.debug = proto.trace = proto | ||
levels.forEach(function (level) { | ||
proto[level] = proto | ||
}) | ||
} | ||
@@ -78,3 +80,3 @@ if (opts.enabled === false || opts.browser.disabled) opts.level = 'silent' | ||
} | ||
logger.levels = pino.levels | ||
logger.levels = getLevels(opts) | ||
logger.level = level | ||
@@ -117,2 +119,6 @@ | ||
set(setOpts, logger, 'trace', 'log') | ||
customLevels.forEach(function (level) { | ||
set(setOpts, logger, level, 'log') | ||
}) | ||
} | ||
@@ -161,2 +167,22 @@ | ||
function getLevels (opts) { | ||
const customLevels = opts.customLevels || {} | ||
const values = Object.assign({}, pino.levels.values, customLevels) | ||
const labels = Object.assign({}, pino.levels.labels, invertObject(customLevels)) | ||
return { | ||
values, | ||
labels | ||
} | ||
} | ||
function invertObject (obj) { | ||
const inverted = {} | ||
Object.keys(obj).forEach(function (key) { | ||
inverted[obj[key]] = key | ||
}) | ||
return inverted | ||
} | ||
pino.levels = { | ||
@@ -211,4 +237,4 @@ values: { | ||
const transmitLevel = opts.transmit.level || logger.level | ||
const transmitValue = pino.levels.values[transmitLevel] | ||
const methodValue = pino.levels.values[level] | ||
const transmitValue = logger.levels.values[transmitLevel] | ||
const methodValue = logger.levels.values[level] | ||
if (methodValue < transmitValue) return | ||
@@ -220,3 +246,3 @@ transmit(this, { | ||
transmitLevel, | ||
transmitValue: pino.levels.values[opts.transmit.level || logger.level], | ||
transmitValue: logger.levels.values[opts.transmit.level || logger.level], | ||
send: opts.transmit.send, | ||
@@ -238,3 +264,3 @@ val: logger.levelVal | ||
} | ||
o.level = pino.levels.values[level] | ||
o.level = logger.levels.values[level] | ||
let lvl = (logger._childLevel | 0) + 1 | ||
@@ -241,0 +267,0 @@ if (lvl < 1) lvl = 1 |
@@ -124,2 +124,4 @@ # API | ||
logging methods is called. The first parameter is the value `mergeObject` or an empty object. The second parameter is the log level number. | ||
The third parameter is the logger or child logger itself, which can be used to | ||
retrieve logger-specific context from within the `mixin` function. | ||
The function must synchronously return an object. The properties of the returned object will be added to the | ||
@@ -181,4 +183,37 @@ logged JSON. | ||
If the `mixin` feature is being used merely to add static metadata to each log message, | ||
then a [child logger ⇗](/docs/child-loggers.md) should be used instead. | ||
then a [child logger ⇗](/docs/child-loggers.md) should be used instead. Unless your application | ||
needs to concatenate values for a specific key multiple times, in which case `mixin` can be | ||
used to avoid the [duplicate keys caveat](/docs/child-loggers.md#duplicate-keys-caveat): | ||
```js | ||
const logger = pino({ | ||
mixin (obj, num, logger) { | ||
return { | ||
tags: logger.tags | ||
} | ||
} | ||
}) | ||
logger.tags = {} | ||
logger.addTag = function (key, value) { | ||
logger.tags[key] = value | ||
} | ||
function createChild (parent, ...context) { | ||
const newChild = logger.child(...context) | ||
newChild.tags = { ...logger.tags } | ||
newChild.addTag = function (key, value) { | ||
newChild.tags[key] = value | ||
} | ||
return newChild | ||
} | ||
logger.addTag('foo', 1) | ||
const child = createChild(logger, {}) | ||
child.addTag('bar', 2) | ||
logger.info('this will only have `foo: 1`') | ||
child.info('this will have both `foo: 1` and `bar: 2`') | ||
logger.info('this will still only have `foo: 1`') | ||
``` | ||
As of pino 7.x, when the `mixin` is used with the [`nestedKey` option](#opt-nestedkey), | ||
@@ -185,0 +220,0 @@ the object returned from the `mixin` method will also be nested. Prior versions would mix |
@@ -15,2 +15,3 @@ # Help | ||
* [Avoid Message Conflict](#avoid-message-conflict) | ||
* [Best performance for logging to `stdout`](#best-performance-for-stdout) | ||
@@ -292,1 +293,14 @@ <a id="rotate"></a> | ||
``` | ||
<a id="best-performance-for-stdout"></a> | ||
## Best performance for logging to `stdout` | ||
The best performance for logging directly to stdout is _usually_ achieved by using the | ||
default configuration: | ||
```js | ||
const log = require('pino')(); | ||
``` | ||
You should only have to configure custom transports or other settings | ||
if you have broader logging requirements. |
'use strict' | ||
module.exports = { version: '8.12.1' } | ||
module.exports = { version: '8.14.0' } |
@@ -202,3 +202,3 @@ 'use strict' | ||
if (mixin) { | ||
obj = mixinMergeStrategy(obj, mixin(obj, num)) | ||
obj = mixinMergeStrategy(obj, mixin(obj, num, this)) | ||
} | ||
@@ -205,0 +205,0 @@ |
{ | ||
"name": "pino", | ||
"version": "8.12.1", | ||
"version": "8.14.0", | ||
"description": "super fast, all natural json logger", | ||
@@ -5,0 +5,0 @@ "main": "pino.js", |
@@ -83,3 +83,3 @@ // Project: https://github.com/pinojs/pino.git, http://getpino.io | ||
*/ | ||
child<ChildOptions extends pino.ChildLoggerOptions>(bindings: pino.Bindings, options?: ChildOptions): pino.Logger<Options & ChildOptions>; | ||
child<ChildOptions extends pino.ChildLoggerOptions = {}>(bindings: pino.Bindings, options?: ChildOptions): pino.Logger<Options & ChildOptions>; | ||
@@ -86,0 +86,0 @@ /** |
@@ -118,2 +118,25 @@ 'use strict' | ||
test('set custom level and use it', ({ end, same, is }) => { | ||
const expected = [ | ||
{ | ||
level: 31, | ||
msg: 'this is a custom level' | ||
} | ||
] | ||
const instance = pino({ | ||
customLevels: { | ||
success: 31 | ||
}, | ||
browser: { | ||
write (actual) { | ||
checkLogObjects(is, same, actual, expected.shift()) | ||
} | ||
} | ||
}) | ||
instance.success('this is a custom level') | ||
end() | ||
}) | ||
test('the wrong level throws', ({ end, throws }) => { | ||
@@ -120,0 +143,0 @@ const instance = pino() |
@@ -163,1 +163,57 @@ 'use strict' | ||
}) | ||
test('mixin receives logger as third parameter', async ({ ok, same }) => { | ||
const stream = sink() | ||
const instance = pino({ | ||
mixin (context, num, logger) { | ||
ok(logger !== null, 'logger should be defined') | ||
ok(logger !== undefined, 'logger should be defined') | ||
same(logger, instance) | ||
return { ...context, num } | ||
} | ||
}, stream) | ||
instance.level = name | ||
instance[name]({ | ||
message: '123' | ||
}, 'test') | ||
}) | ||
test('mixin receives child logger', async ({ ok, same }) => { | ||
const stream = sink() | ||
let child = null | ||
const instance = pino({ | ||
mixin (context, num, logger) { | ||
ok(logger !== null, 'logger should be defined') | ||
ok(logger !== undefined, 'logger should be defined') | ||
same(logger.expected, child.expected) | ||
return { ...context, num } | ||
} | ||
}, stream) | ||
instance.level = name | ||
instance.expected = false | ||
child = instance.child({}) | ||
child.expected = true | ||
child[name]({ | ||
message: '123' | ||
}, 'test') | ||
}) | ||
test('mixin receives logger even if child exists', async ({ ok, same }) => { | ||
const stream = sink() | ||
let child = null | ||
const instance = pino({ | ||
mixin (context, num, logger) { | ||
ok(logger !== null, 'logger should be defined') | ||
ok(logger !== undefined, 'logger should be defined') | ||
same(logger.expected, instance.expected) | ||
return { ...context, num } | ||
} | ||
}, stream) | ||
instance.level = name | ||
instance.expected = false | ||
child = instance.child({}) | ||
child.expected = true | ||
instance[name]({ | ||
message: '123' | ||
}, 'test') | ||
}) |
@@ -332,1 +332,13 @@ import P, { pino } from "../../"; | ||
const customLevels = { foo: 99 }; | ||
const customLevelLogger = pino({ customLevels }); | ||
type CustomLevelLogger = typeof customLevelLogger | ||
type CustomLevelLoggerLevels = pino.Level | keyof typeof customLevels | ||
const fn = (logger: Pick<CustomLevelLogger, CustomLevelLoggerLevels>) => {} | ||
const customLevelChildLogger = customLevelLogger.child({ name: "child" }) | ||
fn(customLevelChildLogger); // missing foo typing |
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
671794
13055