Repository: pinojs/pino
Pull request #926 (2020-11-13 18:20:26)

pass merging object to mixins
Allows for mixins to take in an object and add information contextually. 

For instance, if your `mergingObject` has an `error` key (a [JS Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)), I want to add `{ message: error.message, stack: error.stack }`. This will make mixins more useful or having to pull from higher in the scope. 

madelinecameron: Will add a test shortly, sorry about that. :) 
mcollina: This is also missing some docs update
madelinecameron: Yep, was wondering about that, I'll do that. Thanks! 
madelinecameron: Took a crack at integrating it to documentation, let me know any feedback. :) 

Patch:
diff --git a/docs/api.md b/docs/api.md
index 1930921..f84dfcc 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -99,7 +99,7 @@ logger.info('hello') // Will throw an error saying info in not found in logger o
 Default: `undefined`
 
 If provided, the `mixin` function is called each time one of the active
-logging methods is called. The function must synchronously return an
+logging methods is called. The first and only parameter is the value `mergeObject` or an empty object. The function must synchronously return an
 object. The properties of the returned object will be added to the
 logged JSON.
 
@@ -124,7 +124,7 @@ const mixin = {
 }
 
 const logger = pino({
-    mixin() {
+    mixin({ description }) {
         return mixin
     }
 })
diff --git a/docs/pretty.md b/docs/pretty.md
index 6d4b5b6..7be9544 100644
--- a/docs/pretty.md
+++ b/docs/pretty.md
@@ -17,7 +17,7 @@ purposes with other CLI based prettifiers.
 
 Pino prettifier modules are extra modules that provide a CLI for parsing NDJSON
 log lines piped via `stdin` and expose an API which conforms to the Pino
-[metadata streams](api.md#metadata) API.
+[metadata streams](/docs/api.md#metadata) API.
 
 The API requires modules provide a factory function which returns a prettifier
 function. This prettifier function must accept either a string of NDJSON or
diff --git a/lib/proto.js b/lib/proto.js
index 5d1bb08..48da7e3 100644
--- a/lib/proto.js
+++ b/lib/proto.js
@@ -147,9 +147,9 @@ function write (_obj, msg, num) {
   var obj
 
   if (_obj === undefined || _obj === null) {
-    obj = mixin ? mixin() : {}
+    obj = mixin ? mixin({}) : {}
   } else {
-    obj = Object.assign(mixin ? mixin() : {}, _obj)
+    obj = Object.assign(mixin ? mixin(_obj) : {}, _obj)
     if (!msg && objError) {
       msg = _obj.message
     }


Test patch:
diff --git a/test/mixin.test.js b/test/mixin.test.js
index 8ec94ac..94b521d 100644
--- a/test/mixin.test.js
+++ b/test/mixin.test.js
@@ -101,3 +101,36 @@ test('mixin not a function', async ({ throws }) => {
     pino({ mixin: 'not a function' }, stream)
   })
 })
+
+test('mixin can use context', async ({ ok }) => {
+  const stream = sink()
+  const instance = pino({
+    mixin (context) {
+      ok(context !== null && context !== undefined, 'context should be defined')
+      return Object.assign({
+        error: context.message,
+        stack: context.stack
+      })
+    }
+  }, stream)
+  instance.level = name
+  instance[name]({
+    message: '123',
+    stack: 'stack'
+  }, 'test')
+})
+
+test('mixin works without context', async ({ ok }) => {
+  const stream = sink()
+  const instance = pino({
+    mixin (context) {
+      ok(context !== null && context !== undefined, 'context is still defined w/o passing mergeObject')
+
+      return {
+        something: true
+      }
+    }
+  }, stream)
+  instance.level = name
+  instance[name]('test')
+})