Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anyway to push javascript console logs to sentry

Tags:

reactjs

sentry

I am using the console.error() console.warn() in man places in my app to track failing code. Is there anyway to automatically log these to Sentry. https://sentry.io.

It is a react app, and it seems the componentDidCatch() method they suggest only catches exceptions.

like image 333
Robert Lemiesz Avatar asked Sep 07 '25 09:09

Robert Lemiesz


1 Answers

In case this helps anyone, use Sentry's CaptureConsole integration for this: Sentry.Integrations.CaptureConsole

Official Documentation: https://docs.sentry.io/platforms/javascript/configuration/integrations/plugin/#captureconsole

Refer to this post: How to report console.error with Sentry?

Example Code:

import { CaptureConsole as CaptureConsoleIntegration } from "@sentry/integrations";

Sentry.init({
  dsn: 'https://your-sentry-server-dsn',
  debug: true, // If `true`, Sentry will try to print out useful debugging information if something goes wrong with sending the event. Set it to `false` in production
  integrations: [new CaptureConsoleIntegration(
    {
      // array of methods that should be captured
      // defaults to ['log', 'info', 'warn', 'error', 'debug', 'assert']
      levels: ['log', 'info', 'warn', 'error', 'debug', 'assert']
    }
  )]
});
like image 168
limco Avatar answered Sep 09 '25 03:09

limco