oRPC Pino Integration

Integrate oRPC with Pino for structured logging and request tracking.

SKILL.md

GitHub

Pino Integration

Pino is a fast and lightweight JSON logger.

Installation

npm install @orpc/experimental-pino@latest pino@latest

Setup

import { LoggingHandlerPlugin } from '@orpc/experimental-pino'
import pino from 'pino'

const logger = pino()

const handler = new RPCHandler(router, {
  plugins: [
    new LoggingHandlerPlugin({
      logger,
      generateId: ({ request }) => crypto.randomUUID(),
      logRequestResponse: true,
      logRequestAbort: true,
    }),
  ],
})

For development, use pino-pretty:

npm run dev | npx pino-pretty

Using the Logger

import { getLogger, LoggerContext } from '@orpc/experimental-pino'

interface ORPCContext extends LoggerContext {}

const procedure = os
  .$context<ORPCContext>()
  .handler(({ context }) => {
    const logger = getLogger(context)

    logger?.info('Processing request')
    logger?.debug({ userId: 123 }, 'User data')

    return { success: true }
  })

Custom Logger per Request

Useful when integrating with pino-http:

import {
  CONTEXT_LOGGER_SYMBOL,
  LoggerContext,
  LoggingHandlerPlugin
} from '@orpc/experimental-pino'

const logger = pino()
const httpLogger = pinoHttp({ logger })

interface ORPCContext extends LoggerContext {}

const handler = new RPCHandler(router, {
  plugins: [new LoggingHandlerPlugin({ logger })],
})

const server = createServer(async (req, res) => {
  httpLogger(req, res)

  const { matched } = await handler.handle(req, res, {
    prefix: '/api',
    context: {
      [CONTEXT_LOGGER_SYMBOL]: req.log,
    },
  })

  if (!matched) {
    res.statusCode = 404
    res.end('Not Found')
  }
})