Repository: pinojs/hapi-pino
Pull request #115 (2020-08-13 17:30:36)

feat: Filter out logs based on event tags
Added a few feature - which enables us to filter the logs bases event tags.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/32739760/badge)](https://coveralls.io/builds/32739760)

Coverage decreased (-0.2%) to 98.907% when pulling **01db4dbace1cab3d82fb370b0019f6df34e8ffe1 on adarshkr:master** into **e73cc1db6cb9616248619c8727c4c9996b6c0df7 on pinojs:master**.

mcollina: CI is failing, can you take a look?
adarshkr: > CI is failing, can you take a look?

![Screenshot 2020-08-14 at 10 14 55 AM](https://user-images.githubusercontent.com/5213251/90214793-4024f780-de17-11ea-9134-cf812e59ac9c.png)

The build was successful. Just code coverage is dropped by 0.07% because of if check.
mcollina: > The build was successful. Just code coverage is dropped by 0.07% because of if check.

No worries
mcollina: Can you rebase? This now conflicts with master.
krlilly: > Can you rebase? This now conflicts with master.

Done. Rebased

Patch:
diff --git a/README.md b/README.md
index 767a961..18de622 100644
--- a/README.md
+++ b/README.md
@@ -234,6 +234,20 @@ events"](#hapievents) section.
 
   (where `myIgnorePaths` would be an array with paths to be ignored).
 
+
+### `options.ignoredEventTags: object[]`
+  Takes an array of object tags and disables logging for each.  Useful for debug logs or any other tags that does not need logging.
+
+  **Default**: `{ log: '*', request: '*' }`, Logs all the events emitted by server.log and request.log without filtering event tags
+
+  **Example**: 
+  Do not log the events for DEBUG and TEST tag
+  ```js
+  ignoredEventTags: { log: ['DEBUG', 'TEST'], request: ['DEBUG', 'TEST'] }
+  server.log(['DEBUG'], 'DEBUG')
+  ```
+
+
 ### `options.level: Pino.Level`
   **Default**: `'info'`
 
diff --git a/index.js b/index.js
index dd562c2..60a722c 100644
--- a/index.js
+++ b/index.js
@@ -15,6 +15,11 @@ const levelTags = {
   error: 'error'
 }
 
+let ignoredEventTags = {
+  log: '*',
+  request: '*'
+}
+
 async function register (server, options) {
   // clone all user options to account for internal mutations, except for existing stream and pino instances
   options = Object.assign(Hoek.clone(options), {
@@ -66,6 +71,10 @@ async function register (server, options) {
     }
   }
 
+  if (options.ignoredEventTags) {
+    ignoredEventTags = { ...ignoredEventTags, ...options.ignoredEventTags }
+  }
+
   const mergeHapiLogData = options.mergeHapiLogData
   const getChildBindings = options.getChildBindings ? options.getChildBindings : (request) => ({ req: request })
   const shouldLogRequestStart = typeof options.logRequestStart === 'function'
@@ -104,7 +113,7 @@ async function register (server, options) {
   server.events.on('log', function (event) {
     if (event.error) {
       logger.error({ err: event.error })
-    } else {
+    } else if (!isCustomTagsLoggingIgnored(event, ignoredEventTags.log)) {
       logEvent(logger, event)
     }
   })
@@ -131,7 +140,7 @@ async function register (server, options) {
         },
         'request error'
       )
-    } else if (event.channel === 'app') {
+    } else if (event.channel === 'app' && !isCustomTagsLoggingIgnored(event, ignoredEventTags.request)) {
       logEvent(request.logger, event)
     }
   })
@@ -171,6 +180,13 @@ async function register (server, options) {
     logger.info(server.info, 'server stopped')
   })
 
+  function isCustomTagsLoggingIgnored (event, ignoredTags) {
+    if (event.tags && ignoredTags !== '*') {
+      return event.tags.some(tag => ignoredTags.indexOf(tag) > -1)
+    }
+    return false
+  }
+
   function isLoggingIgnored (options, request) {
     if (typeof options.ignoreFunc === 'function') {
       return !!options.ignoreFunc(options, request)


Test patch:
diff --git a/test.js b/test.js
index ca5e6a5..57e07d2 100644
--- a/test.js
+++ b/test.js
@@ -2023,3 +2023,44 @@ experiment('log redact', () => {
     await finish
   })
 })
+
+experiment('ignore the log event triggered by request.log and server.log', () => {
+  test('do not log events to console if the event tags are included in ignoredEventTags', async () => {
+    const server = Hapi.server({ port: 0 })
+    let called = false
+    const stream = sink(() => {
+      called = true
+    })
+
+    server.route({
+      path: '/foo',
+      method: 'GET',
+      handler: (req, h) => {
+        req.log(['TEST'], 'even im not getting logged')
+        return 'foo'
+      }
+    })
+    const logger = require('pino')(stream)
+
+    const plugin = {
+      plugin: Pino,
+      options: {
+        instance: logger,
+        logRequestComplete: false,
+        ignoredEventTags: {
+          log: ['test'],
+          request: ['TEST']
+        }
+      }
+    }
+
+    await server.register(plugin)
+    server.log(['test'], 'im not getting logged')
+
+    await server.inject({
+      method: 'GET',
+      url: '/foo'
+    })
+    expect(called).to.be.false()
+  })
+})