-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathloggerSetup.js
More file actions
327 lines (308 loc) · 13.4 KB
/
loggerSetup.js
File metadata and controls
327 lines (308 loc) · 13.4 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*globals Bugsnag, AppConfig*/
// window.AppConfig comes from appConfig.js built by gulp scripts at build time
(function(){
// only log public facing urls and desktop apps directly distributed by us. local host dev time
// should not be logged in bugsnag. Don't log any other urls not managed by us.
const loggableURLS = [
"phtauri://localhost", // mac or linux desktop
"https://phtauri.localhost", //windows desktop
"https://phcode.dev", // prod
"https://create.phcode.dev", // extension authoring prod
"https://dev.phcode.dev", // dev url
"https://staging.phcode.dev" // staging url
];
const nativeEnvName = window.__TAURI__ ? "tauri" : (window.__ELECTRON__ ? "electron" : "");
let isBugsnagLoggableURL = false; // to test bugsnag in dev builds, set this to true
for(let loggableURL of loggableURLS){
if(window.location.href.startsWith(loggableURL)){
isBugsnagLoggableURL = true;
break;
}
}
const urlParams = new URLSearchParams(window.location.search || "");
const isBugsnagEnabled = (!window.testEnvironment && isBugsnagLoggableURL && !Phoenix.healthTrackingDisabled);
const MAX_ERR_SENT_RESET_INTERVAL = 60000,
MAX_ERR_SENT_FIRST_MINUTE = 10,
MAX_ERR_ALLOWED_IN_MINUTE = 2;
let firstMinuteElapsed = false, errorsSentThisMinute = 0;
class CustomBugSnagError extends Error {
constructor(message, err){
super(message + ((err && err.message) || ""));
this.name = (err && err.constructor && err.constructor.name) || this.constructor.name;
this.stack= message +" : "+ (err && err.stack) || "stack not available";
}
}
const logger = {
error: console.error,
warn: console.warn,
/**
* By default all uncaught exceptions and promise rejections are sent to logger utility. But in some cases
* you may want to sent handled errors too if it is critical. use this function to report those
* @param {Error} error
* @param {string} [message] optional message
*/
reportError: function (error, message) {
if(isBugsnagEnabled) {
Bugsnag.notify(message?
new CustomBugSnagError(message, error)
:error);
} else {
console.error(message, error, error.nodeStack);
}
},
/**
* By default all uncaught exceptions and promise rejections are sent to logger utility. But in some cases
* you may want to log error without having an error object with you.
*
* @param {string} [message] optional message
*/
reportErrorMessage: function (message) {
if(isBugsnagEnabled) {
Bugsnag.notify(new CustomBugSnagError(message));
} else {
console.error(message);
}
},
/**
* This will help to provide additional context to error reporting. The trail will serve as a series of
* events that happened before an error and will help to backtrack the error.
* @param {string} message
*/
leaveTrail: function (message) {
console.log("[Trail] : ", message);
if(isBugsnagEnabled) {
Bugsnag.leaveBreadcrumb(message);
}
},
loggingOptions: {
LOCAL_STORAGE_KEYS: {
// change these keys in devEnable.html too
LOG_TO_CONSOLE_KEY: "logToConsole",
LOG_LIVE_PREVIEW: "logLivePreview",
// these need not be dev enable for now
LOG_GIT: "logGitDebugMode"
},
healthDataDisabled: false,
logGit: false,
logLivePreview: false // logLivePreview will be setup below
},
livePreview: {
log: function (...args) {
if(logger.loggingOptions.logLivePreview){
logger.log(...args);
}
}
}
// other API setup below
};
window.logger = logger;
// logger setup
function swallowLogs() {
// Do nothing
}
const savedLoggingFn = console.log;
const savedInfoFn = console.info;
/**
* interceptors for console.log and info
* @returns {boolean}
*/
window.setupLogging = function () {
const logToConsoleOverride = urlParams.get(logger.loggingOptions.LOCAL_STORAGE_KEYS.LOG_TO_CONSOLE_KEY);
const logToConsolePref = localStorage.getItem(logger.loggingOptions.LOCAL_STORAGE_KEYS.LOG_TO_CONSOLE_KEY);
if((logToConsoleOverride && logToConsoleOverride.toLowerCase() === 'true')
|| (logToConsolePref && logToConsolePref.toLowerCase() === 'true' && !logToConsoleOverride)){
console.log= savedLoggingFn;
console.info= savedInfoFn;
logger.log = console.log;
logger.info = console.info;
logger.logToConsolePref = 'true';
window.debugMode = true;
return true;
} else {
console.info = console.log = swallowLogs;
logger.info = logger.log = swallowLogs;
logger.logToConsolePref = 'false';
window.debugMode = false;
return false;
}
};
window.setupLogging();
window.isLoggingEnabled = function (key) {
let loggingEnabled = localStorage.getItem(key) || "false";
return loggingEnabled.toLowerCase() === 'true';
};
window.toggleLoggingKey = function(key) {
if(window.isLoggingEnabled(key)){
localStorage.setItem(key, 'false');
} else {
localStorage.setItem(key, 'true');
}
};
logger.loggingOptions.logLivePreview = window.isLoggingEnabled(
logger.loggingOptions.LOCAL_STORAGE_KEYS.LOG_LIVE_PREVIEW);
logger.loggingOptions.logGit = window.isLoggingEnabled(
logger.loggingOptions.LOCAL_STORAGE_KEYS.LOG_GIT);
function _shouldDiscardError(errors = []) {
if(!window.Phoenix || !window.Phoenix.VFS){
return false;
}
let fileURL, extensionName, userFsURLFound = false,
userExtensionsFolderURL = window.Phoenix.VFS.getVirtualServingURLForPath(window.Phoenix.VFS.getUserExtensionDir()+"/");
// errors with stacks originating from any folder or files from the user file system are not logged for privacy
for(let error of errors){
if(error.stacktrace && error.stacktrace[0]) {
for(let stack of error.stacktrace){
fileURL = stack.file || "";
if(fileURL.startsWith(userExtensionsFolderURL)) {
// an extension installed from extension store has error. we dont log, but raise metric
extensionName = fileURL.replace(userExtensionsFolderURL, "");
extensionName = extensionName.split("/")[0];
let supportStatus = "Y";
if(!Phoenix.isSupportedBrowser){
supportStatus = "N";
}
window.Metrics.countEvent(window.Metrics.EVENT_TYPE.ERROR,
`extn-${supportStatus}-${extensionName}`, error.type);
window.Metrics.countEvent(window.Metrics.EVENT_TYPE.ERROR,
`extn-${supportStatus}-${extensionName}`, error.errorClass);
logger.leaveTrail(
`Extension Error for ${extensionName} of type ${error.type} class ${error.errorClass}`);
return true;
}
if(window.Phoenix.VFS.getPathForVirtualServingURL(fileURL)) {
userFsURLFound = true;
}
}
}
}
if(userFsURLFound) {
return true;
}
return false;
}
function onError(event) {
// for more info https://docs.bugsnag.com/platforms/javascript/customizing-error-reports
try{
let reportedStatus = "Reported";
let shouldReport = true;
if(logger.loggingOptions.healthDataDisabled
|| (firstMinuteElapsed && errorsSentThisMinute > MAX_ERR_ALLOWED_IN_MINUTE)){
// after the first minute which allows up to 10 error reports,
// we restrict error reports to 2 per minute after that.
reportedStatus = "Not Reported as health data disabled or max reports per minute breached.";
shouldReport = false;
} else if(_shouldDiscardError(event.errors)){
reportedStatus = "Not Reported error from user extension or fs.";
shouldReport = false;
}
// change health logger popup string before changing the below to anything other than "Caught Critical error"
console.error(`Caught Critical error, ${reportedStatus}: `, event);
if(window.Metrics) {
let supportStatus = "supportedBrowser";
if(!Phoenix.isSupportedBrowser){
supportStatus = "unsupportedBrowser";
}
window.Metrics.countEvent(window.Metrics.EVENT_TYPE.ERROR, "uncaught", supportStatus);
}
if(shouldReport){
errorsSentThisMinute++;
}
return shouldReport;
} catch (e) {
console.error("exception occurred while reposting error: ", e);
event.addMetadata('onError', 'exception', e.message);
}
}
let context = 'desktop';
if(Phoenix.browser.isTablet) {
context = 'tablet';
} else if(Phoenix.browser.isMobile) {
context = 'mobile';
}
if(Phoenix.isNativeApp) {
context = `${nativeEnvName}-${context}`;
}
if(Phoenix.browser.isMobile || Phoenix.browser.isTablet) {
let device = 'unknownDevice';
if(Phoenix.browser.mobile.isAndroid){
device = 'android';
} else if(Phoenix.browser.mobile.isIos){
device = 'ios';
} else if(Phoenix.browser.mobile.isWindows){
device = 'windows';
}
context = `${context}-${device}`;
}
if(Phoenix.browser.isDeskTop) {
let browser = 'unknownBrowser';
if(Phoenix.browser.desktop.isOperaChromium){
browser = 'operaChrome';
} else if(Phoenix.browser.desktop.isEdgeChromium){
browser = 'edgeChrome';
} else if(Phoenix.browser.desktop.isChrome){
browser = 'googleChrome';
} else if(Phoenix.browser.desktop.isChromeBased){
browser = 'chromeLike';
} else if(Phoenix.browser.desktop.isFirefox){
browser = 'firefox';
} else if(Phoenix.browser.desktop.isOpera){
browser = 'operaLegacy';
} else if(Phoenix.browser.desktop.isSafari){
browser = 'safari';
} else if(Phoenix.browser.desktop.isWebKit){
browser = 'webkit';
}
context = `${context}-${Phoenix.platform}-${browser}`;
}
context = Phoenix.isSupportedBrowser ? `supported-${context}` : `unsupported-${context}`;
Phoenix.supportContextName = context;
console.log("BugSnag context is - ", context);
if(isBugsnagEnabled) {
Bugsnag.start({
apiKey: '94ef94f4daf871ca0f2fc912c6d4764d',
context: context,
appType: nativeEnvName || "browser",
collectUserIp: false,
appVersion: AppConfig.version,
enabledReleaseStages: [ 'development', 'production', 'staging',
'tauri-development', 'tauri-production', 'tauri-staging',
'electron-development', 'electron-production', 'electron-staging'],
releaseStage: window.__IS_NATIVE_SHELL__ ?
`${nativeEnvName}-${AppConfig.config.bugsnagEnv}` : AppConfig.config.bugsnagEnv,
// https://docs.bugsnag.com/platforms/javascript/#logging-breadcrumbs
// breadcrumbs is disabled as it seems a bit intrusive in Pheonix even-though it might help with debugging.
// only manual explicit privacy ready breadcrumbs are allowed
enabledBreadcrumbTypes: ['manual'],
// https://docs.bugsnag.com/platforms/javascript/configuration-options/#maxevents
maxEvents: MAX_ERR_SENT_FIRST_MINUTE,
maxBreadcrumbs: 50,
onError
});
setInterval(()=>{
Bugsnag.resetEventCount();
firstMinuteElapsed = true;
errorsSentThisMinute = 0;
}, MAX_ERR_SENT_RESET_INTERVAL);
} else {
console.warn("Logging to Bugsnag is disabled as current environment is localhost.");
}
}());