Skip to main content
Home

Built and signed on GitHub Actions

A middleware for handling logging with Pino for Hono

This package works with DenoIt is unknown whether this package works with Cloudflare Workers, Bun
It is unknown whether this package works with Cloudflare Workers
This package works with Deno
It is unknown whether this package works with Bun
JSR Score
70%
Published
a year ago (0.0.7)

Typescript

@bramanda48/hono-pino

A middleware for handling logging with Pino for Hono

JSR release GitHub release License

Installation & Usage

Install @bramanda48/hono-pino using deno add:

deno add jsr:@bramanda48/hono-pino

Example usage :

import { Hono } from "@hono/hono";
import { logger } from "@bramanda48/hono-pino";

const app = new Hono().use(logger());

app.get("/", (ctx) => {
  const logger = ctx.get("logger");
  logger.info("This is info log");
  return ctx.text("Hello World!");
});

app.get("/error", async (ctx) => {
  try {
    throw new Error("Something went wrong");
  } catch (e) {
    const logger = ctx.get("logger");
    logger.error(e);
  }
  return ctx.text("Catch the error!");
});

app.get("/wait", (ctx) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(ctx.text("Oke! See response time!"));
    }, 2000);
  });
});

Deno.serve({ port: 3000 }, app.fetch);

For more sample, you can see in Example folder

Options

export interface PinoOptions<ContextKey> {
  /**
   * Context key for logger
   *
   * @default "logger"
   */
  contextKey?: ContextKey;
  /**
   * Header name for Request ID
   *
   * @default "X-Request-ID"
   */
  requestIdKey?: string;
  /**
   * Pino logger instance
   */
  pino?: Logger | LoggerOptions | DestinationStream;
  /**
   * Pino http bindings
   */
  http?: IPinoHttp;
  /**
   * Response time
   *
   * @default true
   */
  responseTime?: boolean;
}

Request and Response Bindings

With bindings you can feel free to custom the request and response log. you just create class to custom it. For example.


import { logger } from "@bramanda48/hono-pino";

export class CustomHttpBindings extends PinoHttp {
  override RequestId(): Promise<string> {
    return Promise.resolve(crypto.randomUUID());
  }

  override onRequest(ctx: Context): Bindings {
    return {
      req: {
        url: ctx.req.path,
        method: ctx.req.method,
        headers: ctx.req.header(),
      },
    };
  }

  override onRequestLevel(): Level {
    return "info";
  }

  override onRequestMessage(): string {
    return "Request started";
  }

  override onResponse(ctx: Context): Bindings {
    return {
      res: {
        status: ctx.res.status,
        headers: ctx.res.headers,
      },
    };
  }

  override onResponseLevel(ctx: Context): Level {
    return ctx.error ? "error" : "info";
  }

  override onResponseMessage(ctx: Context): string {
    return ctx.error ? ctx.error.message : "Request completed";
  }
}

After that, just add it to logger configuration.

import { Hono } from "@hono/hono";
import { logger } from "@bramanda48/hono-pino";

const app = new Hono().use(logger({
  http: new CustomHttpBindings()
}));

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Acknowledgments

The code in this repository is a rewrite version for deno. Original source code can be found in here maou-shonen/hono-pino.

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@bramanda48/hono-pino

Import symbol

import * as hono_pino from "@bramanda48/hono-pino";
or

Import directly with a jsr specifier

import * as hono_pino from "jsr:@bramanda48/hono-pino";

Add Package

bunx jsr add @bramanda48/hono-pino

Import symbol

import * as hono_pino from "@bramanda48/hono-pino";