Repository: pinojs/pino (JavaScript)

Issue:
Overriding messageKey not working
Hi Team,

I'm just getting started with Pino, and try to override the messageKey as mentioned in the documentation.  But it's always using the default msg as key.

My config

````
const formatters = {
    level (label) {
      return { severity : label }
    }
  }
const logger = require('pino')({
    messageKey: 'message',
    browser: {
        asObject: true, 
        formatters: formatters,
        serialize: true,
        write: (o) => {
            console.log(JSON.stringify(o))
          }
    },
    base: undefined,
    timestamp: false,
    level: 'info',
})

 logger.info(`Random name is some name`)
  logger.warn("experimenting warning")
  logger.error("Evil, program failed")
````

and the output is:
{"severity":"info","msg":"Random name is relevant-indigo"}
{"severity":"warn","msg":"experimenting warning"}
{"severity":"error","msg":"Evil, program failed"}

like to have message as key instead of msg.

Please help with the right config to achieve the expected result.

Additional info:
Pino version: 9.1.0
Tested with cloudflare workers and hit the request from browser

please let me know if additional details are required.

Regards,
Raj


Discussion:
Thanks for reporting! 

It does not look this is supported in the browser impl. 
Would you like to send a Pull Request to address this issue? Remember to add unit tests.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.



Patch:
diff --git a/browser.js b/browser.js
index a27cd231b..8560f1f99 100644
--- a/browser.js
+++ b/browser.js
@@ -111,7 +111,8 @@ function pino (opts) {
     asObject: opts.browser.asObject,
     formatters: opts.browser.formatters,
     levels,
-    timestamp: getTimeFunction(opts)
+    timestamp: getTimeFunction(opts),
+    messageKey: opts.messageKey || 'msg'
   }
   logger.levels = getLevels(opts)
   logger.level = level
@@ -308,7 +309,7 @@ function createWrap (self, opts, rootLogger, level) {
         argsIsSerialized = true
       }
       if (opts.asObject || opts.formatters) {
-        write.call(proto, asObject(this, level, args, ts, opts.formatters))
+        write.call(proto, asObject(this, level, args, ts, opts))
       } else write.apply(proto, args)
 
       if (opts.transmit) {
@@ -330,11 +331,11 @@ function createWrap (self, opts, rootLogger, level) {
   })(self[baseLogFunctionSymbol][level])
 }
 
-function asObject (logger, level, args, ts, formatters = {}) {
+function asObject (logger, level, args, ts, opts) {
   const {
     level: levelFormatter,
     log: logObjectFormatter = (obj) => obj
-  } = formatters
+  } = opts.formatters || {}
   const argsCloned = args.slice()
   let msg = argsCloned[0]
   const logObject = {}
@@ -358,7 +359,7 @@ function asObject (logger, level, args, ts, formatters = {}) {
     }
     msg = argsCloned.length ? format(argsCloned.shift(), argsCloned) : undefined
   } else if (typeof msg === 'string') msg = format(argsCloned.shift(), argsCloned)
-  if (msg !== undefined) logObject.msg = msg
+  if (msg !== undefined) logObject[opts.messageKey] = msg
 
   const formattedLogObject = logObjectFormatter(logObject)
   return formattedLogObject


Test patch:
diff --git a/test/browser.test.js b/test/browser.test.js
index f2bb9b318..8684006b4 100644
--- a/test/browser.test.js
+++ b/test/browser.test.js
@@ -165,6 +165,23 @@ test('opts.browser.asObject logs pino-like object to console', ({ end, ok, is })
   instance.info('test')
   end()
 })
+test('opts.browser.asObject uses opts.messageKey in logs', ({ end, ok, is }) => {
+  const messageKey = 'message'
+  const instance = require('../browser')({
+    messageKey,
+    browser: {
+      asObject: true,
+      write: function (o) {
+        is(o.level, 30)
+        is(o[messageKey], 'test')
+        ok(o.time)
+      }
+    }
+  })
+
+  instance.info('test')
+  end()
+})
 
 test('opts.browser.formatters (level) logs pino-like object to console', ({ end, ok, is }) => {
   const info = console.info