UNPKG

slf

Version:
150 lines (149 loc) 6.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Logger", { enumerable: true, get: function() { return Logger; } }); var _loggerFactory = require("./LoggerFactory"); var _utils = require("./utils"); function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } var checkIfLogFuncKey = function(key) { return Object.keys(Logger.prototype).filter(function(key) { return key !== "log"; }).includes(key); }; var Logger = /*#__PURE__*/ function() { "use strict"; function Logger(name, sink, chain, logLevel) { var _this = this; _classCallCheck(this, Logger); this.name = name; this.sink = sink; this.chain = chain; this.logLevel = logLevel; this.log = function(level) { for(var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++){ params[_key - 1] = arguments[_key]; } if (level) { var levelKey = (0, _utils.capitalize)(level); if (_loggerFactory.Level[levelKey] < _this.logLevel) { return; } var event = _this.buildLogEvent(level, params); var i = 0; // apply middlewares var next = function(err, evt) { if (err) { _this.sink(_this.buildLogEvent("error", [ err ])); } if (_this.chain && _this.chain[i]) { _this.chain[i++](evt, next); } else { // sink to logger impl _this.sink(evt); } }; // start middleware chain next(null, event); } else { var _$level = params[0]; if (checkIfLogFuncKey(_$level)) { _this[_$level].apply(_this, params.slice(1)); } else { _this.info.apply(_this, params); } } }; this./** Log fine grained informational events that are most useful to debug an application. */ debug = function() { for(var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++){ params[_key] = arguments[_key]; } _this.log.apply(_this, [ "debug" ].concat(_toConsumableArray(params))); }; this./** Log informational messages that highlight the progress of the application at a coarse grained level. */ info = function() { for(var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++){ params[_key] = arguments[_key]; } _this.log.apply(_this, [ "info" ].concat(_toConsumableArray(params))); }; this./** Log potentially harmful situations. */ warn = function() { for(var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++){ params[_key] = arguments[_key]; } _this.log.apply(_this, [ "warn" ].concat(_toConsumableArray(params))); }; this./** Log error events that might still allow the application to continue running. */ error = function() { for(var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++){ params[_key] = arguments[_key]; } _this.log.apply(_this, [ "error" ].concat(_toConsumableArray(params))); }; this./** Log very severe error events that will presumably lead the application to abort- */ critical = function() { for(var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++){ params[_key] = arguments[_key]; } _this.log.apply(_this, [ "critital" ].concat(_toConsumableArray(params))); }; } var _proto = Logger.prototype; _proto.buildLogEvent = function buildLogEvent(level, params) { var event = { name: this.name, level: level, params: params, timeStamp: new Date().valueOf() }; return event; }; Logger.getLogger = function getLogger() { var name = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : "/"; return _loggerFactory.LoggerFactory.getLogger(name); }; return Logger; }();