cabana/src/logging/CloudLog.js

54 lines
981 B
JavaScript
Raw Normal View History

import LogEntries from './LogEntries';
import { LOGENTRIES_TOKEN } from '../config';
class CloudLog {
2017-12-12 19:27:20 -07:00
constructor() {
LogEntries.init({
token: LOGENTRIES_TOKEN,
no_format: true,
catchall: false
});
this.context = {};
}
bind(obj) {
this.context.update(obj);
}
emit(message, level = 'log') {
if (typeof global.__JEST__ !== 'undefined') {
2017-12-12 19:27:20 -07:00
// Don't log in testing environment
return;
}
2017-12-12 19:27:20 -07:00
const entry = {
ctx: this.context,
created: new Date().getTime() / 1000,
msg: message,
src: 'JSCloudLog'
2017-12-12 19:27:20 -07:00
};
if (level === 'log') {
2017-12-12 19:27:20 -07:00
LogEntries.log(entry);
} else if (level === 'warn') {
2017-12-12 19:27:20 -07:00
LogEntries.warn(entry);
} else if (level === 'error') {
2017-12-12 19:27:20 -07:00
LogEntries.error(entry);
}
2017-12-12 19:27:20 -07:00
}
2017-12-12 19:27:20 -07:00
log(message) {
this.emit(message);
}
2017-12-12 19:27:20 -07:00
warn(message) {
this.emit(message, 'warn');
2017-12-12 19:27:20 -07:00
}
2017-12-12 19:27:20 -07:00
error(message) {
this.emit(message, 'error');
2017-12-12 19:27:20 -07:00
}
}
2017-12-12 19:27:20 -07:00
export default new CloudLog();