-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
188 lines (151 loc) · 5.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const SCHEMA_VERSION = 2;
class Loggerhead {
constructor(options = {}) {
this.configure(options);
this.error = this.error.bind(this);
this.logError = this.logError.bind(this);
this._sequenceNumber = 0;
this._instanceId = Math.random().toString(36).slice(2);
}
configure(options = {}) {
if ('logLevel' in options) this.logLevel = options.logLevel || 'info';
if ('endpoint' in options) this.endpoint = options.endpoint;
this.beforeLog = options.beforeLog || this.beforeLog || new Function;
this.afterLog = options.afterLog || this.afterLog || new Function;
this.metadata = this.metadata || {};
const META_FIELDS = ['applicationName', 'applicationVersion', 'email', 'displayName', 'userId', 'tenantId', 'userAgentShort'];
for (const f of META_FIELDS) {
if (f in options) this.metadata[f] = options[f];
}
this.metadata.timezone = options.timezone || Intl.DateTimeFormat().resolvedOptions().timeZone;
this.metadata.platform = options.platform || this.metadata.platform || navigator.platform;
this.metadata.vendor = options.vendor || this.metadata.vendor || navigator.vendor;
this.metadata.userAgent = options.userAgent || this.metadata.userAgent || navigator.userAgent;
this.metadata.details = (typeof options.details == 'object') ? options.details : {};
}
trackClicks() {
document.addEventListener('click', e => {
let el = e.target;
const trackableEl = el.closest('[data-track]');
if (trackableEl) {
var identifier = trackableEl.getAttribute('data-track');
} else {
var identifier = el.tagName + '-' + (el.textContent.replace(/[^\w]+/g, '-').slice(0,36) || el.getAttribute('alt') || el.getAttribute('title'))
}
this.info('click', identifier);
}, true);
}
trackExceptions() {
if (this._exceptionHandlersInitialized) {
console.warn("exception handlers already initialized");
return;
}
window.addEventListener('error', this.logError, true);
window.addEventListener('unhandledrejection', this.logError, true);
this._exceptionHandlersInitialized = true;
}
log(eventName, context, details={}, logLevel) {
if (LOG_LEVELS[this.logLevel] > LOG_LEVELS[logLevel]) return
if (!this.endpoint) throw "we need a configured log endpoint";
if (this._sequenceNumber++ > 10000) throw "too many logs";
const detailsJSON = JSON.stringify(Object.assign({}, this.metadata.details, details));
const payload = Object.assign({}, this.metadata, {
schemaVersion: `%%version_${SCHEMA_VERSION}%%`,
eventName,
context,
details: detailsJSON,
url: window.location.href,
timestamp: Date.now(),
logLevel,
instanceId: this._instanceId,
sequenceNumber: this._sequenceNumber,
uniqueId: this.uid(),
});
this.beforeLog(payload);
const data = this.encodePayload(payload);
this.fetch(`${this.endpoint}?d=${data}`);
this.afterLog(payload)
}
fetch(url) {
const i = new Image();
i.src = url;
}
trace(eventName, context='', details={}) {
this.log(eventName, context, details, 'trace');
}
debug(eventName, context='', details={}) {
this.log(eventName, context, details, 'debug');
}
info(eventName, context='', details={}) {
this.log(eventName, context, details, 'info');
}
warn(eventName, context='', details={}) {
this.log(eventName, context, details, 'warn');
}
error(eventName, context='', details={}) {
this.log(eventName, context, details, 'error');
}
logError(errorEvent) {
const error = errorEvent.error || errorEvent.message || errorEvent.reason || errorEvent;
if (!error) return;
try {
var errorMessage = error.toString();
var stack = error.stack && error.stack
.replace(/@https:\/\/[^\/]+\//g, '@')
.split('\n').slice(0, 12).join('\n');
} catch (e) {
return;
}
if (!errorMessage) errorMessage = "null";
if (!stack) stack = "null";
if (errorMessage.trim() == '[object Event]') {
// skip if we don't have useful information to log
return;
}
this.error('error', errorMessage.trim(), { stack: stack.trim() });
console.error(errorEvent);
}
encodePayload(data) {
const d = FIELDS.map(f => data[f]);
return encodeURIComponent(JSON.stringify(d));
}
decodePayload(payload) {
const values = JSON.parse(decodeURIComponent(payload))
return FIELDS.reduce((a, i) => (a[i] = values.shift(), a), {})
}
uid() {
return Date.now().toString(36) + Math.random().toString(36).slice(2);
}
}
const LOG_LEVELS = {
trace: 10,
debug: 20,
info: 30,
warn: 40,
error: 50,
}
const FIELDS = [
'timestamp',
'eventName',
'context',
'vendor',
'platform',
'userAgent',
'url',
'applicationVersion',
'email',
'tenantId',
'applicationName',
'displayName',
'timezone',
'userId',
'userAgentShort',
'logLevel',
'instanceId',
'sequenceNumber',
'uniqueId',
'details',
'schemaVersion',
];
const loggerhead = new Loggerhead();
export { Loggerhead, loggerhead };