LogTide
Framework
Easy

Fastify Logging Integration

Add structured logging to Fastify applications with plugin-based integration, lifecycle hooks, and trace propagation.

Native Fastify plugin Lifecycle hook integration Request scoping Auto error capture

LogTide’s Fastify SDK provides a native plugin that hooks into Fastify’s lifecycle for automatic request logging, per-request scoping, and distributed tracing.

Why use LogTide with Fastify?

  • Native plugin architecture: Follows Fastify plugin conventions
  • Lifecycle hooks: Automatic instrumentation via onRequest, onResponse, onError
  • Per-request scoping: Isolated context per request through decorators
  • Auto-shutdown: Flushes logs on server close via onClose hook
  • Zero overhead: Asynchronous batching keeps Fastify fast

Prerequisites

  • Node.js 18+
  • Fastify 4.x or 5.x
  • LogTide instance with a DSN

Installation

npm install @logtide/fastify

Quick Start

import Fastify from 'fastify';
import { hub } from '@logtide/core';
import { logtide } from '@logtide/fastify';

const fastify = Fastify();

// Register the plugin (initializes LogTide automatically)
await fastify.register(logtide, {
  dsn: process.env.LOGTIDE_DSN,
  service: 'api-server',
  environment: process.env.NODE_ENV,
});

fastify.get('/users/:id', async (request) => {
  request.logtideScope.setTag('userId', request.params.id);

  hub.captureLog('info', 'Fetching user', {
    userId: request.params.id,
  });

  return { id: request.params.id };
});

await fastify.listen({ port: 3000 });

Plugin Options

await fastify.register(logtide, {
  dsn: process.env.LOGTIDE_DSN,
  service: 'api-server',
  environment: process.env.NODE_ENV,
  release: '1.0.0',
  tracesSampleRate: 1.0,
});

Request Scoping

Each request gets an isolated scope:

fastify.addHook('onRequest', async (request) => {
  request.logtideScope.setExtra('userId', request.user?.id);
  request.logtideScope.setTag('tenant', request.headers['x-tenant-id']);
});

Lifecycle Integration

The plugin hooks into Fastify’s lifecycle automatically:

HookBehavior
onRequestCreates scope, extracts traceparent, starts span
onResponseLogs completion with duration and status
onErrorCaptures errors with request context
onCloseFlushes pending events on shutdown

Error Handling

fastify.setErrorHandler((error, request, reply) => {
  // Error already captured by LogTide plugin
  reply.status(error.statusCode || 500).send({
    error: error.message,
    traceId: request.logtideTraceId,
  });
});

Next Steps