@shipengine/connect
Version:
The official developer tooling for building ShipEngine connect apps
80 lines • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDIPLogs = void 0;
const tslib_1 = require("tslib");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
/**
* Parse logs from DIP into a more human readable format.
*/
function parseDIPLogs(logs, lines = 500) {
// Strip tailing logs that are greater than the line parameter
const splitLogs = logs.split('\n');
// Remove empty space tail to prevent unnecessary newline at the end of the logs
if (splitLogs[splitLogs.length - 1] === '') {
splitLogs.pop();
}
// Currently the DIP only supports a maximum of 1500 lines.
if (lines > 1500) {
lines = 1500;
}
const trimmedLogs = splitLogs.slice(splitLogs.length - lines);
const parsedLogs = [];
for (const log of trimmedLogs) {
try {
// If not valid JSON object then output raw string data.
const parsedLog = JSON.parse(log);
// Check for DIP specifc
if (isDIPLog(parsedLog)) {
parsedLogs.push(formatDIPLog(parsedLog));
}
else {
parsedLogs.push(JSON.stringify(parsedLog));
}
}
catch {
parsedLogs.push(chalk_1.default.grey(log));
}
}
return parsedLogs;
}
exports.parseDIPLogs = parseDIPLogs;
function isDIPLog(obj) {
return 'level' in obj && 'message' in obj;
}
function isHTTPLog(obj) {
if ('meta' in obj) {
return 'request' in obj.meta && 'response' in obj.meta;
}
return false;
}
function formatDIPLog(log) {
let formattedMessage = '';
switch (log.level) {
case 'info':
formattedMessage = chalk_1.default.green(`${log.metadata.timestamp}`);
break;
case 'warning':
formattedMessage = chalk_1.default.yellow(`${log.metadata.timestamp}`);
break;
case 'error':
formattedMessage = chalk_1.default.red(`${log.metadata.timestamp}`);
break;
default:
formattedMessage = chalk_1.default.green(`${log.metadata.timestamp}`);
}
const tid = log.transactionId;
formattedMessage += `: message=${chalk_1.default.grey(log.message)}`;
if (tid !== 'no-txid' && tid !== undefined) {
formattedMessage += ` transactionId=${tid}`;
}
if (isHTTPLog(log.metadata)) {
formattedMessage += ` meta=${chalk_1.default.grey(JSON.stringify(log.metadata.meta))}`;
}
for (const key of Object.keys(log.metadata)) {
if (!['meta', 'timestamp'].includes(key)) {
formattedMessage += ` ${key}=${chalk_1.default.grey(JSON.stringify(log.metadata[key]))}`;
}
}
return formattedMessage;
}
//# sourceMappingURL=dip-log-helpers.js.map