|
| 1 | +/* |
| 2 | + * GNU AGPL-3.0 License |
| 3 | + * |
| 4 | + * Copyright (c) 2021 - present core.ai . All rights reserved. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU Affero General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License |
| 14 | + * for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see https://opensource.org/licenses/AGPL-3.0. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +/*globals Bugsnag*/ |
| 22 | + |
| 23 | +import AppConfig from "./loggerConfig.js"; |
| 24 | + |
| 25 | +// logger setup |
| 26 | +function swallowLogs() { |
| 27 | + // Do nothing |
| 28 | +} |
| 29 | +const savedLoggingFn = console.log; |
| 30 | +const savedInfoFn = console.info; |
| 31 | + |
| 32 | +window.setupLogging = function () { |
| 33 | + const urlParams = new URLSearchParams(window.location.search); |
| 34 | + const logToConsoleOverride = urlParams.get('logToConsole'); |
| 35 | + const logToConsolePref = localStorage.getItem("logToConsole"); |
| 36 | + window.testEnvironment = (urlParams.get('testEnvironment') === 'true'); |
| 37 | + if((logToConsoleOverride && logToConsoleOverride.toLowerCase() === 'true') |
| 38 | + || (logToConsolePref && logToConsolePref.toLowerCase() === 'true' && !logToConsoleOverride)){ |
| 39 | + console.log= savedLoggingFn; |
| 40 | + console.info= savedInfoFn; |
| 41 | + window.logToConsolePref = 'true'; |
| 42 | + window.debugMode = true; |
| 43 | + return true; |
| 44 | + } else { |
| 45 | + console.info = console.log = swallowLogs; |
| 46 | + window.logToConsolePref = 'false'; |
| 47 | + window.debugMode = false; |
| 48 | + return false; |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +window.isLoggingEnabled = function (key) { |
| 53 | + let loggingEnabled = localStorage.getItem(key) || "false"; |
| 54 | + return loggingEnabled.toLowerCase() === 'true'; |
| 55 | +}; |
| 56 | + |
| 57 | +window.toggleLoggingKey = function(key) { |
| 58 | + if(window.isLoggingEnabled(key)){ |
| 59 | + localStorage.setItem(key, 'false'); |
| 60 | + } else { |
| 61 | + localStorage.setItem(key, 'true'); |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +window.loggingOptions = { |
| 66 | + LOCAL_STORAGE_KEYS: { |
| 67 | + LOG_LIVE_PREVIEW: "logLivePreview" |
| 68 | + }, |
| 69 | + livePreview: { |
| 70 | + log: function (...args) { |
| 71 | + if(window.loggingOptions.logLivePreview){ |
| 72 | + console.log(...args); |
| 73 | + } |
| 74 | + } |
| 75 | + }, |
| 76 | + healthDataDisabled: false, |
| 77 | + /** |
| 78 | + * By default all uncaught exceptions and promise rejections are sent to logger utility. But in some cases |
| 79 | + * you may want to sent handled errors too if it is critical. use this function to report those |
| 80 | + * @param error |
| 81 | + */ |
| 82 | + reportError: function (error) { |
| 83 | + Bugsnag.notify(error); |
| 84 | + } |
| 85 | +}; |
| 86 | +window.loggingOptions.logLivePreview = window.isLoggingEnabled( |
| 87 | + window.loggingOptions.LOCAL_STORAGE_KEYS.LOG_LIVE_PREVIEW); |
| 88 | + |
| 89 | +window.setupLogging(); |
| 90 | + |
| 91 | +function onError(event) { |
| 92 | + // for mroe info https://docs.bugsnag.com/platforms/javascript/customizing-error-reports |
| 93 | + // change health logger popup string before changing the below line to anything other than "Caught Critical error" |
| 94 | + let reportedStatus = window.loggingOptions.healthDataDisabled? "Not Reported as health data disabled." : "Reported"; |
| 95 | + |
| 96 | + console.error(`Caught Critical error, ${reportedStatus}: `, event); |
| 97 | + if(window.Metrics) { |
| 98 | + window.Metrics.countEvent(window.Metrics.EVENT_TYPE.ERROR, "uncaught", "logger"); |
| 99 | + } |
| 100 | + if(window.loggingOptions.healthDataDisabled){ |
| 101 | + // don't log anything as user disabled health tracking |
| 102 | + return false; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +const isTestWindow = (new window.URLSearchParams(window.location.search || "")).get("testEnvironment"); |
| 107 | + |
| 108 | +if(!isTestWindow) { |
| 109 | + Bugsnag.start({ |
| 110 | + apiKey: 'a899c29d251bfdf30c3222016a2a7ea7', |
| 111 | + appType: window.__TAURI__ ? "tauri" : "browser", |
| 112 | + collectUserIp: false, |
| 113 | + appVersion: AppConfig.version, |
| 114 | + enabledReleaseStages: [ 'development', 'production', 'staging' ], |
| 115 | + releaseStage: AppConfig.config.bugsnagEnv, |
| 116 | + // https://docs.bugsnag.com/platforms/javascript/#logging-breadcrumbs |
| 117 | + // breadcrumbs is disabled as it seems a bit intrusive in Pheonix even-though it might help with debugging. |
| 118 | + enabledBreadcrumbTypes: [], |
| 119 | + // https://docs.bugsnag.com/platforms/javascript/configuration-options/#maxevents |
| 120 | + maxEvents: 10, |
| 121 | + onError |
| 122 | + }); |
| 123 | +} |
0 commit comments