Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.1 KB
Raw Blame
const SENSITIVE_HEADERS = ['authorization', 'cookie', 'x-api-key']

// Wide log class for accumulating request data and emitting a single log at the end
export class RequestLogger {
    private data: Record<string, unknown> = {}
    private startTime = Date.now()

    constructor(requestId?: string) {
        this.data.requestId = requestId ?? crypto.randomUUID().slice(0, 8)
    }

    extend(data: Record<string, unknown>): void {
        Object.assign(this.data, data)
    }

    emit(status: number): void {
        this.data.status = status
        this.data.durationMs = Date.now() - this.startTime
        console.info('[MCP]', JSON.stringify(this.data))
    }
}

type FetchHandler<Props> = (
    request: Request,
    env: Env,
    ctx: ExecutionContext<Props>,
    log: RequestLogger
) => Promise<Response>

// Middleware that wraps a handler with automatic wide logging
export function withLogging<Props>(handler: FetchHandler<Props>) {
    return async (request: Request, env: Env, ctx: ExecutionContext<Props>) => {
        const log = new RequestLogger()

        const url = new URL(request.url)
        const headers: Record<string, string> = {}
        request.headers.forEach((value, key) => {
            headers[key] = SENSITIVE_HEADERS.includes(key.toLowerCase()) ? '[REDACTED]' : value
        })

        log.extend({
            method: request.method,
            pathname: url.pathname,
            search: url.search,
            mcpSessionId: request.headers.get('mcp-session-id') ?? undefined,
            headers,
        })

        try {
            const response = await handler(request, env, ctx, log)
            const responseSessionId = response.headers.get('mcp-session-id')
            if (responseSessionId && responseSessionId !== request.headers.get('mcp-session-id')) {
                log.extend({ mcpSessionId: responseSessionId })
            }
            log.emit(response.status)
            return response
        } catch (error) {
            log.extend({ error: error instanceof Error ? error.message : String(error) })
            log.emit(500)
            throw error
        }
    }
}