npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@hono/structured-logger

v0.1.0

Published

Structured Logger middleware for Hono

Readme

@hono/structured-logger

Structured Logger middleware for Hono.

Library agnostic: works with pino, winston, bunyan, console, or any logger that implements the BaseLogger interface. Zero dependencies. Provides a request scoped logger on c.var.logger with full type safety, automatic response time measurement, and native integration with hono/request-id.

Install

npm install @hono/structured-logger

Usage

With pino

import { Hono } from 'hono'
import { requestId } from 'hono/request-id'
import { structuredLogger } from '@hono/structured-logger'
import pino from 'pino'

const rootLogger = pino()

const app = new Hono()

app.use(requestId())
app.use(
  structuredLogger({
    createLogger: (c) => rootLogger.child({ requestId: c.var.requestId }),
  })
)

app.get('/', (c) => {
  c.var.logger.info('handling request')
  return c.text('Hello!')
})

With winston

import { Hono } from 'hono'
import { structuredLogger } from '@hono/structured-logger'
import winston from 'winston'

const rootLogger = winston.createLogger({
  /* config */
})

const app = new Hono()

app.use(
  structuredLogger({
    createLogger: (c) => rootLogger.child({ requestId: c.var.requestId }),
  })
)

With console (development, zero deps)

import { Hono } from 'hono'
import { structuredLogger } from '@hono/structured-logger'

const app = new Hono()

app.use(
  structuredLogger({
    createLogger: () => console,
  })
)

Custom hooks

import { Hono } from 'hono'
import { structuredLogger } from '@hono/structured-logger'
import pino from 'pino'

const rootLogger = pino()

const app = new Hono()

app.use(
  structuredLogger({
    createLogger: (c) => rootLogger.child({ requestId: c.var.requestId }),
    onRequest: (logger, c) => {
      logger.info(
        {
          method: c.req.method,
          path: c.req.path,
          userAgent: c.req.header('user-agent'),
        },
        'incoming request'
      )
    },
    onResponse: (logger, c, elapsedMs) => {
      logger.info(
        {
          status: c.res.status,
          elapsedMs,
          contentLength: c.res.headers.get('content-length'),
        },
        'request completed'
      )
    },
    onError: (logger, err, c) => {
      logger.error(
        {
          err,
          method: c.req.method,
          path: c.req.path,
        },
        'request failed'
      )
    },
  })
)

Custom context key

If you already have a logger variable on your context, use contextKey to pick a different name:

app.use(
  structuredLogger({
    createLogger: () => myLogger,
    contextKey: 'log',
  })
)

app.get('/', (c) => {
  c.var.log.info('hello')
  return c.text('ok')
})

Type safe context

Declare the logger type on your Hono app for full type safety:

import type { pino } from 'pino'

type Env = {
  Variables: {
    logger: pino.Logger
  }
}

const app = new Hono<Env>()

API

structuredLogger(options)

Returns a Hono MiddlewareHandler.

Options

| Option | Type | Required | Default | Description | | -------------- | --------------------------------------------------------------------- | -------- | -------------------------------------------------------- | ----------------------------------------------------- | | createLogger | (c: Context) => L | Yes | | Factory that creates a request scoped logger instance | | contextKey | string | No | 'logger' | Key used to store the logger on c.var | | onRequest | (logger: L, c: Context) => void \| Promise<void> | No | Logs method + path at info level | Called before handler execution | | onResponse | (logger: L, c: Context, elapsedMs: number) => void \| Promise<void> | No | Logs method, path, status and elapsed time at info level | Called after handler execution | | onError | (logger: L, err: Error, c: Context) => void \| Promise<void> | No | Logs error, method, path and status at error level | Called when handler throws |

BaseLogger

Minimal interface your logger must implement:

interface BaseLogger {
  info(obj: unknown, msg?: string, ...args: unknown[]): void
  warn(obj: unknown, msg?: string, ...args: unknown[]): void
  error(obj: unknown, msg?: string, ...args: unknown[]): void
  debug(obj: unknown, msg?: string, ...args: unknown[]): void
}

Compatible with pino, winston, bunyan, console, and most logging libraries out of the box.

Behavior

  1. createLogger(c) is called once per request.
  2. The logger is stored on c.var[contextKey].
  3. onRequest fires before handler execution.
  4. After handler completes, onResponse fires with elapsed time in milliseconds (measured via performance.now()).
  5. If the handler throws, Hono's error handler runs first, then onError fires (checking c.error). onResponse is skipped when an error occurred.
  6. onError and onResponse are mutually exclusive per request.

Runtime compatibility

Works on all runtimes supported by Hono: Node.js, Deno, Bun, Cloudflare Workers, AWS Lambda, Vercel Edge, Fastly Compute. No Node specific APIs used.

License

MIT