@applitools/logger
Version:
Applitools logger
91 lines (90 loc) • 4.14 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatter = void 0;
const colorize_1 = require("./colorize");
const util_1 = require("util");
const utils = __importStar(require("@applitools/utils"));
const defaultColorTheme = {
label: 'cyan',
tags: 'blueBright',
timestamp: 'greenBright',
level: {
info: ['bgBlueBright', 'black'],
warn: ['bgYellowBright', 'black'],
error: ['bgRedBright', 'white'],
fatal: ['bgRed', 'white'],
},
};
function formatter(chunks, { prelude = true, label, timestamp = new Date(), level = 'info', tags, colors, masks, maskLog } = {}) {
var _a, _b;
if (utils.types.isBoolean(colors))
colors = colors ? defaultColorTheme : undefined;
const shouldMaskMessage = (_a = utils.general.getEnvValue('MASK_LOG', 'boolean')) !== null && _a !== void 0 ? _a : maskLog;
const message = [];
if (prelude) {
if (label) {
const text = label;
const color = colors === null || colors === void 0 ? void 0 : colors.label;
message.push(color ? (0, colorize_1.colorize)(text, { color }) : text);
}
if (tags && !utils.types.isEmpty(tags)) {
const text = `(${utils.types.isArray(tags[0]) ? tags.map(tags => tags.join('/')).join(' & ') : tags.join('/')})`;
const color = colors === null || colors === void 0 ? void 0 : colors.tags;
message.push(color ? (0, colorize_1.colorize)(text, { color }) : text);
}
if (!colors && (label || (tags && !utils.types.isEmpty(tags)))) {
message.push('|');
}
if (timestamp) {
timestamp = timestamp === true ? new Date() : timestamp;
const text = timestamp.toISOString();
const color = colors === null || colors === void 0 ? void 0 : colors.timestamp;
message.push(color ? (0, colorize_1.colorize)(text, { color }) : text);
}
if (level) {
const text = level.toUpperCase().padEnd(5);
const color = (_b = colors === null || colors === void 0 ? void 0 : colors.level) === null || _b === void 0 ? void 0 : _b[level];
message.push(color ? (0, colorize_1.colorize)(` ${text} `, { color }) : `[${text}]`);
}
}
if (chunks && chunks.length > 0) {
const color = colors === null || colors === void 0 ? void 0 : colors.message;
const strings = chunks.map(chunk => {
let text = utils.types.isString(chunk)
? (0, colorize_1.colorize)(chunk, { color })
: util_1.inspect === null || util_1.inspect === void 0 ? void 0 : (0, util_1.inspect)(chunk, { colors: Boolean(colors), compact: 5, depth: 5 });
if (shouldMaskMessage) {
masks === null || masks === void 0 ? void 0 : masks.forEach(mask => {
text = text.replaceAll(mask, '***');
});
}
return text;
});
message.push(strings.join(' '));
}
return message.join(' ');
}
exports.formatter = formatter;