Created
July 6, 2025 18:14
-
-
Save tanishqkancharla/e46353cbec718dab2a40bd3272c625aa to your computer and use it in GitHub Desktop.
ORPC Logging Middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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