From 28afd0d7d79abe630c864c7ddc71aa4888fd810d Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Thu, 17 Dec 2020 20:07:15 -0500 Subject: [PATCH] Update logging class to be a singleton Should prevent having large numbers of identical file descriptors, when calling logging from multiple files. --- app.js | 2 +- src/logging.js | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index 7370194..bc1584e 100644 --- a/app.js +++ b/app.js @@ -10,7 +10,7 @@ const { Logging, testLogLevels } = require('./src/logging.js'); // Initialize logging. -let logger = new Logging('./logging/'); +let logger = Logging.init('./logging/'); /** diff --git a/src/logging.js b/src/logging.js index eaff4dd..d1e07ae 100644 --- a/src/logging.js +++ b/src/logging.js @@ -18,9 +18,36 @@ const { getCurrentDate } = require('./helper_functions.js'); /** - * Logging class. + * "Fake" logging class used exclusively to initialize our real logging class. + * Helps keep our real singleton class private. + * + * This is necessary because JavaScript doesn't seem to allow constructor overloading or private constructors. */ class Logging { + + /** + * Class Constructor. + */ + constructor() { + throw new Error('Initialize class with "Logging.init()".'); + } + + /** + * Method to initialize our singleton class. + */ + static init(logging_dir) { + if ( ! Logging.instance ) { + Logging.instance = new LoggingClass(logging_dir); + } + return Logging.instance; + } +} + + +/** + * Real logging class. + */ +class LoggingClass { test_var = 'test_var string'; #console_debug = 'debug test'; #debug_file_logger; @@ -234,7 +261,6 @@ function testLogLevels(logger) { logger.warning('WARNING level test.'); logger.error('ERROR level test.'); logger.log('Log/default level test.'); - } -- GitLab