From 0ceb493afc4a7dee0dc9e2cc2bf546dbfe431b5e Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Fri, 31 Jul 2020 20:00:52 -0400 Subject: [PATCH] Improve logging output to be more useful --- app.js | 14 ++++++++++- package.json | 1 + src/logging.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/mysql.js | 17 ++++++++------ 4 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 src/logging.js diff --git a/app.js b/app.js index e0d8d69..04b89c1 100644 --- a/app.js +++ b/app.js @@ -3,11 +3,11 @@ */ // System Imports. -// const mysql = require('mysql2'); // User Imports. const config = require('./src/config.js'); const MySql = require('./src/mysql.js'); +require('./src/logging.js'); /** @@ -24,6 +24,18 @@ function main() { mysql.open_conn(); mysql.close_conn(); + console.log(''); + console.log(''); + + + // Test output. + console.debug('Debug level test'); + console.info('Info level test.'); + console.warn('Warn level test.'); + console.error('Error level test.'); + console.log('Log/default level test.'); + + console.log(''); console.log('Terminating program.'); } diff --git a/package.json b/package.json index a15068b..c7af6d5 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "dependencies": { + "chalk": "^4.1.0", "mysql2": "^2.1.0" } } diff --git a/src/logging.js b/src/logging.js new file mode 100644 index 0000000..3ed9d90 --- /dev/null +++ b/src/logging.js @@ -0,0 +1,63 @@ +/** + * Custom logging to create more useful output. + */ + +// System Imports. +const chalk = require('chalk'); +const path = require('path'); + + +/** + * Essentially "upgrades" default Javascript console output to be more informative. + * Adds DEBUG, INFO, WARNING, and ERROR output levels. + * These all display file and line number before statement. + * + * General logic from https://stackoverflow.com/a/60305881 + */ +['debug', 'info', 'warning', 'warn', 'error', 'err', 'console', 'log'].forEach((methodName) => { + let originalLoggingMethod = console[methodName]; + console[methodName] = (firstArgument, ...otherArguments) => { + + // Magic to get our line num and file name. + let originalPrepareStackTrace = Error.prepareStackTrace; + Error.prepareStackTrace = (_, stack) => stack; + let callee = new Error().stack[1]; + Error.prepareStackTrace = originalPrepareStackTrace; + let relativeFileName = path.relative(process.cwd(), callee.getFileName()); + + + // This is where we actually prepend to our provided log text. + if (methodName == 'debug') { + // Prepend text for DEBUG level. + var prefix = ` ${chalk.green('[DEBUG]')} [${relativeFileName} ${callee.getLineNumber()}] `; + + } else if (methodName == 'info') { + // Prepend text for INFO level. + var prefix = ` ${chalk.blue('[INFO]')} [${relativeFileName} ${callee.getLineNumber()}] `; + + } else if (methodName == 'warning' || methodName == 'warn') { + // Prepend text for WARNING level. + var prefix = ` ${chalk.yellow('[WARN]')} [${relativeFileName} ${callee.getLineNumber()}] `; + + } else if (methodName == 'error' || methodName == 'err') { + // Prepend text for ERROR level. + var prefix = ` ${chalk.red('[ERROR]')} [${relativeFileName} ${callee.getLineNumber()}] `; + + } else if (methodName == 'log') { + // LOG level does not get prepend text. + var prefix = ' '; + + } else { + // Fallback prepend text for all other cases. + var prefix = ` [${methodName}] [${relativeFileName} ${callee.getLineNumber()}] `; + } + + + // More magic to generate the console output. + if (typeof firstArgument === 'string') { + originalLoggingMethod(prefix + firstArgument, ...otherArguments); + } else { + originalLoggingMethod(prefix, firstArgument, ...otherArguments); + } + }; +}); diff --git a/src/mysql.js b/src/mysql.js index 52de956..f33ac7f 100644 --- a/src/mysql.js +++ b/src/mysql.js @@ -5,6 +5,9 @@ // System Imports. const mysql = require('mysql2'); +// User Imports. +require('./logging.js'); + class MySql { @@ -13,7 +16,7 @@ class MySql { */ constructor(config, debug) { if (debug) { - console.log('Starting MySql helper class.'); + console.debug('Starting MySql helper class.'); } // Save class variables. @@ -27,13 +30,13 @@ class MySql { */ open_conn() { if (this.debug) { - console.log('Creating MySql connection...'); + console.debug('Creating MySql connection...'); } // Check if connection is currently open. if (this.connection != null) { // Connection currently exists. Close so we can start a new one. - console.log('Connection already exist. Closing old connection first.'); + console.warn('Connection already exist. Closing old connection first.'); this.close_conn(); } @@ -45,7 +48,7 @@ class MySql { user: this.config['user'], password: this.config['password'] }); - console.log('Connection has been created.'); + console.info('Connection has been created.'); // Save connection to class. this.connection =connection; @@ -58,17 +61,17 @@ class MySql { */ close_conn() { if (this.debug) { - console.log('Closing MySql connection...'); + console.debug('Closing MySql connection...'); } // Check if connection is currently open. if (this.connection != null) { // Connection currently exists. Safe to close. this.connection.end(); - console.log('Connection has been terminated.'); + console.info('Connection has been terminated.'); } else { // Connection does not yet exist. Cannot close. - console.log('There is no connection to close.'); + console.warn('There is no connection to close.'); } } } -- GitLab