Sasan Jaghori
Sasan Jaghori
Explore posts from servers
HHono
Created by Sasan Jaghori on 6/10/2024 in #help
hono logger with log levels
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,
});
4 replies