DEV Community

Ethan Arrowood
Ethan Arrowood

Posted on

2 3

TypeScript support for Pino with Fastify

By default, Fastify ships with a Pino instance as its logger; however, since Pino types are not maintained (yet) by the Pino project itself, Fastify does not ship type support for the complete Pino API. Understandably, this can be frustrating, but in this post I want to show you just how easy it is to add @types/pino to your Fastify app.

  1. Install Pino Types

    npm i -D @types/pino
    
  2. Augment the FastifyLoggerInstance type in the same file the app is instantiated in (generally index.ts or server.ts)

    import type { Logger } from 'pino'
    declare module 'fastify' {
        interface FastifyLoggerInstance extends Logger {}
    }
    

This works because in the v3 Fastify types, the FastifyInstance.log property is defined as a generic Logger that is defaulted to FastifyLoggerInstance (ref). Declaration merging the FastifyLoggerInstance type with interface FastifyLoggerInstance extends Logger {}, informs TypeScript to consider the type as an extension of the logger type defined in @types/pino.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay