plogger-sdk
Version:
Typescript based frontend logging library compatible with multiple transports and JS/TS frameworks
188 lines • 7.34 kB
JavaScript
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 __());
};
})();
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);
};
import { BaseLogger } from "../Logger";
import { logLevel, logLevels } from "../logLevel";
import { logRecord } from "../logs/log-record";
/**
* The main library abstraction
* @param name string
* @param hideLogPositionForPerformance boolean
* @param stackDepth number
* @param minLevel logLevel
* @param formatter string
* @param staticParams Object
*/
var pLogger = /** @class */ (function (_super) {
__extends(pLogger, _super);
function pLogger(_a) {
var _b = _a.name, name = _b === void 0 ? "Root" : _b, _c = _a.minLevel, minLevel = _c === void 0 ? null : _c, _d = _a.formatter, formatter = _d === void 0 ? null : _d, _e = _a.staticParams, staticParams = _e === void 0 ? {} : _e, _f = _a.hideLogPositionForPerformance, hideLogPositionForPerformance = _f === void 0 ? true : _f, _g = _a.stackDepth, stackDepth = _g === void 0 ? 0 : _g;
var _this = _super.call(this) || this;
_this.handlers = [];
_this.level = minLevel;
_this.formatter = formatter;
_this.name = name;
_this.hideLogPositionForPerformance = hideLogPositionForPerformance;
_this.stackDepth = stackDepth;
_this.staticParams = staticParams;
_this.logEnums = {};
_this.enumMessages = {};
_this.timestampGenerator = null;
return _this;
}
/**
* Add the logger to send the logs to respective transports.
* @param logger BaseLogger
*/
pLogger.prototype.addHandler = function (logger) {
var index = this.handlers.indexOf(logger, 0);
if (index == -1) {
this.handlers.push(logger);
}
};
pLogger.prototype.removeHandler = function (logger) {
var index = this.handlers.indexOf(logger, 0);
if (index > -1) {
this.handlers.splice(index, 1);
}
};
pLogger.prototype.checkHandlers = function () {
//using this method for debugging purposes
console.trace("pLoggerSDK Handlers: ", this.handlers);
};
pLogger.prototype.setTimestampGenerator = function (timestampGenerator) {
this.timestampGenerator = timestampGenerator;
};
// setLevel log threshold
pLogger.prototype.verify_log_level = function (threshold, incoming_level) {
if (threshold != null &&
logLevels[incoming_level].rank > logLevels[threshold].rank) {
return false;
}
return true;
};
/**
*
* @param logEnums - user defined object keys
* defined in format
* {
* key1 : "key1",
* key2 : "key2"
* ...
* }
*/
pLogger.prototype.setLogEnums = function (logEnums) {
this.logEnums = logEnums;
};
/**
*
* @param enumMessages - user defined object consisting of message object literals mapped to logEnums
*/
pLogger.prototype.setEnumMessages = function (enumMessages) {
this.enumMessages = enumMessages;
};
/**
* define new staticParams for pLogger
* @param newParams - dynamic object literal
*/
pLogger.prototype.updateParams = function (newParams) {
this.staticParams = newParams;
};
/**
* add on new staticParams or override preexisting staticParams.
* @param newParams - dynamic object literal
*/
pLogger.prototype.appendParams = function (newParams) {
var buffer = __assign(__assign({}, this.staticParams), newParams);
this.staticParams = buffer;
};
pLogger.prototype.log = function (logRecord) {
return;
};
pLogger.prototype._log = function (message, log_level, staticParams) {
var _this = this;
if (staticParams === void 0) { staticParams = null; }
//taking log based enum if the string message is in the enum keys.
var logData = this.enumMessages[message];
var enum_message;
if (logData) {
enum_message = __assign({}, logData);
}
else {
enum_message = null;
}
var currentParams;
if (staticParams != null) {
currentParams = __assign(__assign({}, this.staticParams), staticParams);
}
else {
currentParams = this.staticParams;
}
var current_log = new logRecord(log_level, this.name, message, this.formatter, currentParams, enum_message, this.timestampGenerator);
if ((!this.hideLogPositionForPerformance && this.stackDepth > 0) ||
logLevels[log_level].rank <= logLevels[logLevel.Error].rank) {
current_log.fetchCallStack(this.stackDepth);
}
if (!this.verify_log_level(this.level, log_level)) {
return;
}
//sending log to each handler.
this.handlers.forEach(function (handler) {
if (!_this.verify_log_level(handler.getLevel(), log_level)) {
return;
}
handler.log(current_log);
});
};
pLogger.prototype.critical = function (message, staticParams) {
if (staticParams === void 0) { staticParams = null; }
this._log(message, logLevel.Critical, staticParams);
};
pLogger.prototype.error = function (message, staticParams) {
if (staticParams === void 0) { staticParams = null; }
this._log(message, logLevel.Error, staticParams);
};
pLogger.prototype.info = function (message, staticParams) {
if (staticParams === void 0) { staticParams = null; }
this._log(message, logLevel.Info, staticParams);
};
pLogger.prototype.debug = function (message, staticParams) {
if (staticParams === void 0) { staticParams = null; }
this._log(message, logLevel.Debug, staticParams);
};
pLogger.prototype.warn = function (message, staticParams) {
if (staticParams === void 0) { staticParams = null; }
this._log(message, logLevel.Warn, staticParams);
};
pLogger.prototype.trace = function (message, staticParams) {
if (staticParams === void 0) { staticParams = null; }
this._log(message, logLevel.Trace, staticParams);
};
return pLogger;
}(BaseLogger));
export { pLogger };
export { logLevel };
//# sourceMappingURL=p-logger.js.map