@toruslabs/loglevel-sentry
Version:
A package to allow logging and monitoring with sentry
1 lines • 13.2 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.LoglevelSentry=t():e.LoglevelSentry=t()}(self,(()=>(()=>{var e={65:function(e,t,n){var r,o;!function(){"use strict";r=function(){var e=function(){},t="undefined",n=typeof window!==t&&typeof window.navigator!==t&&/Trident\/|MSIE /.test(window.navigator.userAgent),r=["trace","debug","info","warn","error"],o={},i=null;function s(e,t){var n=e[t];if("function"==typeof n.bind)return n.bind(e);try{return Function.prototype.bind.call(n,e)}catch(t){return function(){return Function.prototype.apply.apply(n,[e,arguments])}}}function c(){console.log&&(console.log.apply?console.log.apply(console,arguments):Function.prototype.apply.apply(console.log,[console,arguments])),console.trace&&console.trace()}function a(){for(var n=this.getLevel(),o=0;o<r.length;o++){var i=r[o];this[i]=o<n?e:this.methodFactory(i,n,this.name)}if(this.log=this.debug,typeof console===t&&n<this.levels.SILENT)return"No console available for logging"}function l(e){return function(){typeof console!==t&&(a.call(this),this[e].apply(this,arguments))}}function u(r,o,i){return function(r){return"debug"===r&&(r="log"),typeof console!==t&&("trace"===r&&n?c:void 0!==console[r]?s(console,r):void 0!==console.log?s(console,"log"):e)}(r)||l.apply(this,arguments)}function f(e,n){var s,c,l,f=this,p="loglevel";function y(){var e;if(typeof window!==t&&p){try{e=window.localStorage[p]}catch(e){}if(typeof e===t)try{var n=window.document.cookie,r=encodeURIComponent(p),o=n.indexOf(r+"=");-1!==o&&(e=/^([^;]+)/.exec(n.slice(o+r.length+1))[1])}catch(e){}return void 0===f.levels[e]&&(e=void 0),e}}function d(e){var t=e;if("string"==typeof t&&void 0!==f.levels[t.toUpperCase()]&&(t=f.levels[t.toUpperCase()]),"number"==typeof t&&t>=0&&t<=f.levels.SILENT)return t;throw new TypeError("log.setLevel() called with invalid level: "+e)}"string"==typeof e?p+=":"+e:"symbol"==typeof e&&(p=void 0),f.name=e,f.levels={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,SILENT:5},f.methodFactory=n||u,f.getLevel=function(){return null!=l?l:null!=c?c:s},f.setLevel=function(e,n){return l=d(e),!1!==n&&function(e){var n=(r[e]||"silent").toUpperCase();if(typeof window!==t&&p){try{return void(window.localStorage[p]=n)}catch(e){}try{window.document.cookie=encodeURIComponent(p)+"="+n+";"}catch(e){}}}(l),a.call(f)},f.setDefaultLevel=function(e){c=d(e),y()||f.setLevel(e,!1)},f.resetLevel=function(){l=null,function(){if(typeof window!==t&&p){try{window.localStorage.removeItem(p)}catch(e){}try{window.document.cookie=encodeURIComponent(p)+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"}catch(e){}}}(),a.call(f)},f.enableAll=function(e){f.setLevel(f.levels.TRACE,e)},f.disableAll=function(e){f.setLevel(f.levels.SILENT,e)},f.rebuild=function(){if(i!==f&&(s=d(i.getLevel())),a.call(f),i===f)for(var e in o)o[e].rebuild()},s=d(i?i.getLevel():"WARN");var g=y();null!=g&&(l=d(g)),a.call(f)}(i=new f).getLogger=function(e){if("symbol"!=typeof e&&"string"!=typeof e||""===e)throw new TypeError("You must supply a name when creating a logger.");var t=o[e];return t||(t=o[e]=new f(e,i.methodFactory)),t};var p=typeof window!==t?window.log:void 0;return i.noConflict=function(){return typeof window!==t&&window.log===i&&(window.log=p),i},i.getLoggers=function(){return o},i.default=i,i},void 0===(o=r.call(t,n,t,e))||(e.exports=o)}()}},t={};function n(r){var o=t[r];if(void 0!==o)return o.exports;var i=t[r]={exports:{}};return e[r].call(i.exports,i,i.exports,n),i.exports}n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var r={};return(()=>{"use strict";n.r(r),n.d(r,{LoglevelSentry:()=>h,createLogger:()=>w,redactBreadcrumbData:()=>x,redactEventData:()=>O});var e=n(65),t=n.n(e);function o(e){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},o(e)}function i(e,t,n){return(t=function(e){var t=function(e){if("object"!=o(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var n=t.call(e,"string");if("object"!=o(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==o(t)?t:t+""}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function s(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function c(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?s(Object(n),!0).forEach((function(t){i(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):s(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}const a=Object.prototype.toString;function l(e,t){return a.call(e)===`[object ${t}]`}function u(e,t){try{return e instanceof t}catch(e){return!1}}const f=globalThis,p=80;function y(e,t){const n=e,r=[];if(!n?.tagName)return"";if(f.HTMLElement&&n instanceof HTMLElement&&n.dataset){if(n.dataset.sentryComponent)return n.dataset.sentryComponent;if(n.dataset.sentryElement)return n.dataset.sentryElement}r.push(n.tagName.toLowerCase());const o=t?.length?t.filter((e=>n.getAttribute(e))).map((e=>[e,n.getAttribute(e)])):null;if(o?.length)o.forEach((e=>{r.push(`[${e[0]}="${e[1]}"]`)}));else{n.id&&r.push(`#${n.id}`);const e=n.className;if(e&&l(e,"String")){const t=e.split(/\s+/);for(const e of t)r.push(`.${e}`)}}const i=["aria-label","type","name","title","alt"];for(const e of i){const t=n.getAttribute(e);t&&r.push(`[${e}="${t}"]`)}return r.join("")}function d(e){try{return"undefined"!=typeof Element&&u(e,Element)?function(e,t={}){if(!e)return"<unknown>";try{let n=e;const r=5,o=[];let i=0,s=0;const c=" > ",a=c.length;let l;const u=Array.isArray(t)?t:t.keyAttrs,f=!Array.isArray(t)&&t.maxStringLength||p;for(;n&&i++<r&&(l=y(n,u),!("html"===l||i>1&&s+o.length*a+l.length>=f));)o.push(l),s+=l.length,n=n.parentNode;return o.reverse().join(c)}catch(e){return"<unknown>"}}(e):Object.prototype.toString.call(e)}catch(e){return"<unknown>"}}function g(e){if("object"==typeof e&&null!==e){const t={};for(const n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}return{}}const v="<anonymous>";function m(e,t=100,n=1/0){try{return b("",e,t,n)}catch(e){return{ERROR:`**non-serializable** (${e})`}}}function b(e,t,n=1/0,r=1/0,o=function(){const e=new WeakSet;return[function(t){return!!e.has(t)||(e.add(t),!1)},function(t){e.delete(t)}]}()){const[i,s]=o;if(null==t||["boolean","string"].includes(typeof t)||"number"==typeof t&&Number.isFinite(t))return t;const c=function(e,t){try{if("domain"===e&&t&&"object"==typeof t&&t._events)return"[Domain]";if("domainEmitter"===e)return"[DomainEmitter]";if("undefined"!=typeof global&&t===global)return"[Global]";if("undefined"!=typeof window&&t===window)return"[Window]";if("undefined"!=typeof document&&t===document)return"[Document]";if("object"==typeof(n=t)&&null!==n&&(n.__isVue||n._isVue))return"[VueViewModel]";if(function(e){return function(e){return l(e,"Object")}(e)&&"nativeEvent"in e&&"preventDefault"in e&&"stopPropagation"in e}(t))return"[SyntheticEvent]";if("number"==typeof t&&!Number.isFinite(t))return`[${t}]`;if("function"==typeof t)return`[Function: ${function(e){try{return e&&"function"==typeof e&&e.name||v}catch(e){return v}}(t)}]`;if("symbol"==typeof t)return`[${String(t)}]`;if("bigint"==typeof t)return`[BigInt: ${String(t)}]`;const r=function(e){const t=Object.getPrototypeOf(e);return t?.constructor?t.constructor.name:"null prototype"}(t);return/^HTML(\w*)Element$/.test(r)?`[HTMLElement: ${r}]`:`[object ${r}]`}catch(e){return`**non-serializable** (${e})`}var n}(e,t);if(!c.startsWith("[object "))return c;if(t.__sentry_skip_normalization__)return t;const f="number"==typeof t.__sentry_override_normalization_depth__?t.__sentry_override_normalization_depth__:n;if(0===f)return c.replace("object ","");if(i(t))return"[Circular ~]";const p=t;if(p&&"function"==typeof p.toJSON)try{return b("",p.toJSON(),f-1,r,o)}catch(e){}const y=Array.isArray(t)?[]:{};let m=0;const h=function(e){if(function(e){switch(a.call(e)){case"[object Error]":case"[object Exception]":case"[object DOMException]":case"[object WebAssembly.Exception]":return!0;default:return u(e,Error)}}(e))return{message:e.message,name:e.name,stack:e.stack,...g(e)};if("undefined"!=typeof Event&&u(e,Event)){const t={type:e.type,target:d(e.target),currentTarget:d(e.currentTarget),...g(e)};return"undefined"!=typeof CustomEvent&&u(e,CustomEvent)&&(t.detail=e.detail),t}return e}(t);for(const e in h){if(!Object.prototype.hasOwnProperty.call(h,e))continue;if(m>=r){y[e]="[MaxProperties ~]";break}const t=h[e];y[e]=b(e,t,f-1,r,o),m++}return s(t),y}class h{constructor(e){i(this,"sentry",void 0),i(this,"category",void 0),this.sentry=e,this.category="loglevel-sentry"}static async translateError(e){const t=e.findIndex((e=>e instanceof Error)),n=e.findIndex((e=>"string"==typeof e)),r=e.findIndex((e=>e&&"object"==typeof e&&"status"in e&&"type"in e)),o=e.findIndex((e=>e&&"object"==typeof e&&"isAxiosError"in e&&!0===e.isAxiosError));let i;if(-1!==r){var s;const t=e[r],n=null===(s=t.headers)||void 0===s?void 0:s.get("content-type");if(n.includes("application/json")){const e=await t.json();i=new Error((null==e?void 0:e.error)||(null==e?void 0:e.message)||JSON.stringify(e))}else i=n.includes("text/plain")?new Error(await t.text()):new Error(`${t.status} ${t.type.toString()} ${t.statusText}`)}else if(-1!==o){const t=e[o].response;if(t){var c;const e=null===(c=t.headers)||void 0===c?void 0:c["content-type"];if(e.includes("application/json")){const e=t.data,n="error"in e?e.error:"message"in e?e.message:JSON.stringify(e);i=new Error(n)}else i=e.includes("text/plain")?new Error(t.data):new Error(`${t.status} ${t.data.toString()}`)}else i=new Error("Network error")}else i=-1!==t?e.splice(t,1)[0]:-1!==n?new Error(e.splice(n,1)[0]):new Error("Unknown error");return[i,e]}static translateArgs(e){const t=e.findIndex((e=>"string"==typeof e)),n=-1!==t?e.splice(t,1)[0]:void 0;return n?{message:n,data:{arguments:e}}:{data:{arguments:e}}}static translateLevel(e){switch(e){case"info":return"info";case"warn":return"warning";default:return"debug"}}install(e){const t=e.methodFactory;e.methodFactory=(e,n,r)=>{r&&(this.category=r.toString());const o=t(e,n,r),i="undefined"!=typeof window,s=(...t)=>{var n;const s=null===(n=this.sentry)||void 0===n?void 0:n.getActiveSpan(),{traceId:a,spanId:l}=(null==s?void 0:s.spanContext())||{},u="error"===e&&t.length>=1&&t[0]instanceof Error;if(i){if(u){const e=t[0],n=a||l?`${e.message}: traceId: ${a} - spanId: ${l}`:e.message,r=[new Error(n,{cause:e}),...t.slice(1)];return void(o&&o(...r))}o&&o(...t)}else{let n={timestamp:new Date,level:e.toUpperCase(),logger:r};if((a||l)&&(n=c(c({},n),{},{traceId:a,spanId:l})),u){var f;const e=t[0];n=c(c({},n),{},{message:null!==(f=e.message)&&void 0!==f?f:"",stack:e.stack,extra:t.length>1?t.slice(1):void 0})}else{var p;n=c(c({},n),{},{message:null!==(p=t[0])&&void 0!==p?p:"",extra:t.length>1?t.slice(1):void 0})}o&&o(JSON.stringify(m(n)))}};return"error"===e?async(...e)=>{const{stack:t}=e.find((e=>e instanceof Error))||new Error,[n,r]=await h.translateError(e);n.stack=t,this.error(n,...r),s(n,...r)}:(...t)=>{this.log(h.translateLevel(e),...t),s(...t)}},e.setLevel(e.getLevel())}setEnabled(e){if(this.sentry){const t=this.sentry.getClient().getOptions();t&&(t.enabled=e)}}isEnabled(){if(this.sentry){var e;const t=this.sentry.getClient().getOptions();return null!==(e=null==t?void 0:t.enabled)&&void 0!==e&&e}return!1}log(e,...t){this.sentry&&this.sentry.addBreadcrumb(c(c({},h.translateArgs(t)),{},{category:this.category,level:e,timestamp:Date.now()}))}trace(...e){this.log("debug",...e)}error(e,...t){if(this.sentry){const n={data:{logger:"loglevel-sentry","logger.name":this.category,arguments:t}};this.sentry.captureException(e,n)}}}function w(e,n){const r=t().getLogger(e);return r.enableAll(),new h(n).install(r),r}const E=/(private|key|secret|authorization|address|email|login_hint)+/i;function j(e,t=E){if("object"!=typeof e||null===e)return e;const n=Object.keys(e);for(const r of n){const n=e[r];"string"==typeof n&&t.test(r)?e[r]="***":e[r]=j(n,t)}return e}function O(e,t=E,n=8){return j(m(e,n),t)}function S(e){let t=e;if("object"==typeof e)t=c({},e);else{if(!Array.isArray(e))return e;t=[...e]}for(const n in e)if(Object.prototype.hasOwnProperty.call(e,n)){const r=e[n];if("string"==typeof r&&/^(-0x|0x)?[0-9a-f]*$/i.test(r))t[n]="***";else try{t[n]=S(r)}catch{t[n]="***"}}return t}function x(e,t=8){return e?S(m(e,t)):e}})(),r})()));