Skip to content

Commit

Permalink
feat(worker): Use custom pino logger for http server
Browse files Browse the repository at this point in the history
  • Loading branch information
tzushimelon committed Feb 6, 2024
1 parent 6dd9d71 commit fe875d5
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/sushii-worker/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { register } from "prom-client";
import { Hono } from "hono";
import { Hono, MiddlewareHandler } from "hono";
import { HTTPException } from "hono/http-exception";
import { logger as honoLogger } from "hono/logger";
import { Server } from "bun";
Expand All @@ -22,14 +22,35 @@ export const ShardStatusToName = {

const httpLogger = logger.child({ name: "http" });

const pinoLoggerMiddleware: MiddlewareHandler = async (c, next) => {
const start = Date.now();
await next();
const elapsed = Date.now() - start;

const log = {
method: c.req.method,
path: c.req.routePath,
status: c.res.status,
elapsed,
};

const message = `${c.req.method} ${c.req.path} ${c.res.status} ${elapsed} ms`;

if (c.res.status >= 400) {
httpLogger.error(log, message);
} else {
httpLogger.debug(log, message);
}
};

export default function server(
client: Client<boolean>,
commands: RESTPostAPIApplicationCommandsJSONBody[],
): Server {
const app = new Hono();

// Middleware
app.use("*", honoLogger());
app.use("*", pinoLoggerMiddleware);

// Handlers
app.notFound((c) => c.json({ message: "Not Found", ok: false }, 404));
Expand Down

0 comments on commit fe875d5

Please sign in to comment.