diff --git a/src/logging.js b/src/logging.js index fd91d2b369df9442f4f4fb00a532577b18bfe2f5..d6b9d9d4b8ba32805cfd1a5e4c5a4c3663a1c07a 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'); @@ -17,10 +21,23 @@ const { getCurrentDate } = require('./helper_functions.js'); * 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 + * The LOG output level effectively stays as-is, using JavaScripts default logging implementation. */ -['debug', 'info', 'warning', 'warn', 'error', 'err', 'console', 'log'].forEach((methodName) => { - let originalLoggingMethod = console[methodName]; +['debug', 'info', 'warning', 'warn', 'error', 'log'].forEach((methodName) => { + + // Parse log method value. + let originalLoggingMethod = null; + + // Correct "WARNING" output level value. + if (methodName == 'warning') { + originalLoggingMethod = console['warn']; + + } else { + // Leave all other values as-is. + originalLoggingMethod = console[methodName]; + } + + console[methodName] = (firstArgument, ...otherArguments) => { // Magic to get our line num and file name. @@ -73,7 +90,7 @@ const { getCurrentDate } = require('./helper_functions.js'); log_string = prefix.concat(firstArgument, ...otherArguments, '\n'); logToFile(log_string, 'warn'); - } else if (methodName == 'error' || methodName == 'err') { + } else if (methodName == 'error') { // Prepend text for ERROR level. // Generate and print Console text. @@ -87,7 +104,8 @@ const { getCurrentDate } = require('./helper_functions.js'); logToFile(log_string, 'error'); } else if (methodName == 'log') { - // LOG level does not get prepend text. + // LOG level does not get prepend text. Nor does it save to file. + // This is basically intended for "console only" output. prefix = ' '; console_string = prefix.concat(firstArgument, ...otherArguments); originalLoggingMethod(console_string); @@ -109,19 +127,7 @@ const { getCurrentDate } = require('./helper_functions.js'); function logToFile(log_string, level) { let directory = './logging/'; let file_location = './logging/info.log'; - let log_level = 0; - - // Get appropriate level of logger. Based off of Python log levels. - // https://docs.python.org/3/library/logging.html - if (level == 'error') { - log_level = 40; - } else if (level == 'warn') { - log_level = 30; - } else if (level == 'info') { - log_level = 20; - } else if (level == 'debug') { - log_level = 10; - } + let log_level = getLogLevelNum(level); // Attempt to create logging folder. fs.mkdir(directory, err => { @@ -170,17 +176,42 @@ function logToFile(log_string, level) { } } + +/** + * Get appropriate level of logger. Based off of Python log levels. + * https://docs.python.org/3/library/logging.html + */ +function getLogLevelNum(level) { + + if (level == 'error') { + return 40; + + } else if (level == 'warn') { + return 30; + + } else if (level == 'info') { + return 20; + + } else if (level == 'debug') { + return 10; + + } else { + return 0; + } +} + + 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'); console.info('Info level test.'); console.warn('Warn level test.'); + console.warning('Warning level test.'); console.error('Error level test.'); console.log('Log/default level test.'); }