UNPKG

5.71 kBJavaScriptView Raw
1"use strict";
2
3function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
5!function (e, t) {
6 "use strict";
7
8 function o() {
9 this.sendErrors = !1, this.endpoint = "", this.interval = 0, this.session = "", this.timestamp = "", this.hostName = "", this.sourceCategory = "", this.name = "", this.onSuccess = !1, this.onError = !1;
10 }
11
12 function s() {
13 return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (e) {
14 var t = 16 * Math.random() | 0,
15 o = "x" === e ? t : 3 & t | 8;
16 return o.toString(16);
17 });
18 }
19
20 function n(t) {
21 e && e.console && "function" == typeof e.console.log && console.log(t);
22 }
23
24 function i(t, o) {
25 if (t.sendErrors = o, t.sendErrors === !0) {
26 var s = e.onerror;
27
28 e.onerror = function (o, n, i, r) {
29 o = "message: " + o + ", url: " + n + ", line_num: " + i + ", col_num: " + r, t.log({
30 error: "BrowserJsException",
31 exception: o
32 }), s && "function" == typeof s && s.apply(e, arguments);
33 };
34 }
35 }
36
37 var r = "sumologic.logger.session",
38 a = r.length + 1,
39 u = [];
40 o.prototype = {
41 reset: function reset() {
42 u = [], this.endpoint = "", this.interval = 0, this.session = "", this.timestamp = "", this.hostName = "", this.sourceCategory = "", this.name = "";
43 },
44 getCurrentLogs: function getCurrentLogs() {
45 return u;
46 },
47 setSession: function setSession(e) {
48 e ? (this.session = e, this.setCookie(this.session)) : this.session || (this.session = this.getCookie(), this.session || (this.session = s(), this.setCookie(this.session)));
49 },
50 config: function config(e) {
51 if (!e || "object" != _typeof(e)) return void n("Sumo Logic Logger requires you to set an endpoint.");
52 if (!e.endpoint || "" === e.endpoint) return void n("Sumo Logic Logger requires you to set an endpoint.");
53
54 if (this.endpoint = e.endpoint, e.interval && (this.interval = e.interval), this.setSession(e.sessionKey ? e.sessionKey : s()), e.sendErrors && i(this, e.sendErrors), e.sourceName && (this.name = e.sourceName), e.sourceCategory && (this.sourceCategory = e.sourceCategory), e.hostName && (this.hostName = e.hostName), e.onSuccess && (this.onSuccess = e.onSuccess), e.onError && (this.onError = e.onError), this.interval > 0) {
55 var t = this;
56 setInterval(function () {
57 t.sendLogs();
58 }, this.interval);
59 }
60 },
61 log: function log(t, o) {
62 if ("" === this.endpoint) return void n("Sumo Logic Logger requires you to set an endpoint before pushing logs.");
63 if (!t) return void n("Sumo Logic Logger requires that you pass a value to log.");
64 o = o || !1;
65
66 var s = t instanceof Array,
67 i = s ? t[0] : t,
68 r = _typeof(i);
69
70 if ("undefined" === r || "string" === r && "" === i) return void n("Sumo Logic Logger requires that you pass a value to log.");
71 if ("object" === r && 0 === Object.keys(t).length) return void n("Sumo Logic Logger requires that you pass a non-empty JSON object to log.");
72 s || (t = [t]);
73 var a = new Date();
74 o && (o.sessionKey && this.setSession(o.sessionKey), o.timestamp && (a = o.timestamp)), this.timestamp = a.toUTCString();
75 var c = this,
76 h = t.map(function (t) {
77 if ("string" == typeof t) return JSON.stringify({
78 msg: t,
79 sessionId: c.session,
80 url: encodeURI(e.location),
81 timestamp: c.timestamp
82 });
83 var o = {
84 sessionId: c.session,
85 url: encodeURI(e.location),
86 timestamp: c.timestamp
87 };
88 return Object.keys(t).forEach(function (e) {
89 o[e] = t[e];
90 }), JSON.stringify(o);
91 });
92 u = u.concat(h), 0 === this.interval && this.sendLogs();
93 },
94 logSent: function logSent() {},
95 sendLogs: function sendLogs() {
96 if (0 !== u.length) {
97 var e;
98
99 try {
100 var t = new XMLHttpRequest();
101 e = u, u = [];
102 var o = this.onError;
103
104 if (t.addEventListener("error", function () {
105 u = e, o && o();
106 }), this.onSuccess) {
107 var s = this.onSuccess;
108 t.addEventListener("load", function () {
109 s();
110 });
111 }
112
113 t.open("POST", this.endpoint + "?callback=logSent", !0), t.setRequestHeader("Content-Type", "application/json"), "" !== this.name && t.setRequestHeader("X-Sumo-Name", this.name), "" !== this.sourceCategory && t.setRequestHeader("X-Sumo-Category", this.sourceCategory), "" !== this.hostName && t.setRequestHeader("X-Sumo-Host", this.hostName), t.send(e.join("\n"));
114 } catch (n) {
115 u = e, this.onError && this.onError();
116 }
117 }
118 },
119 flushLogs: function flushLogs() {
120 this.sendLogs();
121 },
122 emptyLogQueue: function emptyLogQueue() {
123 u = [];
124 },
125 getCookie: function getCookie() {
126 var e = t.cookie,
127 o = e.indexOf(r);
128 if (0 > o) return !1;
129 var s = e.indexOf(";", o + 1);
130 return s = 0 > s ? e.length : s, e.slice(o + a, s);
131 },
132 setCookie: function setCookie(e) {
133 t.cookie = r + "=" + e;
134 }
135 };
136 var c = e.SLLogger,
137 h = new o();
138
139 if (c && c.length) {
140 var g = 0,
141 f = c.length;
142
143 for (g = 0; f > g; g++) {
144 h.log(c[g]);
145 }
146 }
147
148 e.SLLogger = h, e.SumoLogger = o;
149}(window, document);
\No newline at end of file