hayd-caf
Version:
Haystacks D-CAF: Distinguished Cloud Automation Framework is a web/cloud/SaaS/mobile GUI automation plugin that leverages a suite of business rules and a factory pattern to auto-generate functions and strings for logging that are used to execute test acti
203 lines (177 loc) • 9.67 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: executrix/loggers.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: executrix/loggers.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @file loggers.js
* @module loggers
* @description Contains all of the functions necessary for logging to the console,
* and logging to the system-specified log file.
* Some of the logic here is duplicated from the Haystacks repo, but kept super basic intentionally.
* Logging from the plugin will remain hard-coded, because the data isn't loaded,
* and by the time it is loaded its already useless to log.
* So uncomment the lines in the plugin code if you need to log from the plugin, and leave them commented out if you don't need to log.
* This is setup here so that the logs can be added to the application level log file,
* especially to include data, as the data can be too much and will easily overflow the console buffer.
* @requires module:plugin.constants
* @requires module:pluginData
* @requires {@link https://www.npmjs.com/package/@haystacks/constants|@haystacks/constants}
* @requires {@link https://www.npmjs.com/package/moment|moment}
* @requires {@link https://nodejs.dev/learn/the-nodejs-fs-module|fs}
* @requires {@link https://www.npmjs.com/package/path|path}
* @author Seth Hollingsead
* @date 2023/04/03
* @copyright Copyright © 2023-… by Seth Hollingsead. All rights reserved
*/
// Internal imports
import * as plg from '../constants/plugin.constants.js';
import D from '../structures/pluginData.js';
// External imports
import hayConst from '@haystacks/constants';
import moment from 'moment';
import fs from 'fs';
import path from 'path';
const {bas, cfg, gen, msg, wrd} = hayConst;
const baseFileName = path.basename(import.meta.url, path.extname(import.meta.url));
// plugins.hayD-CAF.executrix.loggers.
const namespacePrefix = wrd.cplugins + bas.cDot + plg.cpluginName + bas.cDot + wrd.cexecutrix + bas.cDot + baseFileName + bas.cDot;
/**
* @function getLogFileNameAndPath
* @description Determines, using the Haystacks configuration settings data passed as input to the plugin entry-point,
* what the log file name and path should be.
* @param {object} haystacksConfigData A JSON object that contains all of the configuration data including the log file name and path.
* @return {string} The full path and file name for the log file.
* @author Seth Hollingsead
* @date 2023/04/03
*/
async function getLogFileNameAndPath(haystacksConfigData) {
// let functionName = getLogFileNameAndPath.name;
// console.log(`BEGIN ${namespacePrefix}${functionName} function`);
// haystacksConfigData is:
// console.log(msg.chaystacksConfigDataIs + JSON.stringify(haystacksConfigData));
let logFile = haystacksConfigData[wrd.cconfiguration][wrd.csystem][wrd.csystem + bas.cDot + cfg.cclientRootPath];
if (logFile !== undefined) {
logFile = path.join(logFile + bas.cForwardSlash + wrd.clogs + bas.cForwardSlash + haystacksConfigData[wrd.cconfiguration][wrd.csystem][wrd.csystem + bas.cDot + cfg.clogFileName]);
logFile = path.normalize(logFile);
}
// logFile is:
// console.log(msg.clogFileIs + logFile);
// console.log(`END ${namespacePrefix}${functionName} function`);
return logFile;
}
/**
* @function consoleLog
* @description Logs out to the console, and to the same current Haystacks-application log file.
* @param {string} classPath The class pass from which this console log function was called from.
* @param {string} message The message and/or data that should be logged to the console, and the log file.
* @return {void}
* @author Seth Hollingsead
* @date 2023/04/04
*/
async function consoleLog(classPath, message) {
// let functionName = consoleLog.name;
// console.log(`BEGIN ${namespacePrefix}${functionName} function`);
// classPath is:
// console.log(msg.cclassPathIs + classPath);
// message is:
// console.log(msg.cmessageIs + message);
let newMessage = '';
if (message.includes(bas.cDoublePercent) === true) {
// Replace the double percent with the class path.
newMessage = message.replaceAll(bas.cDoublePercent, classPath);
} else {
newMessage = message;
}
console.log(newMessage);
let dateTimeStamp = await getNowMoment(gen.cYYYY_MM_DD_HH_mm_ss_SSS);
await appendMessageToFile(`${dateTimeStamp}: ${plg.cpluginName}: ${newMessage}`);
// console.log(`END ${namespacePrefix}${functionName} function`);
}
/**
* @function appendMessageToFile
* @description Opens a file and appends a message to the file, then closes the file.
* @param {string} message The message that should be appended to the log file.
* @return {void}
* @author Seth Hollingsead
* @date 2023/04/04
*/
async function appendMessageToFile(message) {
// let functionName = appendMessageToFile.name;
// console.log(`BEGIN ${namespacePrefix}${functionName} function`);
// message is:
// console.log(msg.cmessageIs + message);
let fd;
let filePathAndName = D[wrd.cdata][cfg.cLogFilePathAndName];
if (message && filePathAndName) {
try {
// open the file sync:
// console.log(msg.copenFileSyncMessage + filePathAndName);
fd = fs.openSync(filePathAndName, bas.ca);
// append to the file sync:
// console.log(msg.cappendFileSyncMessage + filePathAndName);
fs.appendFileSync(fd, message + bas.cCarriageReturn + bas.cNewLine, gen.cUTF8);
// DONE appending to the file:
// console.log(msg.cDoneAppendingToFileMessage + filePathAndName);
} catch (err) {
console.log(err.message);
} finally {
if (fd !== undefined) {
fs.closeSync(fd);
}
}
}
// console.log(`END ${namespacePrefix}${functionName} function`);
}
/**
* @function getNowMoment
* @description Returns a time stamp string formatted according to the input formatting string.
* @param {string} formatString The formatting string, that tells moment in what format to
* return the value for the day, month, year, hour, minute, second, and millisecond.
* @return {string} A time stamp string that has been formatted according to the input format.
* @author Seth Hollingsead
* @date 2023/04/04
*/
async function getNowMoment(formatString) {
// let functionName = getNowMoment.name;
// console.log(`BEGIN ${namespacePrefix}${functionName} function`);
// formatString is:
// console.log(msg.cformatStringIs + formatString);
let returnData = '';
returnData = moment().format(formatString);
// console.log(msg.creturnDataIs + returnData);
// console.log(`END ${namespacePrefix}${functionName} function`);
return returnData;
}
export default {
getLogFileNameAndPath,
consoleLog,
appendMessageToFile
}</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-allPluginConstantsValidationMetadata.html">allPluginConstantsValidationMetadata</a></li><li><a href="module-chiefCommander.html">chiefCommander</a></li><li><a href="module-chiefConfiguration.html">chiefConfiguration</a></li><li><a href="module-chiefData.html">chiefData</a></li><li><a href="module-chiefRules.html">chiefRules</a></li><li><a href="module-chiefTheme.html">chiefTheme</a></li><li><a href="module-chiefWorkflow.html">chiefWorkflow</a></li><li><a href="module-commandBroker.html">commandBroker</a></li><li><a href="module-commandsLibrary.html">commandsLibrary</a></li><li><a href="module-dCafCommands.html">dCafCommands</a></li><li><a href="module-dataBroker.html">dataBroker</a></li><li><a href="module-loggers.html">loggers</a></li><li><a href="module-main.html">main</a></li><li><a href="module-pluginData.html">pluginData</a></li><li><a href="module-ruleBroker.html">ruleBroker</a></li><li><a href="module-rulesLibrary.html">rulesLibrary</a></li><li><a href="module-selectorConstruction.html">selectorConstruction</a></li><li><a href="module-warden.html">warden</a></li><li><a href="plugin.business.constants.module_validation.html">plugin.business.constants.validation</a></li><li><a href="plugin.business.module_constants.html">plugin.business.constants</a></li><li><a href="plugin.command.constants.module_validation.html">plugin.command.constants.validation</a></li><li><a href="plugin.command.module_constants.html">plugin.command.constants</a></li><li><a href="plugin.constants.module_validation.html">plugin.constants.validation</a></li><li><a href="plugin.message.constants.module_validation.html">plugin.message.constants.validation</a></li><li><a href="plugin.message.module_constants.html">plugin.message.constants</a></li><li><a href="plugin.module_constants.html">plugin.constants</a></li><li><a href="plugin.system.constants.module_validation.html">plugin.system.constants.validation</a></li><li><a href="plugin.system.module_constants.html">plugin.system.constants</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Apr 28 2023 18:01:37 GMT-0500 (Central Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>