@telefonica/sentry-logger v2.0.0
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',
},
});
- This initializes the official Sentry SDKs
- The SDK listens to all unexpected errors and submits them to Sentry
options:
url (string)
: see your Sentry project details to get this URLrelease (string)
: a string that identifies your release, for example be a changeset or version numbertags (object)
: collection of key-value pairs. You can use these tags to filter issues in Sentrylogger (string)
: logger name. Defaults to"javascript"
sampleRate (float)
: sample rate (1
=everything;0
=none). Defaults to1.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
andinfo
messages are discarded andwarn
anderror
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 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago