Repository: pinojs/pino
Pull request #2125 (2025-02-08 08:01:04)

fix: #2053 - re-added is level enabled to browser
Related to issue #2053 

- re-added isLevelEnabled for browser build
- added unit tests


Patch:
diff --git a/.github/dependabot.yml b/.github/dependabot.yml
index dfa7fa6..35d66ca 100644
--- a/.github/dependabot.yml
+++ b/.github/dependabot.yml
@@ -9,5 +9,5 @@ updates:
   - package-ecosystem: "npm"
     directory: "/"
     schedule:
-      interval: "weekly"
+      interval: "monthly"
     open-pull-requests-limit: 10
diff --git a/browser.js b/browser.js
index 7d0aa2b..c70e8d4 100644
--- a/browser.js
+++ b/browser.js
@@ -118,6 +118,13 @@ function pino (opts) {
   logger.levels = getLevels(opts)
   logger.level = level
 
+  logger.isLevelEnabled = function (level) {
+    if (!this.levels.values[level]) {
+      return false
+    }
+
+    return this.levels.values[level] >= this.levels.values[this.level]
+  }
   logger.setMaxListeners = logger.getMaxListeners =
   logger.emit = logger.addListener = logger.on =
   logger.prependListener = logger.once =
diff --git a/docs/web.md b/docs/web.md
index 49a31d0..4b7bcd4 100644
--- a/docs/web.md
+++ b/docs/web.md
@@ -12,6 +12,7 @@ web framework ecosystem.
   - [Pino with Node core `http`](#pino-with-node-core-http)
   - [Pino with Nest](#pino-with-nest)
   - [Pino with H3](#pino-with-h3)
+  - [Pino with Hono](#pino-with-hono)
 
 <a id="fastify"></a>
 ## Pino with Fastify
@@ -267,3 +268,49 @@ router.get(
 Execute `npx --yes listhen -w --open ./server.mjs`.
 
 See the [pino-http README](https://npm.im/pino-http) for more info.
+
+
+<a id="h3"></a>
+## Pino with Hono
+
+```sh
+npm install pino pino-http hono
+```
+
+```ts
+import { HttpBindings, serve } from '@hono/node-server';
+import { Hono } from 'hono';
+import { requestId } from 'hono/request-id';
+import { pino } from 'pino';
+import { pinoHttp } from 'pino-http';
+
+declare module 'hono' {
+  interface ContextVariableMap {
+    logger: pino.Logger;
+  }
+}
+
+const app = new Hono<{ Bindings: HttpBindings }>();
+app.use(requestId());
+app.use(async (c, next) => {
+  // pass hono's request-id to pino-http
+  c.env.incoming.id = c.var.requestId;
+
+  // map express style middleware to hono
+  await new Promise<void>((resolve) => pinoHttp()(c.env.incoming, c.env.outgoing, () => resolve()));
+
+  c.set('logger', c.env.incoming.log);
+
+  await next();
+});
+
+app.get('/', (c) => {
+  c.var.logger.info('something');
+
+  return c.text('Hello Node.js!');
+});
+
+serve(app);
+```
+
+See the [pino-http README](https://npm.im/pino-http) for more info.
diff --git a/package.json b/package.json
index 7bb10af..5649fb5 100644
--- a/package.json
+++ b/package.json
@@ -98,7 +98,7 @@
     "through2": "^4.0.0",
     "ts-node": "^10.9.1",
     "tsd": "^0.31.0",
-    "typescript": "~5.7.2",
+    "typescript": "~5.8.2",
     "winston": "^3.7.2"
   },
   "dependencies": {
@@ -107,7 +107,7 @@
     "on-exit-leak-free": "^2.1.0",
     "pino-abstract-transport": "^2.0.0",
     "pino-std-serializers": "^7.0.0",
-    "process-warning": "^4.0.0",
+    "process-warning": "^5.0.0",
     "quick-format-unescaped": "^4.0.3",
     "real-require": "^0.2.0",
     "safe-stable-stringify": "^2.3.1",


Test patch:
diff --git a/test/browser-is-level-enabled.test.js b/test/browser-is-level-enabled.test.js
new file mode 100644
index 0000000..16d8c88
--- /dev/null
+++ b/test/browser-is-level-enabled.test.js
@@ -0,0 +1,104 @@
+'use strict'
+
+const { test } = require('tap')
+const pino = require('../browser')
+
+const customLevels = {
+  trace: 10,
+  debug: 20,
+  info: 30,
+  warn: 40,
+  error: 50,
+  fatal: 60
+}
+
+test('Default levels suite', ({ test, end }) => {
+  test('can check if current level enabled', async ({ equal }) => {
+    const log = pino({ level: 'debug' })
+    equal(true, log.isLevelEnabled('debug'))
+  })
+
+  test('can check if current level enabled when as object', async ({ equal }) => {
+    const log = pino({ asObject: true, level: 'debug' })
+    equal(true, log.isLevelEnabled('debug'))
+  })
+
+  test('can check if level enabled after level set', async ({ equal }) => {
+    const log = pino()
+    equal(false, log.isLevelEnabled('debug'))
+    log.level = 'debug'
+    equal(true, log.isLevelEnabled('debug'))
+  })
+
+  test('can check if higher level enabled', async ({ equal }) => {
+    const log = pino({ level: 'debug' })
+    equal(true, log.isLevelEnabled('error'))
+  })
+
+  test('can check if lower level is disabled', async ({ equal }) => {
+    const log = pino({ level: 'error' })
+    equal(false, log.isLevelEnabled('trace'))
+  })
+
+  test('ASC: can check if child has current level enabled', async ({ equal }) => {
+    const log = pino().child({}, { level: 'debug' })
+    equal(true, log.isLevelEnabled('debug'))
+    equal(true, log.isLevelEnabled('error'))
+    equal(false, log.isLevelEnabled('trace'))
+  })
+
+  test('can check if custom level is enabled', async ({ equal }) => {
+    const log = pino({
+      customLevels: { foo: 35 },
+      level: 'debug'
+    })
+    equal(true, log.isLevelEnabled('foo'))
+    equal(true, log.isLevelEnabled('error'))
+    equal(false, log.isLevelEnabled('trace'))
+  })
+
+  end()
+})
+
+test('Custom levels suite', ({ test, end }) => {
+  test('can check if current level enabled', async ({ equal }) => {
+    const log = pino({ level: 'debug', customLevels })
+    equal(true, log.isLevelEnabled('debug'))
+  })
+
+  test('can check if level enabled after level set', async ({ equal }) => {
+    const log = pino({ customLevels })
+    equal(false, log.isLevelEnabled('debug'))
+    log.level = 'debug'
+    equal(true, log.isLevelEnabled('debug'))
+  })
+
+  test('can check if higher level enabled', async ({ equal }) => {
+    const log = pino({ level: 'debug', customLevels })
+    equal(true, log.isLevelEnabled('error'))
+  })
+
+  test('can check if lower level is disabled', async ({ equal }) => {
+    const log = pino({ level: 'error', customLevels })
+    equal(false, log.isLevelEnabled('trace'))
+  })
+
+  test('can check if child has current level enabled', async ({ equal }) => {
+    const log = pino().child({ customLevels }, { level: 'debug' })
+    equal(true, log.isLevelEnabled('debug'))
+    equal(true, log.isLevelEnabled('error'))
+    equal(false, log.isLevelEnabled('trace'))
+  })
+
+  test('can check if custom level is enabled', async ({ equal }) => {
+    const log = pino({
+      customLevels: { foo: 35, ...customLevels },
+      level: 'debug'
+    })
+    equal(true, log.isLevelEnabled('foo'))
+    equal(true, log.isLevelEnabled('error'))
+    equal(false, log.isLevelEnabled('trace'))
+  })
+
+  end()
+})
diff --git a/test/pkg/pkg.test.js b/test/pkg/pkg.test.js
index 63a6112..3cb28ad 100644
--- a/test/pkg/pkg.test.js
+++ b/test/pkg/pkg.test.js
@@ -8,7 +8,7 @@ const { join } = require('node:path')
 const { platform } = require('node:process')
 const execFile = promisify(require('node:child_process').execFile)
 
-const skip = process.env.PNPM_CI || process.env.CITGM
+const skip = process.env.PNPM_CI || process.env.CITGM || process.arch === 'ppc64'
 
 /**
  * The following regex is for tesintg the deprecation warning that is thrown by the `punycode` module.