pinojs / pino-pretty

🌲Basic prettifier for Pino log lines

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No output with Jest in WebStorm

OnkelTem opened this issue · comments

I noticed that Jest somehow suppresses pino-pretty output and created these simple tests:

import pino from 'pino';

test('test pino output', () => {
  const logger = pino();
  logger.info('Info');
  logger.error('Error');
});

test('test pino-pretty output', () => {
  const logger = pino({ transport: { target: 'pino-pretty' } });
  logger.info('Info');
  logger.error('Error');
});

Here is the output of running:

  • 'test pino output':
/path/to/.nvm/versions/node/v16.13.1/bin/node --require /snap/webstorm/239/plugins/JavaScriptLanguage/helpers/jest-intellij/lib/jest-intellij-stdin-fix.js /path/to/project/node_modules/jest/bin/jest.js --colors --reporters /snap/webstorm/239/plugins/JavaScriptLanguage/helpers/jest-intellij/lib/jest-intellij-reporter.js --verbose --testNamePattern=^test pino output$ --runTestsByPath /path/to/project/test/pino.test.ts
{"level":30,"time":1642526944225,"pid":46676,"hostname":"fractal","msg":"Info"}
{"level":50,"time":1642526944225,"pid":46676,"hostname":"fractal","msg":"Error"}
  • 'test pino-pretty output'
/path/to/.nvm/versions/node/v16.13.1/bin/node --require /snap/webstorm/239/plugins/JavaScriptLanguage/helpers/jest-intellij/lib/jest-intellij-stdin-fix.js /path/to/project/node_modules/jest/bin/jest.js --colors --reporters /snap/webstorm/239/plugins/JavaScriptLanguage/helpers/jest-intellij/lib/jest-intellij-reporter.js --verbose --testNamePattern=^test pino-pretty output$ --runTestsByPath /path/to/project/test/pino.test.ts

As you may see, there is no any output when using pino-pretty.

Any ideas why?

P.S. I use WebStorm to run tests. Haven't tried to run them in command line. It's worth trying, will write back soon.

I have no clue. You can use const logger = pino(pretty()) for those cases.

I'm not sure what pretty() is?

Got it. It requires an argument:

const logger = pino(pretty({}));

It became even more puzzling now with:

test('test pino-pretty output 2', () => {
  const logger = pino(pretty({}));
  logger.info('Info');
  logger.error('Error');
});

As it returns now only INFO line while error has disappeared:

[1642527375208] INFO (47538 on fractal): Info

Ok, I've discovered that it's a WebStorm bug.
From command line it works correctly:

$ jest -t 'test pino-pretty output'
 PASS  test/pino.test.ts
[1642527675060] INFO (49025 on fractal): Info
[1642527675060] ERROR (49025 on fractal): Error

Test Suites: 2 skipped, 1 passed, 1 of 3 total
Tests:       28 skipped, 1 passed, 29 total
Snapshots:   0 total
Time:        1.532 s, estimated 7 s

I was too fast probably. Problems persist and I don't really get their nature.

Does pino-pretty work asynchronously?
How to use it properly as a transport?

You see, when I use it with jest it can output:

$ jest -t 'test pino-pretty output 2'
 PASS  test/pino.test.ts
[1642527980540] INFO (51472 on fractal): Info

Test Suites: 2 skipped, 1 passed, 1 of 3 total
Tests:       28 skipped, 1 passed, 29 total
Snapshots:   0 total
Time:        1.405 s, estimated 7 s
Ran all test suites with tests matching "test pino-pretty output 2".
[1642527980542] ERROR (51472 on fractal): Error
[1642527980542] ERROR (51472 on fractal): Error2
[1642527980543] ERROR (51472 on fractal): Error3

So, messages on stdout come while jest is working at any moment. Like it was async.
@mcollina Could you please elaborate a little?

No one maintaining this module uses Jest or WebStorm.

@jsumners

That's fine. I just need to know how to use pino-pretty properly. Because now it looks like every call to pino logger has a side-effect, while the code looks like it's synchronous. It doesn't seem to be related to Jest or WebStorm.

How can we improve the documentation to make it easier to understand?

@jsumners we are missing a sync option to in this module, right now we are only logging asynchronously. Then it's docs.

A more clearer test case.

Using pino:

import pino from 'pino';
const logger = pino();
logger.info('Line 1');
console.log('Line 1');
logger.info('Line 2');
console.log('Line 2');
logger.info('Line 3');
console.log('Line 3');

Output:

{"level":30,"time":1642530313924,"pid":54664,"hostname":"fractal","msg":"Line 1"}
Line 1
{"level":30,"time":1642530313924,"pid":54664,"hostname":"fractal","msg":"Line 2"}
Line 2
{"level":30,"time":1642530313925,"pid":54664,"hostname":"fractal","msg":"Line 3"}
Line 3

I.e. it corresponds to what one would expect looking at the code, right?

Using pino-pretty:

import pino from 'pino';
const logger = pino({ transport: { target: 'pino-pretty' } });
logger.info('Line 1');
console.log('Line 1');
logger.info('Line 2');
console.log('Line 2');
logger.info('Line 3');
console.log('Line 3');

Output is:

Line 1
Line 2
Line 3
[1642530508280] INFO (54903 on fractal): Line 1
[1642530508281] INFO (54903 on fractal): Line 2
[1642530508281] INFO (54903 on fractal): Line 3

So... I understand why it could happen. But, really, that doesn't look coherent with the code :(

P.S. Also, I now see how wrong I was trying to use it with my tool. Because sync messages are what I would have needed instead.

@jsumners we are missing a sync option to in this module, right now we are only logging asynchronously. Then it's docs.

It does make sense.

Meanwhile, as I don't want to replace the whole logging thing in my app, I can probably use pino directly with my custom formatter.