Skip to content

Instantly share code, notes, and snippets.

@tanishqkancharla
Created July 6, 2025 18:14
Show Gist options
  • Save tanishqkancharla/e46353cbec718dab2a40bd3272c625aa to your computer and use it in GitHub Desktop.
Save tanishqkancharla/e46353cbec718dab2a40bd3272c625aa to your computer and use it in GitHub Desktop.
ORPC Logging Middleware
const loggerMiddleware = os
.$context<{ req: Request }>()
.middleware(async ({ context, next, path }) => {
const start = Date.now();
const method = context.req.method;
const pathname = `/${path.join("/")}`;
try {
const result = await next({});
const duration = Date.now() - start;
const status = 200; // Success status
console.log(
`${formatNow()} | ${method} ${pathname} ${colorStatus(status)} ${duration}ms`,
);
return result;
} catch (error) {
const duration = Date.now() - start;
const status =
error instanceof ORPCError
? error.code === "UNAUTHORIZED"
? 401
: error.code === "BAD_REQUEST"
? 400
: error.code === "NOT_FOUND"
? 404
: 500
: 500;
console.error(error);
console.log(
`${formatNow()} | ${method} ${pathname} ${colorStatus(status)} ${duration}ms`,
);
throw error;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment