diff --git a/app.js b/app.js index ae5d11a9b4f5bba51cee64f257bfd3e792b3b582..73f5227370bff0fa4dbca3630d27f68d963edd78 100644 --- a/app.js +++ b/app.js @@ -5,11 +5,8 @@ // System Imports. // User Imports. -const config = require('./src/config.js'); const { userConfirmation } = require('./src/helper_functions.js'); const testLogLevels = require('./src/logging.js'); -const MySql = require('./src/mysql.js'); - /** @@ -27,20 +24,6 @@ async function main() { console.log('Log level tests complete.'); console.log(''); - var debug = true; - - // Create MySql helper class. - const mysql = new MySql(config['mysql'], debug); - mysql.open_conn(); - - // mysql.query(); - // mysql.reset_db(); - mysql.reset_db(); - mysql.close_conn(); - - - - console.log(''); console.info('Terminating program.'); } diff --git a/documents/references.md b/documents/references.md new file mode 100644 index 0000000000000000000000000000000000000000..a6bbdeda46e4f122d9056d600b3e50a9e87c04d8 --- /dev/null +++ b/documents/references.md @@ -0,0 +1,21 @@ +# Custom Logging - Documents > References + + +## Description +Various references used in project. + + +## References + +### Official NodeJS Logging Docs +<https://nodejs.org/docs/latest/api/console.html> + +### More on JavaScript Logging +<https://stackify.com/javascript-logging-basic-tips/> + +### Rework Logging to Output Line #, File Name, and Color Output +<https://stackoverflow.com/a/60305881> + +### Python Logging Levels +Used as reference to help determine values used here.<br> +<https://docs.python.org/3/library/logging.html> diff --git a/package.json b/package.json index f70d9e73bd209f63540648a36a732961dc155112..11ed20a6840db4ea4930acf232fbef14bec777ab 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,13 @@ { - "name": "mysql_bot_example", + "name": "custom_logger", "version": "1.0.0", - "description": "Test NodeJS application to connect to MySQL and make database queries.", + "description": "Custom logging file to be imported into other projects.", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "dependencies": { "chalk": "^4.1.0", - "mysql2": "^2.1.0", "readline-sync": "^1.4.10" } } diff --git a/readme.md b/readme.md index ef42fdea028acae9fa594b9f7a59ed03dfe20b6e..f323cfa3ba5fe4c18ab24e54f864476020d96d4e 100755 --- a/readme.md +++ b/readme.md @@ -1,16 +1,21 @@ -# JavaScript - NodeJS MySql Example +# JavaScript - Custom Logger ## Description -Example of using a NodeJS JavaScript application to connect to MySQL and make database queries. +Custom logging file to be imported into other projects. +Improves logging handling above and beyond JavaScript's default definitions. + +Make sure to also copy `src/helper_functions.js` file, as well as dependencies as defined in `package.json`. -## Program Setup -First, make sure MySQL is installed and setup on your program. Then install NodeJs. -Once that's done, import `<project_root>/src/database_export.sql` to a new database, edit `<project_root>/src/config.js` -to connect to your local MySql instance. +## Program Setup +Install NodeJs. Then run `npm install` at project root to install dependencies. ## Running the Program -Once the program is setup, use a terminal to cd into project root, then run `node app.js`. +Once the program is setup, run `node app.js` from project root. + + +## References +See `documents/references.md` for various references used for project. diff --git a/src/config.js b/src/config.js deleted file mode 100644 index e5e44af27c37bbf75908e62f22438b0a79748b5a..0000000000000000000000000000000000000000 --- a/src/config.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Project config file. - */ - - -var config = { - mysql: { - host: '127.0.0.1', - port: '3306', - database: 'mysql_bot_example', - user: 'root', - password: 'root' - } -} - - -module.exports = config; diff --git a/src/database_export.sql b/src/database_export.sql deleted file mode 100644 index a94f2115fbacc3f0ade948cd079c8f4434a47495..0000000000000000000000000000000000000000 --- a/src/database_export.sql +++ /dev/null @@ -1,74 +0,0 @@ --- MySQL dump 10.13 Distrib 5.7.30, for Linux (x86_64) --- --- Host: localhost Database: test --- ------------------------------------------------------ --- Server version 5.7.30-0ubuntu0.18.04.1 - -/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; -/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; -/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; -/*!40101 SET NAMES utf8 */; -/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; -/*!40103 SET TIME_ZONE='+00:00' */; -/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; -/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; -/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; -/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; - --- --- Table structure for table `message` --- - -DROP TABLE IF EXISTS `message`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `message` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `message` varchar(100) DEFAULT NULL, - `user_id` int(11) NOT NULL, - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=latin1; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `message` --- - -LOCK TABLES `message` WRITE; -/*!40000 ALTER TABLE `message` DISABLE KEYS */; -/*!40000 ALTER TABLE `message` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `user` --- - -DROP TABLE IF EXISTS `user`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `user` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` varchar(100) DEFAULT NULL, - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=latin1; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `user` --- - -LOCK TABLES `user` WRITE; -/*!40000 ALTER TABLE `user` DISABLE KEYS */; -/*!40000 ALTER TABLE `user` ENABLE KEYS */; -UNLOCK TABLES; -/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; - -/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; -/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; -/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; -/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; -/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; -/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; -/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; - --- Dump completed on 2020-07-25 9:53:01 diff --git a/src/logging.js b/src/logging.js index fd91d2b369df9442f4f4fb00a532577b18bfe2f5..de6cbc67dcb15d51e5d52163a1ebfca55f44a3a1 100644 --- a/src/logging.js +++ b/src/logging.js @@ -1,8 +1,12 @@ /** * Custom logging to create more useful output. * Simply include this file at the top of other files to get extra logging logic. + * + * https://git.brandon-rodriguez.com/javascript/custom_logger + * Version 1.0 */ + // System Imports. const chalk = require('chalk'); const fs = require('fs'); @@ -16,8 +20,6 @@ const { getCurrentDate } = require('./helper_functions.js'); * 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]; @@ -175,7 +177,6 @@ console.debug('Logging has been initialized.'); /** * Simple test to make sure all loging levels work as expected. - * Should convert this to a proper unit test at a later date. */ function testLogLevels() { console.debug('Debug level test'); diff --git a/src/mysql.js b/src/mysql.js deleted file mode 100644 index 2a58c01ef247d004d3d7f4a9e170a2abcc60bb50..0000000000000000000000000000000000000000 --- a/src/mysql.js +++ /dev/null @@ -1,121 +0,0 @@ -/** - * Helper class to handle MySql logic. - */ - -// System Imports. -const mysql = require('mysql2'); - -// User Imports. -require('./logging.js'); -const { userConfirmation } = require('./helper_functions.js'); - - -class MySql { - - /** - * Class constructor. - */ - constructor(config, debug) { - if (debug) { - console.debug('Starting MySql helper class.'); - } - - // Save class variables. - this.config = config; - this.debug = debug; - this.connection = null; - } - - /** - * Creates and returns a MySql connection object, using provided config values. - */ - open_conn() { - if (this.debug) { - 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.warn('Connection already exist. Closing old connection first.'); - this.close_conn(); - } - - // Open MySQL connection. - const connection = mysql.createConnection({ - host: this.config['host'], - port: this.config['port'], - database: this.config['database'], - user: this.config['user'], - password: this.config['password'] - }); - console.info('Connection has been created.'); - - // Save connection to class. - this.connection =connection; - - return connection; - } - - /** - * Closes current MySql connection. - */ - close_conn() { - if (this.debug) { - 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.info('Connection has been terminated.'); - } else { - // Connection does not yet exist. Cannot close. - console.warn('There is no connection to close.'); - } - } - - /** - * - */ - async query() { - if (this.debug) { - console.debug('Querying.'); - } - - // this.connection.execute( - // 'INSERT INTO `user` (name) VALUES (?);', - // ['test_name_2'], - // function(err, results, fields) { - // console.info('Results: ' + results); - // console.info('Fields: ' + fields); - // } - // ); - - await this.connection.execute( - 'SHOW TABLES;', - function(err, results, fields) { - console.info('Results: ' + results); - } - ); - } - - /** - * - */ - async reset_db() { - console.warn('Attempting to reset tables.'); - var response = userConfirmation(); - - if (response) { - - this.query(); - // this.connection.execute('TRUNCATE `message`;'); - // this.connection.execute('TRUNCATE `user`;'); - } - } -} - - -module.exports = MySql;