logtail / logtail-js

Better Stack JavaScript client
https://betterstack.com/logs
ISC License
54 stars 13 forks source link

Issue with pino and logtail when using multiple transports #111

Closed DomiiBunn closed 6 months ago

DomiiBunn commented 7 months ago

Hello everyone,

I've encountered an issue while attempting to utilize both pino-pretty and @logtail/pino transports with the Pino logger in my Node.js application. The problem arises when configuring multiple transports – only the logs sent to pino-pretty are displayed, while nothing is logged to the standard output (stdout).

Steps to Reproduce:

  1. Configure the Pino logger with multiple transports, including pino-pretty and @logtail/pino.
  2. Log messages using the configured logger.
  3. Observe that only the logs sent to logtail are displayed, while nothing appears in the stdout.

Expected Behavior: I expect the logger to log messages both to pino-pretty and logtail for enhanced readability

Actual Behavior: Only logs sent to pino-pretty are visible, while nothing is logged to logtail.

Code Snippets:

Working code (but missing stdout logging):

const Logger = require("pino");

const transport = Logger.transport({
  target: "@logtail/pino",
  options: { sourceToken: process.env.LOGTAIL_SOURCE_TOKEN },
});

const logger = Logger(transport);

Not logging to stdout, logging to pino-pretty:

const Logger = require("pino");

const transport = Logger.transport({
  targets: [
    {
      target: "@logtail/pino",
      options: { sourceToken: process.env.LOGTAIL_SOURCE_TOKEN },
    },
    {
      target: "pino-pretty",
      options: {
        colorize: true,
        translateTime: true,
        ignore: "pid,hostname",
      },
    }
  ],
});

const logger = Logger(transport);

Additional Information: I'm unsure whether this issue is due to a bug in the configuration or if I'm missing something in my setup. Any assistance or guidance on resolving this issue would be greatly appreciated. Thank you!

curusarn commented 6 months ago

Hi @DomiiBunn,

Sorry for the long silence here! 🙏

Could you share what Pino and pino-logtail versions are you using?

Any chance you are using a level formatter with Pino? We've seen issues before when using a level formatter with multiple transports.

Thanks for reaching out!

DomiiBunn commented 6 months ago

I forgot to close this out actually.

I got it to work in the end with some tweeks. Not entreley sure what did it but ended up with

const pino = require("pino");
const { join } = require("path");
const { LOG_LEVEL, LOGTAIL_TOKEN, PWD } = process.env;
const { name, version } = require("../package.json");
const { format } = require("date-fns");

const today = format(new Date(), "yyyy-MM-dd");
const logFile = join(PWD, `/logs/${today}.log`);

const transport = pino.transport({
  targets: [
    {
      target: "pino/file",
      options: { destination: logFile },
    },
    ...(LOGTAIL_TOKEN
      ? [{ target: "@logtail/pino", options: { sourceToken: LOGTAIL_TOKEN } }]
      : []),
    { target: "pino-pretty" },
  ],
});

const logger = pino(
  {
    name,
    level: LOG_LEVEL || "info",
    version,
  },
  transport
);

module.exports = logger;
curusarn commented 6 months ago

I'm glad you got it working in the end.

Thank you for sharing the code!