UNPKG

plogger-sdk

Version:

Typescript based frontend logging library compatible with multiple transports and JS/TS frameworks

146 lines 6.16 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { BaseLogger } from "../Logger"; import { jsonStringifyRecursive } from "../helperMethods"; import { logLevel, logLevels } from "../logLevel"; /** * HttpLogger -> defines the logger to infer logs to the remote server */ var HttpLogger = /** @class */ (function (_super) { __extends(HttpLogger, _super); function HttpLogger(_a) { var _b = _a.serverUrl, serverUrl = _b === void 0 ? "api/url" : _b, _c = _a.batchOptions, batchOptions = _c === void 0 ? null : _c, _d = _a.retryOptions, retryOptions = _d === void 0 ? { maxRetries: 3, retryDelay: 1000 } : _d, _e = _a.highPriority, highPriority = _e === void 0 ? logLevel.Critical : _e, _f = _a.setCredentials, setCredentials = _f === void 0 ? false : _f, _g = _a.showFormattedLog, showFormattedLog = _g === void 0 ? false : _g; var _this = _super.call(this) || this; /**define the timeout object to flush the last log batch*/ _this.timeoutRef = null; _this.logBucket = []; // this.config = config; _this.serverUrl = serverUrl; _this.batchOptions = batchOptions; _this.retryOptions = retryOptions; _this.highPriority = highPriority; _this.setCredentials = setCredentials; _this.showFormattedLog = showFormattedLog; //out of the config _this.headers = {}; //default headers return _this; } HttpLogger.prototype._buildLogObject = function (log) { return { level: logLevels[log.level], timestamp: log.timestamp.toISOString(), log: log.log, scope: log.scope, metaData: log.metaData, }; }; HttpLogger.prototype.setPriorityLog = function (priority_level) { this.highPriority = priority_level; }; /** * Set custom headers to your Http Request * @param headers Headers() */ HttpLogger.prototype.setHeaders = function (headers) { this.headers = headers; }; HttpLogger.prototype.log = function (logRecord) { var _this = this; var http_log = { logData: this._buildLogObject(logRecord), }; if (this.showFormattedLog) { http_log.formattedLog = this.formatLog(logRecord); } //if batchOptions doesn't exist if (this.batchOptions == null) { //batchOptions doesn't exist this.sendBatchRequest([http_log]); return; } //HighPriority threshold if (logLevels[this.highPriority].rank >= http_log.logData.level.rank) { //push this log directly to the server instead of the batch. this.sendBatchRequest([http_log]); } else { this.logBucket.push(http_log); } // log batch flush upon exceeding batchSize if (this.logBucket.length >= this.batchOptions.batchSize) { this.sendBatchRequest(); } if (this.timeoutRef != null) { return; } // log batch flush upon not fulfilling batchSize this.timeoutRef = setTimeout(function () { clearTimeout(_this.timeoutRef); _this.timeoutRef = null; if (_this.logBucket.length > 0) { _this.sendBatchRequest(); } }, this.batchOptions.debounceTime); }; HttpLogger.prototype.sendBatchRequest = function (batchPayload) { if (batchPayload === void 0) { batchPayload = null; } var _tempBuffer = batchPayload; if (batchPayload == null) { _tempBuffer = this.logBucket; this.logBucket = []; } // this.logBucket = []; var _emitBatch = { logBuffer: _tempBuffer }; this.sendRequest(this.serverUrl, _emitBatch, this.retryOptions.maxRetries, this.retryOptions.retryDelay, this.setCredentials, this.headers); }; HttpLogger.prototype.sendRequest = function (serverUrl, requestData, maxRetries, backoffTimeout, setCredentials, headers) { var _this = this; // "Accept": "application/json", var retryCodes = [408, 500, 502, 503, 504, 522, 404, 400, 405]; var fetchHeaders = new Headers({ "Content-Type": "application/json", }); var credentialHeader = setCredentials ? "include" : "omit"; for (var _i = 0, _a = Object.entries(headers); _i < _a.length; _i++) { var _b = _a[_i], key = _b[0], value = _b[1]; fetchHeaders.append(key, value); } fetch(serverUrl, { method: "POST", credentials: credentialHeader, headers: fetchHeaders, body: jsonStringifyRecursive(requestData), }).then(function (response) { // return response.json(); if (response.ok) { return response.json(); } if (maxRetries > 0 && retryCodes.includes(response.status)) { setTimeout(function () { return _this.sendRequest(serverUrl, requestData, maxRetries - 1, backoffTimeout * 2, setCredentials, headers); }, backoffTimeout); } else { throw new Error("Response error occured...".concat(response)); } }); }; return HttpLogger; }(BaseLogger)); export { HttpLogger }; //# sourceMappingURL=http-logger.js.map