UNPKG

typescript-logging

Version:

Library for logging, written in typescript, can be used by normal es5+ javascript as well.

192 lines 7.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ST = require("stacktrace-js"); var LoggerOptions_1 = require("../log/LoggerOptions"); /** * Some utilities to format messages. */ var MessageFormatUtils = (function () { function MessageFormatUtils() { } /** * Render given date in given DateFormat and return as String. * @param date Date * @param dateFormat Format * @returns {string} Formatted date */ MessageFormatUtils.renderDate = function (date, dateFormat) { var lpad = function (value, chars, padWith) { var howMany = chars - value.length; if (howMany > 0) { var res = ""; for (var i = 0; i < howMany; i++) { res += padWith; } res += value; return res; } return value; }; var fullYear = function (d) { return lpad(d.getFullYear().toString(), 4, "0"); }; var month = function (d) { return lpad((d.getMonth() + 1).toString(), 2, "0"); }; var day = function (d) { return lpad(d.getDate().toString(), 2, "0"); }; var hours = function (d) { return lpad(d.getHours().toString(), 2, "0"); }; var minutes = function (d) { return lpad(d.getMinutes().toString(), 2, "0"); }; var seconds = function (d) { return lpad(d.getSeconds().toString(), 2, "0"); }; var millis = function (d) { return lpad(d.getMilliseconds().toString(), 3, "0"); }; var dateSeparator = dateFormat.dateSeparator; var ds = ""; switch (dateFormat.formatEnum) { case LoggerOptions_1.DateFormatEnum.Default: // yyyy-mm-dd hh:mm:ss,m ds = fullYear(date) + dateSeparator + month(date) + dateSeparator + day(date) + " " + hours(date) + ":" + minutes(date) + ":" + seconds(date) + "," + millis(date); break; case LoggerOptions_1.DateFormatEnum.YearMonthDayTime: ds = fullYear(date) + dateSeparator + month(date) + dateSeparator + day(date) + " " + hours(date) + ":" + minutes(date) + ":" + seconds(date); break; case LoggerOptions_1.DateFormatEnum.YearDayMonthWithFullTime: ds = fullYear(date) + dateSeparator + day(date) + dateSeparator + month(date) + " " + hours(date) + ":" + minutes(date) + ":" + seconds(date) + "," + millis(date); break; case LoggerOptions_1.DateFormatEnum.YearDayMonthTime: ds = fullYear(date) + dateSeparator + day(date) + dateSeparator + month(date) + " " + hours(date) + ":" + minutes(date) + ":" + seconds(date); break; default: throw new Error("Unsupported date format enum: " + dateFormat.formatEnum); } return ds; }; /** * Renders given category log message in default format. * @param msg Message to format * @param addStack If true adds the stack to the output, otherwise skips it * @returns {string} Formatted message */ MessageFormatUtils.renderDefaultMessage = function (msg, addStack) { var result = ""; var logFormat = msg.logFormat; if (logFormat.showTimeStamp) { result += MessageFormatUtils.renderDate(msg.date, logFormat.dateFormat) + " "; } result += LoggerOptions_1.LogLevel[msg.level].toUpperCase(); if (msg.isResolvedErrorMessage) { result += " (resolved)"; } result += " "; if (logFormat.showCategoryName) { result += "["; msg.categories.forEach(function (value, idx) { if (idx > 0) { result += ", "; } result += value.name; }); result += "]"; } // Get the normal string message first var actualStringMsg = ""; var dataString = ""; var messageOrLogData = msg.message; if (typeof messageOrLogData === "string") { actualStringMsg = messageOrLogData; } else { var logData = messageOrLogData; actualStringMsg = logData.msg; // We do have data? if (logData.data) { dataString = " [data]: " + (logData.ds ? logData.ds(logData.data) : JSON.stringify(logData.data)); } } result += " " + actualStringMsg + "" + dataString; if (addStack && msg.errorAsStack !== null) { result += "\n" + msg.errorAsStack; } return result; }; /** * Renders given log4j log message in default format. * @param msg Message to format * @param addStack If true adds the stack to the output, otherwise skips it * @returns {string} Formatted message */ MessageFormatUtils.renderDefaultLog4jMessage = function (msg, addStack) { var format = msg.logGroupRule.logFormat; var result = ""; if (format.showTimeStamp) { result += MessageFormatUtils.renderDate(msg.date, format.dateFormat) + " "; } result += LoggerOptions_1.LogLevel[msg.level].toUpperCase() + " "; if (format.showLoggerName) { result += "[" + msg.loggerName + "]"; } // Get the normal string message first var actualStringMsg = ""; var dataString = ""; if (typeof msg.message === "string") { actualStringMsg = msg.message; } else { var logData = msg.message; actualStringMsg = logData.msg; // We do have data? if (logData.data) { dataString = " [data]: " + (logData.ds ? logData.ds(logData.data) : JSON.stringify(logData.data)); } } result += " " + actualStringMsg + "" + dataString; if (addStack && msg.errorAsStack !== null) { result += "\n" + msg.errorAsStack; } return result; }; /** * Render error as stack * @param error Return error as Promise * @returns {Promise<string>|Promise} Promise for stack */ MessageFormatUtils.renderError = function (error) { var result = error.name + ": " + error.message + "\n@"; return new Promise(function (resolve) { // This one has a promise too ST.fromError(error, { offline: true }).then(function (frames) { var stackStr = (frames.map(function (frame) { return frame.toString(); })).join("\n "); result += "\n" + stackStr; // This resolves our returned promise resolve(result); }).catch(function () { result = "Unexpected error object was passed in. "; try { result += "Could not resolve it, stringified object: " + JSON.stringify(error); } catch (e) { // Cannot stringify can only tell something was wrong. result += "Could not resolve it or stringify it."; } resolve(result); }); }); }; return MessageFormatUtils; }()); exports.MessageFormatUtils = MessageFormatUtils; //# sourceMappingURL=MessageUtils.js.map