File size: 676 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { logger } from "@midday/logger";
import type { Context, MiddlewareHandler } from "hono";

export const httpLogger = (): MiddlewareHandler => {
  return async (context: Context, next) => {
    const start = process.hrtime.bigint();

    const method = context.req.method;
    const url = context.req.url;
    const path = new URL(url).pathname;

    logger.info(`${method} ${path} - incoming request`);

    await next();

    const duration = Number(process.hrtime.bigint() - start) / 1000000; // Convert to ms
    const statusCode = context.res.status;

    logger.info(
      `${method} ${path} ${statusCode} - completed in ${duration.toFixed(2)}ms`,
    );
  };
};