Overview
Log all requests with timing information and color-coded status.
Features
- Request timing
- Color-coded output
- Status code logging
- Method and path logging
Installation
npx hanma add hono-logger --framework hono
Dependencies
hono
Code
import { logger } from "hono/logger";
import { createMiddleware } from "hono/factory";
/**
* Custom logger that includes request details and timing
*/
export const customLogger = createMiddleware(async (c, next) => {
const start = Date.now();
const method = c.req.method;
const path = c.req.path;
await next();
const ms = Date.now() - start;
const status = c.res.status;
const logMessage = `[HONO] ${method} ${path} - ${status} (${ms}ms)`;
if (status >= 500) {
console.error(`\x1b[31m${logMessage}\x1b[0m`);
} else if (status >= 400) {
console.warn(`\x1b[33m${logMessage}\x1b[0m`);
} else {
console.log(`\x1b[32m${logMessage}\x1b[0m`);
}
});
/**
* Standard Hono logger integration
*/
export const setupLogger = (app: any) => {
app.use("*", logger());
app.use("*", customLogger);
};
Usage
import { setupLogger } from './middleware/logger';
setupLogger(app);
Dependencies
hono
Frequently Asked Questions
How do I install Logger Middleware?
Run the following command:
npx hanma add hono-logger --framework honoWhat dependencies are required?
This snippet requires: hono. Make sure to install them before using the snippet.
How do I use this snippet?
Copy the code and paste it into your project. The snippet will be saved to
src/middleware/logger.ts.Can I customize this snippet?
Yes! All snippets are copy-paste code that you own. Feel free to modify them to fit your needs.
Does this work with other frameworks?
This snippet is designed for the specific framework. Check the other framework pages for equivalent implementations.