Skip to content

Commit

Permalink
139064616 Console log standardization
Browse files Browse the repository at this point in the history
Pass the component name to core function writeConsoleLog.
  • Loading branch information
gaonkar18y committed Aug 9, 2019
1 parent 670c94f commit 8d97a77
Show file tree
Hide file tree
Showing 25 changed files with 315 additions and 277 deletions.
17 changes: 9 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var portastic = require('portastic');
const os = require('os');
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;
const options = {};
const CONSOLE_LOG_TAG_COMP = 'microgateway app';

options.env = process.env.EDGEMICRO_ENV;
options.key = process.env.EDGEMICRO_KEY;
Expand All @@ -24,15 +25,15 @@ options.target = process.env.EDGEMICRO_API_TARGET;

options.port = process.env.PORT || 8000;

if (!options.key ) { writeConsoleLog('log','key is required'); process.exit(1);}
if (!options.secret ) { writeConsoleLog('log','secret is required'); process.exit(1);}
if (!options.org ) { writeConsoleLog('log','org is required'); process.exit(1);}
if (!options.env ) { writeConsoleLog('log','env is required'); process.exit(1);}
if (!options.key ) { writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'key is required'); process.exit(1);}
if (!options.secret ) { writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'secret is required'); process.exit(1);}
if (!options.org ) { writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'org is required'); process.exit(1);}
if (!options.env ) { writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'env is required'); process.exit(1);}
if (options.port) {
portastic.test(options.port)
.then(function(isAvailable){
if(!isAvailable) {
writeConsoleLog('error','port is not available.');
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'port is not available.');
process.exit(1);
}
});
Expand All @@ -46,19 +47,19 @@ if (options.configUrl) {
if (parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:") {
request.get(options.configUrl, function(error, response, body) {
if (error) {
writeConsoleLog('error',"config file did not download: "+error);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file did not download: "+error);
process.exit(1);
}
try {
fs.writeFileSync(filePath, body, 'utf8');
run.start(options);
} catch (err) {
writeConsoleLog('error',"config file could not be written: " + err);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file could not be written: " + err);
process.exit(1);
}
});
} else {
writeConsoleLog('error',"url protocol not supported: "+parsedUrl.protocol);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"url protocol not supported: "+parsedUrl.protocol);
process.exit(1);
}
} else {
Expand Down
4 changes: 3 additions & 1 deletion cli/cmd-cert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const cert = require('./lib/cert')();
const prompt = require('cli-prompt');
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;

const CONSOLE_LOG_TAG_COMP = 'microgateway cmd cert';

const setup = function setup() {

commander
Expand Down Expand Up @@ -113,7 +115,7 @@ const setup = function setup() {
function optionError(caller) {
return(((obj) => {
return((message) => {
writeConsoleLog('error',message);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message);
obj.help();
});
})(caller))
Expand Down
4 changes: 3 additions & 1 deletion cli/cmd-private.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const upgradeauth = require('./lib/upgrade-edgeauth')();
const rotatekey = require('./lib/rotate-key')();
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;

const CONSOLE_LOG_TAG_COMP = 'microgateway cmd private';

var prompt = require('cli-prompt');

module.exports = function() {
Expand Down Expand Up @@ -233,7 +235,7 @@ function promptForPassword(options, cb) {
function optionError(caller) {
return(((obj) => {
return((message) => {
writeConsoleLog('error',message);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message);
obj.help();
});
})(caller))
Expand Down
4 changes: 3 additions & 1 deletion cli/cmd-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const commander = require('commander');
const token = require('./lib/token')();
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;

const CONSOLE_LOG_TAG_COMP = 'microgateway cmd token';

const setup = function setup() {

commander
Expand Down Expand Up @@ -63,7 +65,7 @@ const setup = function setup() {
function optionError(caller) {
return(((obj) => {
return((message) => {
writeConsoleLog('error',message);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message);
obj.help();
});
})(caller))
Expand Down
24 changes: 13 additions & 11 deletions cli/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const pidpath = configLocations.getPIDFilePath();
var portastic = require('portastic');
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;

const CONSOLE_LOG_TAG_COMP = 'microgateway cmd';

const setup = function setup() {
commander
.version(require('../package.json').version);
Expand Down Expand Up @@ -97,7 +99,7 @@ const setup = function setup() {
.action((options) => {
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
init(options, (err, location) => {
writeConsoleLog('log',"config initialized to %s", location)
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"config initialized to %s", location)
})
});

Expand Down Expand Up @@ -224,20 +226,20 @@ const setup = function setup() {
debug("downloading file...");
request.get(options.configUrl, function(error, response, body) {
if (error) {
writeConsoleLog('error',"config file did not download: " + error);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file did not download: " + error);
process.exit(1);
}
try {
debug(body);
fs.writeFileSync(filePath, body, 'utf8');
run.start(options);
} catch (err) {
writeConsoleLog('error',"config file could not be written: " + err);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file could not be written: " + err);
process.exit(1);
}
});
} else {
writeConsoleLog('error',"url protocol not supported: " + parsedUrl.protocol);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"url protocol not supported: " + parsedUrl.protocol);
process.exit(1);
}
} else {
Expand Down Expand Up @@ -288,15 +290,15 @@ const setup = function setup() {
debug("downloading file...");
request.get(options.configUrl, function(error, response, body) {
if (error) {
writeConsoleLog('error',"config file did not download: " + error);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file did not download: " + error);
process.exit(1);
}
try {
debug(body);
fs.writeFileSync(filePath, body, 'utf8');
run.reload(options);
} catch (err) {
writeConsoleLog('error',"config file could not be written: " + err);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"config file could not be written: " + err);
process.exit(1);
}
});
Expand Down Expand Up @@ -350,7 +352,7 @@ const setup = function setup() {
fs.appendFileSync(pidpath, process.pid + '|');
child.start();
} catch (piderr) {
writeConsoleLog('error','failed to start microgateway: ' + piderr);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'failed to start microgateway: ' + piderr);
process.exit(1);
}
} else {
Expand All @@ -362,10 +364,10 @@ const setup = function setup() {
});
fs.unlinkSync(pidpath);
} else {
writeConsoleLog('log','pid file not found. please run this command from the folder where microgateway was started.')
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'pid file not found. please run this command from the folder where microgateway was started.')
}
} catch (piderr) {
writeConsoleLog('error','failed to stop microgateway: ' + piderr);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'failed to stop microgateway: ' + piderr);
process.exit(1);
}
}
Expand Down Expand Up @@ -591,7 +593,7 @@ const setup = function setup() {
function optionError(caller) {
return(((obj) => {
return((message) => {
writeConsoleLog('error',message);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message);
obj.help();
});
})(caller))
Expand All @@ -616,7 +618,7 @@ function validateUrl(target) {
url.parse(target, true);
return true;
} catch (err) {
writeConsoleLog('error',"Malformed URL: " + err);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"Malformed URL: " + err);
return false;
}
}
Expand Down
9 changes: 5 additions & 4 deletions cli/edgemicro
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,25 @@ const ltsVersion = 8;
const experimental = ["v7", "v9","v11"];
const latest = 12;

