fastify / one-line-logger

Helps you format fastify's log into a nice one line message
MIT License
32 stars 7 forks source link

Enabled colorization #42

Closed synapse closed 2 months ago

synapse commented 3 months ago

fixes #35

Checklist

synapse commented 3 months ago

Hey @mcollina, sure. I took a look at pino-pretty and it's using colorette to determine if colorization is supported const { isColorSupported } = require('colorette'). What would you recommend, install that dependency or to try grabbing the code from it?

mcollina commented 3 months ago

I think we could expose it in pino-pretty, and then use it here from there.

synapse commented 3 months ago

@mcollina could you give me a review on this one: https://github.com/pinojs/pino-pretty/pull/513

synapse commented 3 months ago

Hi @mcollina, I wanted to let you know that I have made a new commit that includes a check for the presence of colors in TTY, which is set to true by default. However, updating pino-pretty is still necessary. Once the PR is merged and deployed, I will create another commit. Let me know if that works for you.

mcollina commented 3 months ago

pino-pretty v11.1.0 is out.

mcollina commented 3 months ago

I've pulled this and it does not seem to work. If I redirect the output to a file, the colors are still there.

synapse commented 3 months ago

I've pulled this and it does not seem to work. If I redirect the output to a file, the colors are still there.

Hey @mcollina just a quick question, were you running this from your local bash? I'm guessing pino-pretty.isColorSupported is looking and setting the boolean value based on your TTY support for color and not the redirect output by looking at the colorette package.

const isDisabled = "NO_COLOR" in env || argv.includes("--no-color")
const isForced = "FORCE_COLOR" in env || argv.includes("--color")
const isWindows = platform === "win32"
const isDumbTerminal = env.TERM === "dumb"

const isCompatibleTerminal =
  tty && tty.isatty && tty.isatty(1) && env.TERM && !isDumbTerminal

const isCI =
  "CI" in env &&
  ("GITHUB_ACTIONS" in env || "GITLAB_CI" in env || "CIRCLECI" in env)

export const isColorSupported =
  !isDisabled &&
  (isForced || (isWindows && !isDumbTerminal) || isCompatibleTerminal || isCI)

Do you have any suggestions on how to know, from inside the logger formatter, what will be the outcome of the log (file, std or else)? I'm am guessing this should also overwrite the isColorSupported value, right?

mcollina commented 3 months ago

running the process with CI=true shows the colors

synapse commented 3 months ago

From the colorette's logic when you set CI env you should also need one of the other 3. As for the other thing you've mention to prevent having colors when redirected to a file, we're still looking into it (will keep you in the loop). Would it make sense to have this directly in pino-pretty?

mcollina commented 3 months ago

Maybe. I'm pretty happy with how pino-pretty work, I need to do some checks first, as I might have been too quick on my last test.

simoneb commented 3 months ago

Hey folks, just a quick note here, after discussing offline with @synapse . In order to figure out how existing tools handle it, we checked out jest and here's what we found.

So we started looking into how jest does it, and we discovered that:

  1. jest uses chalk to colorize output
  2. chalk has a fairly convoluted logic to detect this, which is not straightforward to implement, and is considerably more complex than colorette's. see chalk's compared to colorette's
synapse commented 3 months ago

Hey @mcollina any news on this?

mcollina commented 2 months ago

This is ok. There is still one tiny bug to fix.

Consider this:

'use strict'

const fastify = require('fastify')({
  logger: {
    transport: {
      target: './index.js',
      options: {
        colorize: true,
      }
    }
  }
})

fastify.get('/', (req, reply) => {
  req.log.fatal('some info')
  reply.send({ hello: 'world' })
})

fastify.listen({ port: 3042 })

Note that colorize: true is set.

If I do node example.js | less, I get:

Screenshot 2024-06-12 at 10 42 08

However if run it in the normal terminal, I get:

Screenshot 2024-06-12 at 10 43 02
synapse commented 2 months ago

Hey @mcollina,

The less command behaves differently across operating systems (in my case I get colored texts in utf-8 codes).

Here's a summary of our current status to help us decide the next steps:

Unfortunately, color detection in colorette is very basic (compared to chalk - which is also used in libraries like jest and tap).

To automatically disable the output of UTF-8 color strings when not needed, we should address this issue in either the pino-pretty or colorette repositories.

What do you suggest as the best steps moving forward?

mcollina commented 2 months ago

It doesn't seem we are in the same page. I see that the colorize: true option generates two different outputs when the destination is a TTY or not. However, the same does not happen with pino-pretty. As both use the same utility, you should be able to achieve the same behavior.

synapse commented 2 months ago

@mcollina I've updated the PR with the changes and also added it in the docs

mcollina commented 2 months ago

linting is failing