TanStackT
TanStack5mo ago
2 replies
cold-orange

Middleware global logging examples

Hello! I was wondering if anybody has managed to produce reasonable logging for tanstack/start on the server? I cannot find any examples of logging request/response and the details of it. request is always undefined (data coming in) and response is mostly just the serverFN response if there is one.

import {
  createMiddleware,
  registerGlobalMiddleware,
} from "@tanstack/react-start";
import * as Sentry from "@sentry/tanstackstart-react";

registerGlobalMiddleware({
  middleware: [
    createMiddleware({ type: "function" }).server(
      Sentry.sentryGlobalServerMiddlewareHandler()
    ),
    createMiddleware({ type: 'function' }).server(
      async ({ next, data, context }) => {
        console.log('Request received:', data)
        console.log('Context:', context)
        const result = await next()
        console.log('Response processed:', result)
        return result
      },
    )
  ],
});

This is my middleware, but this doesn't really give me proper server logs and looking at the response of result it seems to be a lot of stuff we don't need.

I couldn't find any examples of just ending up with json logs like:
req: {
      "id": 1,
      "method": "GET",
      "url": "/",
      "headers": {
        "host": "localhost:3000",
        "user-agent": "curl/7.43.0",
        "accept": "*/*"
      },
      "remoteAddress": "::1",
      "remotePort": 64386
    }


Or how to construct them based on the fact I don't seem to have anything coming in for the request. Has anybody managed to do something with this?
Was this page helpful?