const CONSOLE_LOG_TAG_COMP = 'microgateway edgemicro';

//don't print versions when obtaining a token.
if (!process.argv.includes("token") && !process.argv.includes("get")) {
writeConsoleLog('log','current nodejs version is %s', version);
writeConsoleLog('log','current edgemicro version is %s', edgemicroVersion);
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'current nodejs version is %s', version);
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'current edgemicro version is %s', edgemicroVersion);
}

for (var ver in experimental) {
if (version.includes(experimental[ver])) {
writeConsoleLog('log',"You are using a version of NodeJS that is not supported");
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"You are using a version of NodeJS that is not supported");
return;
}
}

var majorVersion = parseInt(version.split(".")[0].replace("v", ""));

if (majorVersion < ltsVersion || majorVersion > latest) {
writeConsoleLog('log',"You are using a version of NodeJS that is not supported");
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},"You are using a version of NodeJS that is not supported");
return;
} else {
require('./cmd')()
Expand Down
29 changes: 15 additions & 14 deletions cli/lib/cert-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const debug = require('debug')('cert')
//const prompt = require('cli-prompt');
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;

const CONSOLE_LOG_TAG_COMP = 'microgateway cert lib';

const ERR_STORE_EXISTS = 'com.apigee.secure-store.storekey.already.exists';
const ERR_STORE_MISSING = 'com.apigee.secure-store.securestore_does_not_exist';
//const ERR_STORE_ITEM_MISSING = 'com.apigee.secure-store.storeitem_does_not_exist';
Expand Down Expand Up @@ -94,7 +96,7 @@ CertLogic.prototype.installPrivateCert = function(options, callback) {
if (callback) {
return callback(err);
} else {
return writeConsoleLog('log',err, err.stack);
return writeConsoleLog('log', {component: CONSOLE_LOG_TAG_COMP},err, err.stack);
}
}

Expand All @@ -112,9 +114,9 @@ CertLogic.prototype.installPrivateCert = function(options, callback) {
deleteVault(generateCredentialsObject(options), managementUri, options.org, options.env, vaultName, cb);
},
function(cb) {
writeConsoleLog('log','creating KVM');
writeConsoleLog('log','adding private_key');
writeConsoleLog('log','adding public_key');
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'creating KVM');
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'adding private_key');
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'adding public_key');
var entries = [{
'name': 'private_key',
'value': privateKey
Expand Down Expand Up @@ -246,15 +248,15 @@ CertLogic.prototype.generateKeysWithPassword = function generateKeysWithPassword
if (res.statusCode >= 200 && res.statusCode <= 202) {
if (!res.body.region || !res.body.host) {
if (cb) {
cb(writeConsoleLog('error','invalid response from region api', regionUrl, res.text));
cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'invalid response from region api', regionUrl, res.text));
} else {
writeConsoleLog('error','invalid response from region api', regionUrl, res.text);
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'invalid response from region api', regionUrl, res.text);
}

return;
}

