pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.22k stars 874 forks source link

Pino streams do not honor logger level debug #1884

Closed SGudbrandsson closed 9 months ago

SGudbrandsson commented 9 months ago

I'm attempting to use multiple streams in pino, however, I'm unable to get debug messages to be printed in stdout.

Here's an example code, along with the accompanying screenshot

import pino from "pino";
import pretty from "pino-pretty";
import { createWriteStream } from "pino-sentry";

const stream = createWriteStream({
  dsn: process.env.SENTRY_DSN,
  environment: process.env.NODE_ENV || "development",
  level: "error",
});

const streams = [stream];
if (process.env.NODE_ENV !== "production") {
  const pp = pretty({
    colorize: true,
    ignore: "pid,hostname",
    minimumLevel: "debug",
  });

  streams.push(pp);
}

export const logger = pino({ level: "debug" }, pino.multistream(streams));
console.log(logger.isLevelEnabled("debug"));
logger.debug("Logger initialized");
console.log("Logger finished");

image

If I remove streams and use only the pino({ level: "debug" }); then I get debug logs.

Do you have any idea what might be wrong?

mcollina commented 9 months ago

I think this should work:

import pino from "pino";
import pretty from "pino-pretty";
import { createWriteStream } from "pino-sentry";

const stream = createWriteStream({
  dsn: process.env.SENTRY_DSN,
  environment: process.env.NODE_ENV || "development",
  level: "error",
});

const streams = [stream];
if (process.env.NODE_ENV !== "production") {
  const pp = pretty({
    colorize: true,
    ignore: "pid,hostname",
    minimumLevel: "debug",
  });

  streams.push({ level: 'debug', stream: pp });
}

export const logger = pino({ level: "debug" }, pino.multistream(streams));
console.log(logger.isLevelEnabled("debug"));
logger.debug("Logger initialized");
console.log("Logger finished");

I know this is verbose, a PR to improve things would be highly welcomed.

SGudbrandsson commented 9 months ago

This worked, thanks!!!

I'll see what I can do with a PR real quick. The biggest win would be a documentation update, so I'll add that at least. Since the initialization flow is the way it is, the only thing I can think of is to have some config builder that would prepare a standard object for initializing streams, transports, etc.

I don't have a clear idea of how to execute that cleanly without a sizeable refactor. I'll add the documenation bit in a PR though.

SGudbrandsson commented 9 months ago

No, looks like this is documented here https://github.com/pinojs/pino/blob/89fc326c7a118032ad1c0df355f72e49e90c5bed/docs/help.md#log-to-different-streams

I just didn't see it

SGudbrandsson commented 9 months ago

I'll see if I can make a PR that simplifies this a bit later.

github-actions[bot] commented 8 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.