fastify-cli icon indicating copy to clipboard operation
fastify-cli copied to clipboard

Extend the pretty-logs options given to fastify's pino-pretty

Open groma84 opened this issue 2 years ago • 1 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

At the moment the flag --pretty-logs switches to 'pino-pretty' as a target in fastify's logging. But no options are given to pino-pretty while doing so. I propose to extend fastify-cli with a method to set at least some of the possible options for pino-pretty when using --pretty-logs.

I would start with two more CLI switches: --pretty-logs-translate-time and --pretty-logs-ignore which both take a string. If set the values are forwarded to pino-pretty.

Or is there already a way to do this, and I just haven't found it, yet? If so, I propose to extend the documentation with an example.

If one of the two extensions actually make sense I'll try my luck with a PR for them.

Motivation

The default pino-pretty options are on the one hand often unnecessary verbose (printing the pid and the host in every message) and on the other hand hard to read by a human (number timestamp instead of a human readable time).

Example

fastify start --address 0.0.0.0 --log-level debug --pretty-logs --pretty-logs-translate-time="yyyy-mm-dd HH:MM:ss.l" --pretty-logs-ignore="pid,hostname"  dist/app.js

groma84 avatar Aug 10 '22 09:08 groma84

Go ahead and send a PR. I've also just opened https://github.com/pinojs/pino-pretty/pull/366.

mcollina avatar Aug 10 '22 10:08 mcollina

It is possible to configure pino-pretty using --logging-module.

I couldn't find any example in the docs (maybe we can add this explanation somewhere). It took me some experiments to make it work.

You need to: npm install pino pino-pretty --save-dev

create custom_logger.js

const pino = require("pino");
module.exports = pino({
  transport: {
    target: "pino-pretty",
    options: {
      ignore: "pid,req.hostname,req.remoteAddress",
      translateTime: "yyyy-mm-dd HH:MM:ss.l",
    },
  },
});

run something like fastify start --pretty-logs --logging-module="./custom_logger.js" --log-level=info plugin.js

Working demonstration project

https://github.com/avanelli/fastify-cli-pino-pretty-config

avanelli avatar Aug 16 '22 14:08 avanelli