writeConsoleLog('log','configuring host', res.body.host, 'for region', res.body.region);
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'configuring host', res.body.host, 'for region', res.body.region);
var bootstrapUrl = util.format(managementUri, 'bootstrap', options.org, options.env);
var parsedUrl = url.parse(bootstrapUrl);
parsedUrl.host = res.body.host; // update to regional host
Expand All @@ -268,11 +270,11 @@ CertLogic.prototype.generateKeysWithPassword = function generateKeysWithPassword


} else {
cb(writeConsoleLog('error','error retrieving region for org', res.statusCode, res.text));
cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error retrieving region for org', res.statusCode, res.text));
}
});
} else {
cb(writeConsoleLog('error','error uploading credentials', res.statusCode, res.text));
cb(writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'error uploading credentials', res.statusCode, res.text));
}
});
});
Expand Down Expand Up @@ -312,7 +314,7 @@ function createCert(cb) {
}

function deleteVault(credentials, managementUri, organization, environment, vaultName, cb) {
writeConsoleLog('log','deleting KVM');
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'deleting KVM');

var uri = util.format('%s/v1/organizations/%s/environments/%s/keyvaluemaps/%s', managementUri, organization, environment, vaultName);

Expand All @@ -330,7 +332,6 @@ function deleteVault(credentials, managementUri, organization, environment, vaul
});



}

function createVault(credentials, managementUri, organization, environment, vaultName, entries, cb) {
Expand Down Expand Up @@ -430,9 +431,9 @@ function uploadCert(options, managementUri, vaultName, privateKey, publicKey, ca
deleteVault(generateCredentialsObject(options), managementUri, options.org, options.env, vaultName, cb);
},
function(cb) {
writeConsoleLog('log','creating KVM');
writeConsoleLog('log','adding private_key');
writeConsoleLog('log','adding public_key');
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'creating KVM');
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'adding private_key');
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'adding public_key');
var entries = [{
'name': 'private_key',
'value': privateKey
Expand Down
Loading

0 comments on commit 8d97a77

Please sign in to comment.