Prettify structured logging

When initializing a Node.js app (glitch-hello-node), the default code comes with this snippet:

// Require the fastify framework and instantiate it
const fastify = require("fastify")({
  // set this to true for detailed logging:
  logger: false,
});

Changing the logger from false to true causes fastify to write logs in JSON format which looks like this.

I find that such log is very hard to parse. Would be great if JSON-formatted can be parsed and displayed in an easier-to-scan manner by Glitch’s log viewer, e.g.

  • Hide trivial fields like time, pid, hostname by default
  • Show different colors for different levels.
  • Show msg first, followed by other fields.

Hi, if you’re ok with doing this yourself, it looks like you can customise how the logger works by providing a custom pino logger to the logger property - you might be able to play around with the logging functions and see if you can get anywhere.
Alternatively, you could use the pino-pretty package, something like this:

const pino = require('pino')
const pretty = require('pino-pretty')
const logger = pino(pretty())

const fastify = require('fastify')({
  logger
})

Hi, thanks for a suggestion! Fastify’s docs itself also suggests a solution:

const fastify = require('fastify')({
  logger: {
      prettyPrint:
        environment === 'development'
          ? {
              translateTime: 'HH:MM:ss Z',
              ignore: 'pid,hostname'
            }
          : false
    }
})

However, as the doc said, “Enabling the logger with appropriate configuration for both local development and production environment requires bit more configuration.”

The starter code simply suggested just setting this to true, and in other observability platforms (e.g. Elastic, Datadog) JSON logs are parsed and displayed in a nice way out-of-the-box. So might be great if Glitch also has such support.

1 Like