pinojs / pino-http

🌲 high-speed HTTP logger for Node.js
MIT License
539 stars 117 forks source link

`useLevel`/`customLogLevel` won't use levels defined in `customLevels` #224

Closed santicalcagno closed 2 years ago

santicalcagno commented 2 years ago

Using the following example server:

const http = require('http');
const pinoHttp = require('pino-http');

const middleware = pinoHttp({
  customLevels: { request: 31 },
  useLevel: 'request'
});

http
  .createServer((req, res) => {
    middleware(req, res);
    req.log.info('info level');
    req.log.request('request level');
    res.end('hello world');
  })
  .listen(3000);

If I run curl localhost:3000 these are the output logs I get:

{"level":30,"time":1654687524894,"pid":5088,"hostname":"nixos","req":{"id":1,"method":"GET","url":"/","headers":{"host":"localhost:3000","user-agent":"curl/7.83.1","accept":"*/*"},"remoteAddress":"::ffff:127.0.0.1","remotePort":33606},"msg":"info level"}
{"level":31,"time":1654687524895,"pid":5088,"hostname":"nixos","req":{"id":1,"method":"GET","url":"/","headers":{"host":"localhost:3000","user-agent":"curl/7.83.1","accept":"*/*"},"remoteAddress":"::ffff:127.0.0.1","remotePort":33606},"msg":"request level"}
{"level":30,"time":1654687524900,"pid":5088,"hostname":"nixos","req":{"id":1,"method":"GET","url":"/","headers":{"host":"localhost:3000","user-agent":"curl/7.83.1","accept":"*/*"},"remoteAddress":"::ffff:127.0.0.1","remotePort":33606},"res":{"statusCode":200,"headers":{}},"responseTime":6,"msg":"request completed"}

I was expecting the last line to have a level of 31. The same happens if customLogLevel: () => 'request' is defined instead of useLevel, and if an instance that's been reused has the level instead.

Yesterday I was able to fix this locally, but I wanted to open an issue before working on a PR in case I'm missing something and this is a conscious design decision. Thanks for the library!

mcollina commented 2 years ago

Good spot, go ahead and send a PR over!