hono logger with log levels

Is there anyway we can use hono logging Middleware the way we use pino js e.g.?
3 Replies
Aditya Mathur
Aditya Mathur6mo ago
You can use your choice of logger with the hono logger middleware - https://hono.dev/middleware/builtin/logger#printfunc
Logger Middleware - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
Aditya Mathur
Aditya Mathur6mo ago
Or are you talking about the implementation then can you share some sudo code?
Sasan Jaghori
Sasan JaghoriOP6mo ago
inorder to use pino with different log levels, I have combined them like this:
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { logger as loggerMiddleware } from "hono/logger";
import pino from "pino";

const app = new Hono();

const logger = pino({
transport: {
target: "pino-pretty",
options: {
colorize: true,
},
},
level: "trace",
});

export const customLogger = (message: string, ...rest: string[]) => {
logger.info(message, ...rest);
};

app.use(loggerMiddleware(customLogger));

app.get("/", (c) => {
logger.info("hello");
return c.text("Hello Hono!");
});

const port = 3000;
console.log(`Server is running on port ${port}`);

serve({
fetch: app.fetch,
port,
});
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { logger as loggerMiddleware } from "hono/logger";
import pino from "pino";

const app = new Hono();

const logger = pino({
transport: {
target: "pino-pretty",
options: {
colorize: true,
},
},
level: "trace",
});

export const customLogger = (message: string, ...rest: string[]) => {
logger.info(message, ...rest);
};

app.use(loggerMiddleware(customLogger));

app.get("/", (c) => {
logger.info("hello");
return c.text("Hello Hono!");
});

const port = 3000;
console.log(`Server is running on port ${port}`);

serve({
fetch: app.fetch,
port,
});
Want results from more Discord servers?
Add your server