Skip to content
Snippets Groups Projects
Commit 0ceb493a authored by Brandon Rodriguez's avatar Brandon Rodriguez
Browse files

Improve logging output to be more useful

parent 19712a5f
Branches
No related merge requests found
......@@ -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.');
}
......
......@@ -7,6 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"chalk": "^4.1.0",
"mysql2": "^2.1.0"
}
}
/**
* 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);
}
};
});
......@@ -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.');
}
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment