Logging and Performance Timing Middleware
Build observability into your tRPC API with middleware that logs every call and measures its execution time.
Why Observe Every Call?
You already use middleware for auth. Middleware is also perfect for cross-cutting concerns like logging and timing that should apply to many procedures.
Middleware Recap
A tRPC middleware wraps a procedure: it runs code, calls next(), then can run code after the result.
const mw = t.middleware(async (opts) => {
// before
const result = await opts.next();
// after
return result;
});All lessons in this course
- Creating tRPC Context
- Authentication Middleware
- Custom Middleware Chains
- Logging and Performance Timing Middleware