Skip to content

[Feature request] Add ability to configure log level for instrumentConsole #3946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
romap0 opened this issue Aug 31, 2021 · 3 comments · Fixed by #5249
Closed

[Feature request] Add ability to configure log level for instrumentConsole #3946

romap0 opened this issue Aug 31, 2021 · 3 comments · Fixed by #5249
Labels
Meta: Help Wanted Package: browser Issues related to the Sentry Browser SDK Type: Improvement

Comments

@romap0
Copy link

romap0 commented Aug 31, 2021

It would be useful to have the ability to pass console log methods which will be added to breadcrumbs.

For example:

Sentry.init({
  breadcrumbConsoleLevels: ['trace', 'debug', 'info'],
});
@AbhiPrasad
Copy link
Member

You can use the beforeBreadcrumb hook to customize what breadcrumbs show up. https://docs.sentry.io/platforms/javascript/enriching-events/breadcrumbs/#customize-breadcrumbs

Sentry.init({
  // ...

  beforeBreadcrumb(breadcrumb, hint) {
    if (breadcrumb.category === "console") {
        // add logic to control what breadcrumbs are here
    }

    return breadcrumb;
  },
});

See what it looks like here:

private _consoleBreadcrumb(handlerData: { [key: string]: any }): void {
const breadcrumb = {
category: 'console',
data: {
arguments: handlerData.args,
logger: 'console',
},
level: Severity.fromString(handlerData.level),
message: safeJoin(handlerData.args, ' '),
};

@kamilogorek Do you think we should expand the API to add this? Not sure if it's worth the bundle size increase.

@AbhiPrasad AbhiPrasad added Package: browser Issues related to the Sentry Browser SDK Type: Discussion labels Aug 31, 2021
@romap0
Copy link
Author

romap0 commented Aug 31, 2021

@AbhiPrasad Thanks for your reply.

beforeBreadcrumb hook can be used to filter extra console methods, but with it I can`t add a trace level.

Now as a workaround I use this after Sentry initialization:

fill(global.console, 'trace', (originalConsoleLevel) => (message, ...args) => {
  Sentry.addBreadcrumb({
    category: 'console',
    level: Sentry.Severity.Debug, // <-- there is not a trace severity, so debug is used
    message,
    data: { level: 'trace', args },
  });

  if (originalConsoleLevel) {
    Function.prototype.apply.call(originalConsoleLevel, global.console, [message, ...args]);
  }
});

@kamilogorek
Copy link
Contributor

@romap0 you can add trace here

['debug', 'info', 'warn', 'error', 'log', 'assert'].forEach(function(level: string): void {
and I'll happily merge it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Meta: Help Wanted Package: browser Issues related to the Sentry Browser SDK Type: Improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants