UNPKG

plogger-sdk

Version:

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

105 lines 5.1 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { logLevels } from "../logLevel"; // import {LogHelper} from './log-helper' import { jsonStringifyRecursive } from "../helperMethods"; import { getCallerStackFrame, isError, toErrorObject, } from "./errorObj"; var logRecord = /** @class */ (function () { function logRecord(level, scope, message, formatter, staticParams, enumMessage, timestampGenerator) { this.metaData = null; this.level = level; this.scope = scope; if (this.timestampGenerator) { this.timestamp = this.timestampGenerator(); } else { this.timestamp = new Date(); } var current_enumMessage = enumMessage; // checking if message is an error object var current_message = null; if (isError(message)) { var error_obj = toErrorObject(message); var _1 = error_obj.stack, temp_curr_message = __rest(error_obj, ["stack"]); this.metaData = error_obj.stack; current_message = temp_curr_message; } else { current_message = { message: message, }; } this.log = __assign(__assign(__assign({}, current_message), { staticParams: staticParams }), current_enumMessage); this.rawMessage = message; this.formatter = formatter; } logRecord.prototype.fetchCallStack = function (stackDepth) { this.metaData = getCallerStackFrame(stackDepth); }; logRecord.prototype.generateFormatPlaceholders = function () { var _a, _b, _c, _d; this.formatPlaceholders = {}; //denotes log level name this.formatPlaceholders["levelName"] = logLevels[this.level].name; //denotes the log level rank this.formatPlaceholders["levelRank"] = logLevels[this.level].rank; //denotes the timestamp in POSIX format this.formatPlaceholders["timestampEpoch"] = this.timestamp; //denotes the timestamp in ISO format this.formatPlaceholders["timestampISO"] = this.timestamp.toISOString(); //the scope of the logger, also defined as "name" in pLogger constructor this.formatPlaceholders["scope"] = this.scope; //the message entered by the user, could be a raw string or a enum message mapped to the string/key (check setLogEnums() and setEnumMessages()) this.formatPlaceholders["message"] = jsonStringifyRecursive(this.log); //the string message entered by the user this.formatPlaceholders["rawMessage"] = jsonStringifyRecursive(this.rawMessage); //Return the time value of the system this.formatPlaceholders["timestampLocalIso"] = new Date(this.timestamp.getTime() - this.timestamp.getTimezoneOffset() * 60000).toISOString(); var metaData = null; if (Array.isArray(this.metaData)) { metaData = this.metaData[0]; } else { metaData = this.metaData; } //Returns the filename where the log is present along with the line number this.formatPlaceholders["fileNameWithLine"] = (_a = metaData === null || metaData === void 0 ? void 0 : metaData.fileNameWithLine) !== null && _a !== void 0 ? _a : ""; //Return the filepath where the log is present along with the line number this.formatPlaceholders["filePathWithLine"] = (_b = metaData === null || metaData === void 0 ? void 0 : metaData.filePathWithLine) !== null && _b !== void 0 ? _b : ""; //Return the full file path this.formatPlaceholders["fullFilePath"] = (_c = metaData === null || metaData === void 0 ? void 0 : metaData.fullFilePath) !== null && _c !== void 0 ? _c : ""; //Return the method in which the log is being called. this.formatPlaceholders["method"] = (_d = metaData === null || metaData === void 0 ? void 0 : metaData.method) !== null && _d !== void 0 ? _d : ""; }; logRecord.prototype.getFormatPlaceholders = function () { if (this.formatPlaceholders === undefined) { this.generateFormatPlaceholders(); } return this.formatPlaceholders; }; return logRecord; }()); export { logRecord }; //# sourceMappingURL=log-record.js.map