Log every request your Hono app handles, plus anything else you want to log, with a few lines of middleware.
Install the Better Stack logging library for the runtime your Hono app runs on:
In your src/index.ts, create the Better Stack client and add a middleware that logs each request once its response is ready:
Other edge runtimes without an execution context, such as Deno Deploy, use logtail.log(...) directly like the Node.js version.
All HTTP requests handled by Hono will be logged and sent to Better Stack.
Log anything else with debug(), info(), warn(), and error():
On Node.js, logs are sent in batches. Ensure all logs are flushed to Better Stack before the process exits:
You should see your logs in Better Stack β Live tail.
The middleware uses c.req.routePath and c.error, available since Hono v4.
Please let us know at hello@betterstack.com.
We're happy to help! π
The middleware runs after await next(), when Hono has finished the response, and logs each request once with these fields:
method and path: the request as it came in, without the query string.route: the matched route pattern such as /users/:id, handy for grouping requests in dashboards.status and duration_ms: the response status and how long the request took.Requests that end with a 2xx or 3xx status code are logged to Better Stack with Info log level.
Requests with 4xx status codes, including 404 for unknown routes, are considered warnings. They are logged with Warn log level.
Requests with 5xx status codes are considered errors. They are logged with Error log level.
When a handler throws, Hono's error handler turns the error into the response (500 by default, or the status of an HTTPException) before the middleware logs the request. The exception is available as c.error and is sent under the error field together with its message and stack trace.
The middleware is part of your app, so extend it as you need:
user_agent: c.req.header("user-agent") or query: c.req.query().await next(), for example if (c.req.path === "/health") return;.Everything from the Logtail JavaScript client applies, and the Cloudflare Workers logging page covers the execution context in more depth.
We use cookies to authenticate users, improve the product user experience, and for personalized ads. Learn more.