0Pricing
tRPC End-to-End Type Safe APIs · Lesson

Logging and Performance Timing Middleware

Build observability into your tRPC API with middleware that logs every call and measures its execution time.

Logging and Performance Timing Middleware is a free tRPC End-to-End Type Safe APIs lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the tRPC End-to-End Type Safe APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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;
});

A Simple Logger

Log the procedure type and path on every call.

const logger = t.middleware(async ({ path, type, next }) => {
  console.log("-> " + type + " " + path);
  return next();
});

Measuring Duration

Capture the time before and after next() to compute how long the call took.

const timing = t.middleware(async ({ path, next }) => {
  const start = Date.now();
  const result = await next();
  const ms = Date.now() - start;
  console.log(path + " took " + ms + "ms");
  return result;
});

Inspecting the Result

The result tells you whether the call succeeded, useful for logging error rates.

const result = await next();
console.log(result.ok ? "ok" : "error");
return result;

Attaching to a Procedure

Use .use() to apply middleware when defining a base procedure.

const loggedProcedure = t.procedure.use(logger).use(timing);

Reusing Across Routers

Export the enhanced procedure and build all routers from it so logging applies everywhere automatically.

export const appRouter = router({
  ping: loggedProcedure.query(() => "pong"),
});

Adding Request IDs

Generate a unique id per call to correlate logs across services.

const withId = t.middleware(async ({ next, ctx }) => {
  const reqId = crypto.randomUUID();
  return next({ ctx: { ...ctx, reqId } });
});

Structured Logging

Emit JSON instead of plain text so logs are searchable in tools like Datadog or ELK.

console.log(JSON.stringify({ path, type, ms, level: "info" }));

Order of Middleware

Middleware runs in the order added. Put timing outermost so it includes the work of inner middleware.

Sampling Logs

In high-traffic APIs, logging every call is costly. Sample a percentage of requests so you keep visibility without overwhelming your log pipeline.

if (Math.random() < 0.1) console.log(path);

Quick Check

Test your middleware knowledge.

Recap

You added observability with middleware:

  • Middleware runs code before and after next()
  • Use it for logging, timing, and request IDs
  • Build a shared base procedure so it applies everywhere

Good observability makes a tRPC API far easier to debug in production.

Frequently asked questions

Is the “Logging and Performance Timing Middleware” lesson free?

Yes — the full text of “Logging and Performance Timing Middleware” is free to read here on the web, and the tRPC End-to-End Type Safe APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the tRPC End-to-End Type Safe APIs course, upgrade to CoddyKit PRO.

What will I learn in “Logging and Performance Timing Middleware”?

Build observability into your tRPC API with middleware that logs every call and measures its execution time. You practise tRPC End-to-End Type Safe APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start tRPC End-to-End Type Safe APIs?

No prior experience is required. tRPC End-to-End Type Safe APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Logging and Performance Timing Middleware” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this tRPC End-to-End Type Safe APIs lesson?

Yes. Every tRPC End-to-End Type Safe APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating tRPC Context
  2. Authentication Middleware
  3. Custom Middleware Chains
  4. Logging and Performance Timing Middleware
← Back to tRPC End-to-End Type Safe APIs