Repository: pinojs/pino
Pull request #1480 (2022-06-30 04:35:00)

fix(#1347): added disabled to browser opts for logs
adds disable logs for browser's console


Patch:
diff --git a/browser.js b/browser.js
index 51989ba..8a72166 100644
--- a/browser.js
+++ b/browser.js
@@ -53,7 +53,7 @@ function pino (opts) {
     proto.error = proto.fatal = proto.warn =
     proto.info = proto.debug = proto.trace = proto
   }
-  if (opts.enabled === false) opts.level = 'silent'
+  if (opts.enabled === false || opts.browser.disabled) opts.level = 'silent'
   const level = opts.level || 'info'
   const logger = Object.create(proto)
   if (!logger.log) logger.log = noop
diff --git a/docs/browser.md b/docs/browser.md
index cf8a692..0bfa42d 100644
--- a/docs/browser.md
+++ b/docs/browser.md
@@ -197,3 +197,12 @@ const pino = require('pino')({
   }
 })
 ```
+
+### `disabled` (Boolean)
+
+```js
+const pino = require('pino')({browser: {disabled: true}})
+```
+
+The `disabled` option will disable logging in browser if set
+to `true`. Default is set to `false`.


Test patch:
diff --git a/test/browser-disabled.test.js b/test/browser-disabled.test.js
new file mode 100644
index 0000000..36d1b11
--- /dev/null
+++ b/test/browser-disabled.test.js
@@ -0,0 +1,87 @@
+'use strict'
+const test = require('tape')
+const pino = require('../browser')
+
+test('set browser opts disabled to true', ({ end, same }) => {
+  const instance = pino({
+    browser: {
+      disabled: true,
+      write (actual) {
+        checkLogObjects(same, actual, [])
+      }
+    }
+  })
+  instance.info('hello world')
+  instance.error('this is an error')
+  instance.fatal('this is fatal')
+
+  end()
+})
+
+test('set browser opts disabled to false', ({ end, same }) => {
+  const expected = [
+    {
+      level: 30,
+      msg: 'hello world'
+    },
+    {
+      level: 50,
+      msg: 'this is an error'
+    },
+    {
+      level: 60,
+      msg: 'this is fatal'
+    }
+  ]
+  const instance = pino({
+    browser: {
+      disabled: false,
+      write (actual) {
+        checkLogObjects(same, actual, expected.shift())
+      }
+    }
+  })
+  instance.info('hello world')
+  instance.error('this is an error')
+  instance.fatal('this is fatal')
+
+  end()
+})
+
+test('disabled is not set in browser opts', ({ end, same }) => {
+  const expected = [
+    {
+      level: 30,
+      msg: 'hello world'
+    },
+    {
+      level: 50,
+      msg: 'this is an error'
+    },
+    {
+      level: 60,
+      msg: 'this is fatal'
+    }
+  ]
+  const instance = pino({
+    browser: {
+      write (actual) {
+        checkLogObjects(same, actual, expected.shift())
+      }
+    }
+  })
+  instance.info('hello world')
+  instance.error('this is an error')
+  instance.fatal('this is fatal')
+
+  end()
+})
+
+function checkLogObjects (same, actual, expected, is) {
+  const actualCopy = Object.assign({}, actual)
+  const expectedCopy = Object.assign({}, expected)
+  delete actualCopy.time
+  delete expectedCopy.time
+
+  same(actualCopy, expectedCopy)
+}
diff --git a/test/types/pino.test-d.ts b/test/types/pino.test-d.ts
index 6dde5b2..b2a6515 100644
--- a/test/types/pino.test-d.ts
+++ b/test/types/pino.test-d.ts
@@ -103,6 +103,7 @@ pino({
                 logEvent.messages;
             },
         },
+        disabled: false
     },
 });