aft-reporting-filesystem
Version:
Automated Functional Testing (AFT) reporting plugin package supporting logging to files
120 lines • 5.71 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilesystemReportingPlugin = exports.FilesystemReportingPluginConfig = void 0;
const process = require("node:process");
const path = require("node:path");
const aft_core_1 = require("aft-core");
const date = require("date-and-time");
class FilesystemReportingPluginConfig extends aft_core_1.ReportingPluginConfig {
constructor() {
super(...arguments);
this.logLevel = 'trace';
this.outputPath = path.join(process.cwd(), 'logs');
this.includeResults = true;
this.dateFormat = 'YYYY-MM-DD HH:mm:ss.SSS';
this.maxFilenameLength = 222;
this.ellipsisLocation = 'middle';
}
}
exports.FilesystemReportingPluginConfig = FilesystemReportingPluginConfig;
class FilesystemReportingPlugin extends aft_core_1.ReportingPlugin {
get logLevel() {
return this._level;
}
constructor(aftCfg) {
var _a, _b, _c, _d, _e, _f;
super(aftCfg);
this.initialise = (logName) => __awaiter(this, void 0, void 0, function* () {
/* do nothing */
});
this.log = (logObj) => __awaiter(this, void 0, void 0, function* () {
if (this.enabled) {
this._appendToFile(logObj);
}
});
this.submitResult = (result) => __awaiter(this, void 0, void 0, function* () {
if (this.enabled && this._includeResults) {
let level;
switch (result.status) {
case 'failed':
level = 'fail';
break;
case 'passed':
level = 'pass';
break;
case 'blocked':
case 'skipped':
level = 'warn';
break;
default:
level = 'info';
break;
}
const data = {
name: result.testName,
level,
message: JSON.stringify(result)
};
this._appendToFile(data);
}
});
this.finalise = (logName) => __awaiter(this, void 0, void 0, function* () {
/* do nothing */
});
const fslpc = this.aftCfg.getSection(FilesystemReportingPluginConfig);
this._level = (_b = (_a = fslpc.logLevel) !== null && _a !== void 0 ? _a : this.aftCfg.logLevel) !== null && _b !== void 0 ? _b : 'trace';
if (!path.isAbsolute(fslpc.outputPath)) {
this._outputPath = path.join(process.cwd(), fslpc.outputPath);
}
else {
this._outputPath = fslpc.outputPath;
}
this._includeResults = (_c = fslpc.includeResults) !== null && _c !== void 0 ? _c : true;
this._dateFormat = (_d = fslpc.dateFormat) !== null && _d !== void 0 ? _d : 'YYYY-MM-DD HH:mm:ss.SSS';
this._maxFilenameLength = (_e = fslpc.maxFilenameLength) !== null && _e !== void 0 ? _e : 222;
this._ellipsisLocation = (_f = fslpc.ellipsisLocation) !== null && _f !== void 0 ? _f : 'middle';
}
_appendToFile(logObj) {
if (aft_core_1.LogLevel.toValue(logObj.level) >= aft_core_1.LogLevel.toValue(this.logLevel) && logObj.level !== 'none') {
const filename = aft_core_1.convert.toSafeString(logObj.name);
const filenameShortened = (0, aft_core_1.ellide)(filename, this._maxFilenameLength, this._ellipsisLocation);
const fullPath = path.join(this._outputPath, `${filenameShortened}.log`);
const lock = new aft_core_1.ExpiringFileLock(fullPath, this.aftCfg);
try {
aft_core_1.fileio.append(fullPath, `${this._format(logObj)}\n`);
}
finally {
lock.unlock();
}
}
}
_format(logObj) {
var _a, _b, _c, _d, _e;
logObj !== null && logObj !== void 0 ? logObj : (logObj = {});
(_a = logObj.message) !== null && _a !== void 0 ? _a : (logObj.message = '');
(_b = logObj.level) !== null && _b !== void 0 ? _b : (logObj.level = 'none');
const dataStrings = (_d = (_c = logObj.data) === null || _c === void 0 ? void 0 : _c.map(d => {
try {
return JSON.stringify(d);
}
catch (_a) {
return d === null || d === void 0 ? void 0 : d.toString();
}
})) !== null && _d !== void 0 ? _d : [];
const args = ((_e = logObj.data) === null || _e === void 0 ? void 0 : _e.length) ? ` ${dataStrings.join(' ')}` : '';
const d = date.format(new Date(), this._dateFormat);
const out = `[${d}] - ${(0, aft_core_1.ellide)(logObj.level.toUpperCase(), 5, 'end', '')} - ${logObj.message}${args}`;
return out;
}
}
exports.FilesystemReportingPlugin = FilesystemReportingPlugin;
//# sourceMappingURL=filesystem-reporting-plugin.js.map