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

134 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-05-02 23:18:50 +05:30
// @flow
import type {User} from 'components/user';
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
2018-05-02 23:18:50 +05:30
class Logger {
init({ sentryCdn }: { sentryCdn: string }) {
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
2018-05-02 23:18:50 +05:30
this.info(`Unhandled rejection${message}`, {
error,
event
2017-01-02 19:07:02 +05:30
});
});
}
2018-05-02 23:18:50 +05:30
}
2018-05-02 23:18:50 +05:30
setUser(user: User) {
Raven.setUserContext({
username: user.username,
email: user.email,
id: user.id
});
}
2018-05-02 23:18:50 +05:30
error(message: string | Error, context: Object) {
log('error', message, context);
}
2017-03-02 11:28:33 +05:30
2018-05-02 23:18:50 +05:30
info(message: string | Error, context: Object) {
log('info', message, context);
}
2018-05-02 23:18:50 +05:30
warn(message: string | Error, context: Object) {
log('warning', message, context);
}
}
function log(
level: 'error' | 'warning' | 'info' | 'debug',
message: string | Error,
context: Object
) {
const method: 'error' | 'warn' | 'info' | 'debug' = level === 'warning' ? 'warn' : level;
if (isTest) {
return;
}
if (typeof context !== 'object') {
// it would better to always have an object here
context = {
message: context
};
}
prepareContext(context).then((context) => {
console[method](message, context); // eslint-disable-line
Raven.captureException(message, {
level,
extra: context,
});
2018-05-02 23:18:50 +05:30
});
}
/**
* prepare data for JSON.stringify
*
* @param {object} context
*
* @return {Promise}
*/
2018-05-02 23:18:50 +05:30
function prepareContext(context: any) {
if (context instanceof Response) {
// TODO: rewrite abbreviate to use promises and recursively find Response
return context.json()
.catch(() => context.text())
.then((body) =>
abbreviate({
type: context.type,
url: context.url,
status: context.status,
statusText: context.statusText,
body
})
);
} else if (context.originalResponse instanceof Response) {
return prepareContext(context.originalResponse)
.then((originalResponse) =>
abbreviate({
...context,
originalResponse
})
);
}
return Promise.resolve(abbreviate(context));
}
2018-05-02 23:18:50 +05:30
export default new Logger();