@tigawanna/hono-pino
v0.7.1
Published
A pino logger plugin for hono
Readme
Hono + Pino
This repository is inspired by pino-http and nestjs-pino.
Runtime Support
[!IMPORTANT] This is fork of the original hono-pino project that adds minimal output message option for cleaner logs
ex: with minimal enabled
return logger({
pino: pino(
{
level: envVariables.LOG_LEVEL || "info",
},
envVariables.NODE_ENV === "production"
? undefined
: pretty({
colorize: true,
}),
),
http: {
reqId: () => crypto.randomUUID(),
minimalMessage: true,
},
});[10:37:26.771] INFO (223659): ✅ /api/v1/inventory GET 200 OK application/json 29ms
With custom bindings
return logger({
pino: pino(
{
level: envVariables.LOG_LEVEL || "info",
},
envVariables.NODE_ENV === "production"
? undefined
: pretty({
colorize: true,
}),
),
http: {
reqId: () => crypto.randomUUID(),
minimalMessage: (b, c) => {
return {
extra: "i want to log this too",
...b,
};
},
},
});[10:38:23.093] INFO (225266): Request completed
extra: "i want to log this too"
res: {
"status": 200,
"headers": {}
}
req: {
"url": "/api/v1/inventory",
"method": "GET",
"headers": {
[!IMPORTANT] This package uses pino, the pino is designed for Node.js and support browser environment,
for edge environments (e.g. Cloudflare Workers), some pino advanced features maybe not working,
if fixing these issues is feasible, I will make an effort to implement it, but I cannot guarantee this.
known issues:
transports: Alternative -> browser.write
Installation
# npm
npm install @tigawanna/hono-pino pino
# pnpm
pnpm add @tigawanna/hono-pino pino
# bun
bun add @tigawanna/hono-pino pinoUsage
import { Hono } from 'hono'
import { pinoLogger } from '@tigawanna/hono-pino'
const app = new Hono()
.use(
pinoLogger({
pino: {level: "debug"}
}),
)
.get((c) => {
const { logger } = c.var;
const token = c.req.header("Authorization") ?? "";
logger.debug({ token });
const user = getAuthorizedUser(token);
logger.assign({ user });
const posts = getPosts();
logger.assign({ posts });
logger.setResMessage("Get posts success"); // optional
return c.text("");
});
await app.request("/", {
headers: {
Authorization: "Bearer token",
},
});
// output (formatted for easier reading)
{"level":20, "token":"Bearer token"}
{
"level": 30,
"msg": "Get posts success",
"user": {
"id": 1,
"name": "john"
},
"posts": [
{
"id": 1,
"title": "My first post"
},
{
"id": 2,
"title": "My second post"
}
],
"req": {
"headers": {
"authorization": "Bearer token"
},
"method": "GET",
"url": "/"
},
"reqId": 1,
"res": {
"headers": {},
"status": 200
},
"responseTime": 2
}Example
see examples
Options & Types
View the full options in JSR
View the full options in github
Logger method
class PinoLogger {
// Same as pino[logger level]
trace: pino.LogFn
debug: pino.LogFn
info: pino.LogFn
warn: pino.LogFn
error: pino.LogFn
fatal: pino.LogFn
// Get bindings (object)
bindings(): pino.Bindings
// Reset bindings
resetBindings(): void
// Assign bindings (default shallow merge)
assign(
bindings: pino.Bindings
opts?: {
/** deep merge */
deep?: boolean;
},
): void
}