2.0.0 • Published 2 years ago

@telefonica/sentry-logger v2.0.0

Weekly downloads
-
License
UNLICENSED
Repository
github
Last release
2 years ago

logger

Send traces and messages to Sentry

Install

yarn add '@telefonica/sentry-logger'

Importing

import {error, warn, logException} from '@telefonica/sentry-logger';

Usage

During development all messages are logged to console. To send messages to Sentry, NODE_ENV must be set to production.

initErrorLog({url, release, tags, logger, sampleRate})

Initializes the logger, this must be called once (and only once) as soon as possible in your program.

initErrorLog({
  url: 'https://path/to/sentry/dsn',
  release: '1.2.3',
  tags: {
    foo: 'bar',
  },
});

options:

  • url (string): see your Sentry project details to get this URL
  • release (string): a string that identifies your release, for example be a changeset or version number
  • tags (object): collection of key-value pairs. You can use these tags to filter issues in Sentry
  • logger (string): logger name. Defaults to "javascript"
  • sampleRate (float): sample rate (1=everything; 0=none). Defaults to 1.0

log, debug, info, warn, error

Message loggers. Use them when you simply want to log a message.

debug('no one cares about this');
info('some useful information');
warn('something weird happened');
error('fix this NOW!');

These functions are variadic (similar to console.log), all parameters will be serialized:

warn('Api call', name, 'has failed', count, 'times');
  • During development, those messages are logged to the console.
  • In production builds, debug and info messages are discarded and warn and error are sent to Sentry.
  • :warning: Messages don't send stacktraces so be descriptive! If you need a stacktrace, log an exception.

logException(error, extra?)

Logs an exception. Use this function when you want to have stacktrace information. When throwing, always throw Error instances:

const dirtyStuff = () => {
  throw Error('ouch!'); // always throw Errors!
};

try {
  dirtyStuff();
} catch (e) {
  logException(e, {data: 'some data'});
}

:warning: In development, logException logs to the console as an error.

logBreadcrumb

Logs a breadcrumb. Breadcrumbs are records of server and application lifecycle events that can be helpful in understanding the state of the application leading up to a crash.

logBreadcrumb({category: 'Action', message: 'This is a breadcrumb', data: {foo: 123}});

:warning: many actions are automatically logged as breadcrumbs like xhr requests, some user generated dom events, errors, warnings...

updateErrorLogUserContext(context)

Updates the user context, you would usually call this function after acquiring/changing session

updateErrorLogUserContext({
  id: userId,
  email: 'pedro@mail.com',
  msisdn: '34654834455',
  // etc
});

If the user logs out, you can clear this context by calling it without parameters:

updateErrorLogUserContext(); // clears user context
2.1.0-beta1

2 years ago

2.1.0-beta2

2 years ago

2.0.0

2 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.0-beta4

3 years ago

1.0.0-beta3

3 years ago

1.0.0-beta2

3 years ago

1.0.0-beta1

3 years ago