sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

Support `pino` for Sentry Structured Logs

Open AbhiPrasad opened this issue 7 months ago β€’ 2 comments

Sentry now supports Logs!

We should add a pinoIntegration that will forward logs from pino to sentry.

This needs to live in @sentry/core, and work for all SDKs.

AbhiPrasad avatar Apr 01 '25 22:04 AbhiPrasad

Workaround: https://gist.github.com/aldy505/2d0b6ac49a888924b2252ef356e7ff53

ref: https://github.com/getsentry/sentry/discussions/86804#discussioncomment-12694742

AbhiPrasad avatar Apr 02 '25 07:04 AbhiPrasad

Another workaround from discord: https://discord.com/channels/621778831602221064/1378726896384217158/1378726896384217158

{
              level: "debug",
              stream: {
                write: async log => {
                  if (env.isTest) {
                    return;
                  }

                  const cleanedLog = log.replace(/\\n/g, " ").replace(/\s+/g, " ").trim(); // Clean up log format
                  const cleanedLogJson = JSON.parse(cleanedLog); // Parse cleaned log to JSON
                  Sentry.addBreadcrumb({
                    type: cleanedLogJson.type == "error" ? "error" : "default",
                    category: cleanedLogJson.type,
                    level: cleanedLogJson.level,
                    message: cleanedLogJson.message || "Log message",
                    data: cleanedLogJson, // Parse cleaned log for Sentry
                  });

                  Sentry.setUser({
                    id: this.userId ?? undefined,
                  });

                  switch (cleanedLogJson.level) {
                    case "trace":
                      Sentry.logger.trace(cleanedLogJson.message, cleanedLogJson);
                      break;
                    case "debug":
                      Sentry.logger.debug(cleanedLogJson.message, cleanedLogJson);
                      break;
                    case "info":
                      Sentry.logger.info(cleanedLogJson.message, cleanedLogJson);
                      break;
                    case "warn":
                      Sentry.logger.warn(cleanedLogJson.message, cleanedLogJson);
                      break;
                    case "error":
                      Sentry.logger.error(cleanedLogJson.message, cleanedLogJson);
                      break;
                    case "fatal":
                      Sentry.logger.fatal(cleanedLogJson.message, cleanedLogJson);
                      break;
                    default:
                      Sentry.logger.info(cleanedLogJson.message, cleanedLogJson);
                      break;
          }
      },
   }, 
}

AbhiPrasad avatar Jun 02 '25 17:06 AbhiPrasad

Hey, this is cool! Will this allow for pino error logs to be picked up by Sentry as errors?

apetta avatar Jun 25 '25 15:06 apetta

@apetta yup! this is being tracked with https://github.com/getsentry/sentry-javascript/issues/16623

Our primary focus is support logs, but we'll get it working for errors right after :)

AbhiPrasad avatar Jun 25 '25 15:06 AbhiPrasad

@apetta yup! this is being tracked with #16623

Our primary focus is support logs, but we'll get it working for errors right after :)

Looking forward to that!

Slightly unrelated, but in case it helps anyone, here's how I'm using Pino to send errors to Sentry on AWS Node.js Lambdas (as it does not capture console errors by default):

https://github.com/getsentry/sentry-javascript/issues/16740#issuecomment-3005668651

apetta avatar Jun 25 '25 18:06 apetta

Any ETA on this integration @AbhiPrasad ?

hexablob avatar Jul 04 '25 09:07 hexablob

Only major thing left is https://github.com/getsentry/sentry-javascript/issues/16723, aiming to ship by the end of the week!

AbhiPrasad avatar Jul 07 '25 15:07 AbhiPrasad

Hey @AbhiPrasad , just curious if you've got any updates on this? Thanks again!

micahjon avatar Jul 24 '25 16:07 micahjon

hey apologies for the not getting this resolved! Was blocked by some other higher priority items and currently traveling this week.

Have some other stuff to finish up, but then will get back to this! I appreciate the excitement, thanks for your patience everyone.

