MatrixAI / js-logger

TypeScript/JavaScript Logger Library
https://polykey.com
Apache License 2.0
1 stars 0 forks source link

Using `console.error` instead of `console.log` to print to stderr #5

Closed emmacasolin closed 2 years ago

emmacasolin commented 2 years ago

Description

Switching from console.log to console.error in order to log to stderr rather than stdout.

Tasks

Final checklist

CMCDragonkai commented 2 years ago

This changes the default log handler to print out to STDERR instead of STDOUT. This makes sense for most unixy applications.

In browsers and in jest, console.error is augmented in ways beyond console.log. Both of these will print it out more "prettier" than normal.

For example:

image

And jest will highlight any usage of console.error, and probably show where it is coming from. This can be quite useful to find out any logs that being printed out that aren't connected to the logger hierarchy.

In some ways, this is a breaking change though, so merging this should bump a major version.

CMCDragonkai commented 2 years ago

In Jest this is what it will look like now:

  console.error
    2020-01-01T00:00:00.000Z:WARN MESSAGE
      3 | class ConsoleHandler extends Handler {
      4 |   public emit(output: string): void {
    > 5 |     console.error(output);
        |             ^
      6 |   }
      7 | }
      8 |
      at console.<anonymous> (node_modules/jest-mock/build/index.js:854:25)
      at ConsoleHandler.emit (src/handlers/ConsoleHandler.ts:5:13)
      at ConsoleHandler.handle (src/Handler.ts:18:10)
      at Logger.callHandlers (src/Logger.ts:112:15)

Compared to before:

  console.log
    2020-01-01T00:00:00.000Z:DEBUG MESSAGE
      at console.<anonymous> (node_modules/jest-mock/build/index.js:854:25)
CMCDragonkai commented 2 years ago

Before merging this, you want to check that this won't result in crazy amounts of noise in our downstream projects. I believe it shouldn't because downstream projects should be using StreamHandler.

emmacasolin commented 2 years ago

Before merging this, you want to check that this won't result in crazy amounts of noise in our downstream projects. I believe it shouldn't because downstream projects should be using StreamHandler.

How would I check for this?

CMCDragonkai commented 2 years ago

Just monkey patch the node_modules/@matrixai/logger with console.error or use npm link.

tegefaulkes commented 2 years ago

js-polykey passes all tests after monkey patching the node module to use console.error. I checked to see what it looks like if we use the default console handler. It's pretty much the same as the console.log but with some extra trace information.

I think this should be good to merge.