UNPKG

container.ts

Version:
99 lines 3.91 kB
"use strict"; 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 (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var container_1 = require("../../container"); var error_1 = require("../error"); var validate_1 = require("../validate"); /** Logs environment variable names. */ var ELogsEnv; (function (ELogsEnv) { /** Application logs level (default warning). */ ELogsEnv["Level"] = "LOGS_LEVEL"; })(ELogsEnv = exports.ELogsEnv || (exports.ELogsEnv = {})); /** Abstract container logs handler module. */ var Logs = /** @class */ (function (_super) { __extends(Logs, _super); function Logs(options) { var _this = _super.call(this, options) || this; /** * Parsed application logs level. * Get log level from environment, defaults to warning. */ _this.envLogsLevel = _this.logsParseLevel(validate_1.isString(_this.environment.get(ELogsEnv.Level, "warning"))); // Subscribe to container log messages filtered by level. _this.container.filterLogs(_this.envLogsLevel).subscribe(function (log) { return _this.logsOnMessage(log); }); return _this; } /** * Returns a new instance of ContainerLogMessage where message is a string. * Some logging libraries only accept strings, in case message is an error, message/name property * is used for log and other error properties are appended to log arguments array. */ Logs.prototype.logsMessageAsString = function (log) { var message = log.message; var args = log.args; // If log message is an error instance, use message string and // prepend error object to variable arguments. if (error_1.ErrorChain.isError(log.message)) { var error = log.message; message = error.message || error.name; args = [error].concat(log.args); } return new container_1.ContainerLogMessage(log.level, message, log.metadata, args); }; /** Convert environment log level string to level index, defaults to warning. */ Logs.prototype.logsParseLevel = function (level) { switch ((level || "").toLowerCase()) { case "emerg": case "emergency": { return container_1.ELogLevel.Emergency; } case "alert": { return container_1.ELogLevel.Alert; } case "crit": case "critical": { return container_1.ELogLevel.Critical; } case "err": case "error": { return container_1.ELogLevel.Error; } case "warn": case "warning": { return container_1.ELogLevel.Warning; } case "notice": { return container_1.ELogLevel.Notice; } case "info": case "information": case "informational": { return container_1.ELogLevel.Informational; } case "debug": { return container_1.ELogLevel.Debug; } default: { return container_1.ELogLevel.Warning; } } }; /** Default module name. */ Logs.moduleName = "Logs"; return Logs; }(container_1.Module)); exports.Logs = Logs; //# sourceMappingURL=logs.js.map