AbhiPrasad avatar Jul 25 '25 08:07 AbhiPrasad

Look forward to this intergration coming in place? Any ETA now?

Any way I can bypass this with some push to console log and use the console log integration? Since now using pino and pino console logs, it is not pickd up unfortunately.

vongohren avatar Aug 22 '25 10:08 vongohren

FWIW, I realized that even though @sentry/pino-transport is not yet released, its code can be found in this PR: https://github.com/getsentry/sentry-javascript/pull/16667

I was able to adapt it and get it working alongside my existing pino-pretty transport. Here's all the code in a Gist: https://gist.github.com/micahjon/9c7aeaa9246cd6f1095214d16428ab57

Nice thing is there's no wrapper required--you can just add Sentry as a transport. That being said, I've run into some flakiness with multiple transports. Once in a while my API will start up (bun run api.ts) and neither transport will work (both will fail silently, outputting zero logs). If I restart a time or two I eventually get logs and then everything works perfectly, so the nondeterminism appears to be just during Pino transport startup. Anyway, maybe a weird Bun issue or something. Just wanted to flag it in case it was somehow related to the Sentry transport (or pino-abstract-transport which it depends on).

micahjon avatar Aug 24 '25 04:08 micahjon

@AbhiPrasad also curious if there is any ETA for the release

elchead avatar Aug 29 '25 16:08 elchead

Hey folks, apologies with the delay. I was busy with the rest of the logs launch (now out of beta πŸŽ‰) hence I couldn't give my resources here.

I'm going to ask someone on my team to help take this over so we can bring it over to the finish line (PRs are also welcome if folks want to finish up the WIP implementation in the repo!)

AbhiPrasad avatar Aug 29 '25 16:08 AbhiPrasad

faster plz


Image
Image

lforst avatar Aug 30 '25 10:08 lforst

Since @sentry/pino-transport hasn't been published to npm yet, you can install it directly from the GitHub release assets:

{
  "dependencies": {
    "@sentry/pino-transport": "https://github.com/getsentry/sentry-javascript/releases/download/9.33.0/sentry-pino-transport-9.33.0.tgz",
  }
}

Make sure to replace 9.33.0 with the version you need.

neoantox avatar Sep 04 '25 13:09 neoantox

Since @sentry/pino-transport hasn't been published to npm yet, you can install it directly from the GitHub release assets:

{ "dependencies": { "@sentry/pino-transport": "https://github.com/getsentry/sentry-javascript/releases/download/9.33.0/sentry-pino-transport-9.33.0.tgz", } } Make sure to replace 9.33.0 with the version you need.

Hello, is there a way to use the GitHub release assets while avoiding to get a short live jwt in the lockfiles ? Also is there any target date on the release to npm ?

erwan-joly avatar Sep 06 '25 13:09 erwan-joly

@erwan-joly which package manager are you using? I tested with pnpm 10, npm 10, and yarn 1, and in all cases the lockfile still contains the original URL without any JWT tokens

Image

neoantox avatar Sep 06 '25 20:09 neoantox

@erwan-joly which package manager are you using? I tested with pnpm 10, npm 10, and yarn 1, and in all cases the lockfile still contains the original URL without any JWT tokens

Image

Seems like it was an issue with my pnpm, uninstalled reinstalled and it's working fine without jwt now

erwan-joly avatar Sep 06 '25 23:09 erwan-joly

Can't wait to use it

Image

kamilogorek avatar Sep 09 '25 12:09 kamilogorek

Hey folks! πŸ‘‹

Just wanted to check on the status of this β€”Β is it supported yet?

Thanks 😊

chonknick avatar Sep 25 '25 00:09 chonknick

Hi @chonknick, we have a PR in the works, it just needs some more polish.You can follow progress on this issue.

andreiborza avatar Sep 25 '25 07:09 andreiborza

@andreiborza Is this ready for use with nestjs? I see its in the release notes for latest. Is there any guide on how to set it up?

rtman avatar Oct 08 '25 19:10 rtman

