Skip to content

Commit

Permalink
Add logger setup (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
leelaprasadv authored May 6, 2024
1 parent 2ec86fd commit 2915e29
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src/beats/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const request = require('phin-retry');
const TestResult = require('test-results-parser/src/models/TestResult');
const { getCIInformation } = require('../helpers/ci');
const logger = require('../utils/logger');
const { HOOK } = require('../helpers/constants');

function get_base_url() {
Expand All @@ -20,7 +21,7 @@ async function run(config, result) {
await attachTestBeatsFailureSummary(config, result, run_id);
}
} else {
console.warn('Missing testbeats config parameters');
logger.warn('Missing testbeats config parameters');
}
}

Expand All @@ -47,7 +48,7 @@ function isValid(config) {
* @param {TestResult} result
*/
async function publishTestResults(config, result) {
console.log("Publishing results to TestBeats");
logger.info("Publishing results to TestBeats Cloud...");
try {
const payload = {
project: config.project,
Expand All @@ -66,10 +67,11 @@ async function publishTestResults(config, result) {
},
body: payload
});
logger.info("Result published to TestBeats Cloud successfully!");
return response.id;
} catch (error) {
console.log("Unable to publish results to TestBeats");
console.log(error);
logger.error(`Unable to publish results to TestBeats Cloud: ${error.message}`);
logger.debug(error);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ const sade = require('sade');

const prog = sade('testbeats');
const publish = require('./commands/publish');
const logger = require('./utils/logger');

prog
.version('2.0.1')
.option('-c, --config', 'Provide path to custom config', 'config.json');
.option('-c, --config', 'Provide path to custom config', 'config.json')
.option('-l, --logLevel', 'Log Level', "INFO");

prog.command('publish')
.action(async (opts) => {
try {
logger.setLevel(opts.logLevel);
logger.info(`Initiating...`);
await publish.run(opts);
} catch (error) {
console.error(error);
logger.error(`Report publish failed: ${error.message}`);
process.exit(1);
}
});
Expand Down
14 changes: 12 additions & 2 deletions src/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const prp = require('performance-results-parser');
const { processData } = require('../helpers/helper');
const beats = require('../beats');
const target_manager = require('../targets');
const logger = require('../utils/logger');

/**
* @param {import('../index').PublishOptions} opts
Expand Down Expand Up @@ -35,13 +36,15 @@ async function run(opts) {
validateConfig(config);
await processReport(config);
}
logger.info("Report successfully processed!")
}

/**
*
* @param {import('../index').PublishReport} report
*/
async function processReport(report) {
logger.debug("processReport: Started")
const parsed_results = [];
for (const result_options of report.results) {
if (result_options.type === 'custom') {
Expand All @@ -61,28 +64,32 @@ async function processReport(report) {
await target_manager.run(target, result);
}
} else {
console.log('No targets defined, skipping sending results to targets');
logger.warn('No targets defined, skipping sending results to targets');
}
}
logger.debug("processReport: Ended")
}

/**
*
* @param {import('../index').PublishReport} config
*/
function validateConfig(config) {
logger.info("Validating publish configuration...")
if (!config) {
throw new Error('Missing publish config');
}
validateResults(config);
validateTargets(config);
logger.info("Validating publish configuration sucessful")
}

/**
*
* @param {import('../index').PublishReport} config
*/
function validateResults(config) {
logger.debug("Validating results...")
if (!config.results) {
throw new Error('Missing results properties in config');
}
Expand Down Expand Up @@ -112,15 +119,17 @@ function validateResults(config) {
}
}
}
logger.debug("Validating results - Successful!")
}

/**
*
* @param {import('../index').PublishReport} config
*/
function validateTargets(config) {
logger.debug("Validating targets...")
if (!config.targets) {
console.warn('targets are not defined in config');
logger.warn('Targets are not defined in config');
return;
}
if (!Array.isArray(config.targets)) {
Expand Down Expand Up @@ -150,6 +159,7 @@ function validateTargets(config) {
}
}
}
logger.debug("Validating targets - Successful!")
}

