UNPKG

hono-pino-logger

Version:
46 lines (43 loc) 1.14 kB
import pino from 'pino'; import { getPath } from 'hono/utils/url'; // src/index.ts var pinoLogger = (logger = pino({ level: "info" })) => { return async (c, next) => { const { method } = c.req; const path = getPath(c.req.raw); logger.info( { requestId: "requestId" in c.req ? c.req.requestId : void 0, request: { method, path } }, "Incoming request" ); const start = Date.now(); await next(); const { status } = c.res; logger.info( { requestId: "requestId" in c.req ? c.req.requestId : void 0, response: { status, ok: String(c.res.ok), time: time(start) } }, "Request completed" ); }; }; function humanize(times) { const [delimiter, separator] = [",", "."]; const orderTimes = times.map((v) => v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + delimiter)); return orderTimes.join(separator); } function time(start) { const delta = Date.now() - start; return humanize([delta < 1e3 ? delta + "ms" : Math.round(delta / 1e3) + "s"]); } export { pinoLogger };