This is now released with 10.18.0! https://docs.sentry.io/platforms/javascript/guides/node/logs/#pino-integration

Set up is super easy, just add the integration (and make sure you enable logs)!

Sentry.init({
  enableLogs: true,
  integrations: [Sentry.pinoIntegration()],
});

You can also configure what exact levels create logs

Sentry.init({
  enableLogs: true,
  integrations: [
    // only have info, warn, and error levels create logs
    Sentry.pinoIntegration({ log: { levels: ["info", "warn", "error"] } }),
  ],
});

Thanks for your patience everyone! We took some time to make this as 0 config/seamless as possible (while making everything trace connected).

AbhiPrasad avatar Oct 08 '25 22:10 AbhiPrasad

@AbhiPrasad any idea why my logs are not coming in as logs but as issues? Is this built to work with nestjs-pino?

  Sentry.init({
    dsn: <myDsn>,
    integrations: [
      nodeProfilingIntegration(),
      Sentry.consoleLoggingIntegration({ levels: ['log', 'warn', 'error'] }),
      Sentry.pinoIntegration({ log: { levels: ['info', 'warn', 'error'] } }),
    ],
    environment: process.env.ENV,
    // Send structured logs to Sentry
    enableLogs: true,
    // Tracing
    tracesSampleRate: 1.0, //  Capture 100% of the transactions
    // Set sampling rate for profiling - this is evaluated only once per SDK.init call
    profileSessionSampleRate: 1.0,
    // Trace lifecycle automatically enables profiling during active traces
    profileLifecycle: 'trace',
    // Setting this option to true will send default PII data to Sentry.
    // For example, automatic IP address collection on events
    sendDefaultPii: true,
  });

rtman avatar Oct 08 '25 23:10 rtman

@rtman mind opening a new issue with details about your app? We can take a look! nestjs-pino uses pino under the hood, so the integration should work here.

AbhiPrasad avatar Oct 08 '25 23:10 AbhiPrasad

@AbhiPrasad nvm I think it was a version mismatch on pino-http. Seems to be working now.

rtman avatar Oct 09 '25 17:10 rtman

I'm facing an issue where logs aren't being sent to Sentry. Using Sentry.logger.info works, but the integration fails. I am using Fastify v4.29 (which uses Pino 9.7.0), and newest SDK 10.18

Sample of Sentry.init:

Sentry.init({
    dsn: opts.dsn,
    environment: config.cloudEnv,

    // Add Tracing by setting tracesSampleRate
    tracesSampleRate: 1.0,

    // Set sampling rate for profiling
    // This is relative to tracesSampleRate
    profilesSampleRate: 1.0,

    // Add request headers and IP to the Sentry event
    sendDefaultPii: true,

    // Logs
    enableLogs: true,

    // Add integrations
    integrations: [
      nodeProfilingIntegration(),
      Sentry.knexIntegration(),
      Sentry.pinoIntegration(),
    ],
  });

And this is my main.js

const server = Fastify({
    logger: true,
  });

  Sentry.setupFastifyErrorHandler(server);

Any ideas on what might be causing this? Is there a debug flag to get more insight into the library's behavior?

mrljsh avatar Oct 10 '25 09:10 mrljsh

@mrljsh you are not alone. I'm also not having any luck with the pino integration. Sentry.logger.error seems to mostly work, although I'm not sure I'm actually seeing all logs in Sentry.

I'm using @sentry/nextjs v10.19.0 with nextjs v15.5.4 and pino v9.5.0.

tobalsgithub avatar Oct 10 '25 15:10 tobalsgithub

I went ahead and created https://github.com/getsentry/sentry-javascript/issues/17910 to track this as a separate issue.

tobalsgithub avatar Oct 10 '25 15:10 tobalsgithub

Should this work with pino-https package (pino for express), we're using v9 of it, and we cannot see any http log apart from the explicit use of Sentry.info ?

jalbertsr avatar Oct 10 '25 18:10 jalbertsr