accounts-frontend/src/services/logger/logger.js

89 lines
2.3 KiB
JavaScript
Raw Normal View History

import Raven from 'raven-js';
import abbreviate from './abbreviate';
const isTest = process.env.__TEST__; // eslint-disable-line
2017-03-01 11:14:11 +05:30
const isProduction = process.env.__PROD__; // eslint-disable-line
const logger = {
init({sentryCdn}) {
if (sentryCdn) {
Raven.config(sentryCdn, {
logger: 'accounts-js-app',
level: 'info',
environment: process.env.APP_ENV, // eslint-disable-line
release: process.env.__VERSION__, // eslint-disable-line
shouldSendCallback: () => !isTest,
dataCallback: (data) => {
if (!data.level) {
// log unhandled errors as info
data.level = 'info';
}
return data;
2017-03-01 11:14:11 +05:30
},
whitelistUrls: isProduction ? [
/ely\.by/
] : []
}).install();
2017-01-02 19:07:02 +05:30
window.addEventListener('unhandledrejection', (event) => {
const error = event.reason || {};
let message = error.message || error;
if (typeof message === 'string') {
message = `: ${message}`;
} else {
message = '';
}
2017-01-02 19:07:02 +05:30
logger.info(`Unhandled rejection${message}`, {
error
});
});
}
},
setUser(user) {
Raven.setUserContext({
username: user.username,
email: user.email,
id: user.id
});
}
};
[
// 'fatal',
'error',
'warning',
'info',
'debug'
].forEach((level) => {
const method = level === 'warning' ? 'warn' : level;
logger[method] = (message, context) => {
if (isTest) {
return;
}
2017-03-02 11:28:33 +05:30
if (typeof context !== 'object') {
// it would better to always have an object here
context = {
message: context
};
}
context = abbreviate(context); // prepare data for JSON.stringify
2017-01-02 19:07:02 +05:30
console[method](message, context); // eslint-disable-line
Raven.captureException(message, {
level,
extra: context
});
};
});
export default logger;