sentry-javascript
sentry-javascript copied to clipboard
Support `pino` for Sentry Structured Logs
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.
Workaround: https://gist.github.com/aldy505/2d0b6ac49a888924b2252ef356e7ff53
ref: https://github.com/getsentry/sentry/discussions/86804#discussioncomment-12694742
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;
}
},
},
}
Hey, this is cool! Will this allow for pino error logs to be picked up by Sentry as errors?
@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 :)
@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
Any ETA on this integration @AbhiPrasad ?
Only major thing left is https://github.com/getsentry/sentry-javascript/issues/16723, aiming to ship by the end of the week!
Hey @AbhiPrasad , just curious if you've got any updates on this? Thanks again!
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.
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.
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).
@AbhiPrasad also curious if there is any ETA for the release
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!)
faster plz
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.
Since
@sentry/pino-transporthasn'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.0with 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 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
@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
![]()
Seems like it was an issue with my pnpm, uninstalled reinstalled and it's working fine without jwt now
Can't wait to use it
Hey folks! π
Just wanted to check on the status of this βΒ is it supported yet?
Thanks π
Hi @chonknick, we have a PR in the works, it just needs some more polish.You can follow progress on this issue.
@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?
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 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 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 nvm I think it was a version mismatch on pino-http. Seems to be working now.
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 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.
I went ahead and created https://github.com/getsentry/sentry-javascript/issues/17910 to track this as a separate issue.
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 ?