selim
selim

Reputation: 391

pretty is not defined in fastify pretty print

I'm trying to use prettyprint: true on fastify logger options like this:

const build = require("./app");

const startServer = async () => {
  try {
    const app = await build({
      logger: {
        prettyPrint: true
      },
    });

    const PORT = process.env.PORT;
    await app.listen(PORT);
  } catch (err) {
    console.log(err);
    process.exit(1);
  }
};

startServer().catch();

This code is from official docs from here and here.

The pino-pretty package is installed

And I get this error when I try to run server:

const formatted = pretty(typeof redact === 'function' ? redact(obj) : obj)
                        ^

TypeError: pretty is not a function

Checked for this line of code (/node_modules/pino/lib/tools.js 282), pretty type is object, but in the code it is used as function

const formatted = pretty(typeof redact === 'function' ? redact(obj) : obj)

Any ideas?

Upvotes: 2

Views: 2292

Answers (2)

colo
colo

Reputation: 121

For the googlers, since i was not sure how to ue fastify logger with typescript, you can obtain pretty the same result using:

const envToLogger = {
development: {
transport: {
  target: 'pino-pretty',
  options: {
    translateTime: 'HH:MM:ss Z',
    ignore: 'pid,hostname',
  },
},
},
production: true,
test: false,
}

And then:

const server = Fastify({
   logger:  envToLogger["development"], // or your env in general
 })

More here: https://fastify.dev/docs/latest/Reference/Logging#enable-logging

Upvotes: 0

selim
selim

Reputation: 391

Found a solution

  1. Install pino and pino-pretty packages (npm i pino pino-pretty)

  2. Initialize logger by requiring pino and adding pretty-print: true to options

const build = require("./app");

const startServer = async () => {
  try {
    const app = await build({
      logger: require("pino")({ prettyPrint: true }), // here
    });

    await app.listen(app.config.PORT);
  } catch (err) {
    console.log(err);
    process.exit(1);
  }
};

startServer().catch();

Upvotes: 2

Related Questions