module.exports = {
Expand Down
7 changes: 4 additions & 3 deletions src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const ci_info = require('./ci-info');
const ai_failure_summary = require('./ai-failure-summary');
const { EXTENSION } = require('../helpers/constants');
const { checkCondition } = require('../helpers/helper');
const logger = require('../utils/logger');

async function run(options) {
const { target, result, hook } = options;
Expand All @@ -25,9 +26,9 @@ async function run(options) {
try {
await extension_runner.run(options);
} catch (error) {
console.log('Failed to run extension');
console.log(extension);
console.log(error);
logger.error(`Failed to run extension: ${error.message}`);
logger.debug(`Extension details`, extension);
logger.debug(`Error: `, error);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/extensions/report-portal-history.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { getSuiteHistory, getLastLaunchByName, getLaunchDetails } = require('../helpers/report-portal');
const { addChatExtension, addSlackExtension, addTeamsExtension } = require('../helpers/extension.helper');
const { HOOK, STATUS } = require('../helpers/constants');
const logger = require('../utils/logger');

async function getLaunchHistory(extension) {
const { inputs, outputs } = extension;
Expand Down Expand Up @@ -71,8 +72,8 @@ async function run({ extension, target, payload }) {
}
}
} catch (error) {
console.log('Failed to get report portal history');
console.log(error);
logger.error(`Failed to get report portal history: ${error.message}`);
logger.debug(`Error: ${error}`);
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/helpers/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const options = {
disableColors: false
};

const textToFormat = (open, close, searchRegex, replaceValue) => (txt) =>
!options.disableColors
? open +
(~(txt += "").indexOf(close, 4) // skip opening \x1b[
? txt.replace(searchRegex, replaceValue)
: txt) +
close
: txt

const init = (open, close) => {
return textToFormat(
`\x1b[${open}m`,
`\x1b[${close}m`,
new RegExp(`\\x1b\\[${close}m`, "g"),
`\x1b[${open}m`
)
}
const colors = {
// modifiers
reset: init(0, 0),
bold: init(1, 22),
dim: init(2, 22),
italic: init(3, 23),
underline: init(4, 24),

// colors
black: init(30, 39),
red: init(31, 39),
green: init(32, 39),
yellow: init(33, 39),
blue: init(34, 39),
magenta: init(35, 39),
cyan: init(36, 39),
white: init(37, 39),
gray: init(90, 39),
grey: init(90, 39),

// setting options
options: options
}

module.exports=colors
104 changes: 104 additions & 0 deletions src/utils/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const { magenta, blue, green, yellow, red, options } = require('../helpers/colors');
const trm = console;

const LEVEL_VERBOSE = 2;
const LEVEL_TRACE = 3;
const LEVEL_DEBUG = 4;
const LEVEL_INFO = 5;
const LEVEL_WARN = 6;
const LEVEL_ERROR = 7;
const LEVEL_SILENT = 8;

/**
* returns log level value
* @param {string} level - log level
*/
function getLevelValue(level) {
const logLevel = level.toUpperCase();
switch (logLevel) {
case 'TRACE':
return LEVEL_TRACE;
case 'DEBUG':
return LEVEL_DEBUG;
case 'INFO':
return LEVEL_INFO;
case 'WARN':
return LEVEL_WARN;
case 'ERROR':
return LEVEL_ERROR;
case 'SILENT':
return LEVEL_SILENT;
case 'VERBOSE':
return LEVEL_VERBOSE;
default:
return LEVEL_INFO;
}
}

class Logger {

constructor() {
this.level = process.env.TESTBEATS_LOG_LEVEL || 'INFO';
this.levelValue = getLevelValue(this.level);
if (process.env.TESTBEATS_DISABLE_LOG_COLORS === 'true') {
options.disableColors = true;
}
}

/**
* sets log level
* @param {('TRACE'|'DEBUG'|'INFO'|'WARN'|'ERROR')} level - log level
*/
setLevel(level) {
this.level = level;
this.levelValue = getLevelValue(this.level);
}

trace(...msg) {
if (this.levelValue <= LEVEL_TRACE) {
process.stdout.write(`[${magenta('T')}] `);
msg.forEach(m => trm.debug(m));
}
}

debug(...msg) {
if (this.levelValue <= LEVEL_DEBUG) {
process.stdout.write(`[${blue('D')}] `);
msg.forEach(m => trm.debug(m));
}
}

info(...msg) {
if (this.levelValue <= LEVEL_INFO) {
process.stdout.write(`[${green('I')}] `);
msg.forEach(m => trm.info(m));
}
}

warn(...msg) {
if (this.levelValue <= LEVEL_WARN) {
process.stdout.write(`[${yellow('W')}] `);
msg.forEach(m => trm.warn(getMessage(m)));
}
}

error(...msg) {
if (this.levelValue <= LEVEL_ERROR) {
process.stdout.write(`[${red('E')}] `);
msg.forEach(m => trm.error(getMessage(m)));
}
}

}


function getMessage(msg) {
try {
return typeof msg === 'object' ? JSON.stringify(msg, null, 2) : msg;
} catch (_) {
return msg;
}
}


module.exports = new Logger();

0 comments on commit 2915e29

Please sign in to comment.