Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: better logger middleware #3963

Open
jansepke opened this issue Feb 27, 2025 · 1 comment
Open

Request: better logger middleware #3963

jansepke opened this issue Feb 27, 2025 · 1 comment
Labels
enhancement New feature or request.

Comments

@jansepke
Copy link

What is the feature you are proposing?

I would like to improve the logger middleware of hono. The current logger middleware is very basic and likely only intented for development purpose. For production application I would like to add a logger library (e.g. pino or winston) and was not able to find any or good middlewares for those.

I started to wrap pino-http (pino's express middleware) to make it work with hono (see PR pinojs/pino#2136) but it did not felt so nice. I would prefer a more native way of using a logging library together with hono.

So what do you think about adapting the existing logger middleware (backwards compatible) or creating a new logger middleware that is flexible enough to support any logging library and handles this features:

  • providing a request bound logger instance on the context
  • allow any logger library
  • measures response times
  • could log request/response details
  • is used as the error handler
  • (optional) integration with hono's request-id middleware

it could be used like this

import { Hono } from 'hono';
import { requestId } from 'hono/request-id';
import { pino } from 'pino';

const logger = pino();

const app = new Hono();
app.use(requestId());
app.use(loggerV2({
  createRequestLogger: (c) => logger.child({ requestId: c.var.requestId }),
  onRequest: (c) => c.var.logger.info({ userAgent: c.req.header('User-Agent') }, "request start"),
  onResponse: (c, responseTime) => c.var.logger.info({ responseTime }, "request end"),
}));

app.get('/', (c) => {
  c.var.logger.info('something');

  return c.text('Hello Node.js!');
});

Do you think something like this would be usefull or should every logger library build its own middleware for hono? I would also be intested to work on this feature.

@jansepke jansepke added the enhancement New feature or request. label Feb 27, 2025
@atoko
Copy link

atoko commented Mar 9, 2025

I think there's the matter of context here, there are two things at play:

  • Create a function that satisfies Hono<{ Variables: logger: ReturnType createRequestLogger }>
  • Middleware hooks for before/after request

I think the first one is more useful, but when you consider the framework, it interacts in a lot of places with the client API. Take for example, nested routers. Remember that these are created with

import { Hono } from "hono";

app.route( new Hono() ) // I want to use the logger inside nested router, but if I use() the middleware, I'll have 2 logger instances :(

This is a contrived example, (I think the current approach is to merge the inner app context with the outer router, via typeof).

Hono context is still a new paradigm but with some more work it really could be great, IMO.

Just my unrelated 2 cents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

2 participants