From 5c88219e367a75ff01928d13fff10fe8ac885bc2 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 16 Dec 2020 01:24:43 -0500 Subject: [PATCH] Update logging to handle a little bit better --- src/logging.js | 64 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/src/logging.js b/src/logging.js index de6cbc6..d6b9d9d 100644 --- a/src/logging.js +++ b/src/logging.js @@ -20,9 +20,24 @@ 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. + * + * 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. @@ -75,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. @@ -89,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); @@ -111,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 => { @@ -172,6 +176,31 @@ 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.'); @@ -182,6 +211,7 @@ 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.'); } -- GitLab