From 3990e25c187e59f710d45b5fab99870505ee40d7 Mon Sep 17 00:00:00 2001 From: Isaac Grynsztein Date: Thu, 2 Apr 2020 23:14:07 -0400 Subject: [PATCH] added logging to config api and subscriptions api, meaning the entire backend has the new logging system --- backend/app.js | 5 ++++- backend/config.js | 12 ++++++++---- backend/subscriptions.js | 24 +++++++++++++----------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/backend/app.js b/backend/app.js index 314cd20..46995ff 100644 --- a/backend/app.js +++ b/backend/app.js @@ -53,6 +53,9 @@ const logger = winston.createLogger({ ] }); +config_api.setLogger(logger); +subscriptions_api.setLogger(logger); + // var GithubContent = require('github-content'); // Set some defaults @@ -1630,7 +1633,7 @@ app.post('/api/updatePlaylist', async (req, res) => { logger.info(new_val);*/ success = true; } catch(e) { - console.error(`Failed to find playlist with ID ${playlistID}`); + logger.error(`Failed to find playlist with ID ${playlistID}`); } res.send({ diff --git a/backend/config.js b/backend/config.js index 33d425d..025bbc9 100644 --- a/backend/config.js +++ b/backend/config.js @@ -5,6 +5,9 @@ const debugMode = process.env.YTDL_MODE === 'debug'; let configPath = debugMode ? '../src/assets/default.json' : 'appdata/default.json'; +var logger = null; +function setLogger(input_logger) { logger = input_logger; } + // https://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key Object.byString = function(o, s) { s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties @@ -51,7 +54,7 @@ function getConfigFile() { let parsed_data = JSON.parse(raw_data); return parsed_data; } catch(e) { - console.log('ERROR: Failed to get config file'); + logger.error('Failed to get config file'); return null; } } @@ -68,13 +71,13 @@ function setConfigFile(config) { function getConfigItem(key) { let config_json = getConfigFile(); if (!CONFIG_ITEMS[key]) { - console.log(`ERROR: Config item with key '${key}' is not recognized.`); + logger.error(`Config item with key '${key}' is not recognized.`); return null; } let path = CONFIG_ITEMS[key]['path']; const val = Object.byString(config_json, path); if (val === undefined && Object.byString(DEFAULT_CONFIG, path)) { - console.log(`WARNING: Cannot find config with key '${key}'. Creating one with the default value...`); + logger.warning(`Cannot find config with key '${key}'. Creating one with the default value...`); setConfigItem(key, Object.byString(DEFAULT_CONFIG, path)); return Object.byString(DEFAULT_CONFIG, path); } @@ -130,7 +133,8 @@ module.exports = { getConfigFile: getConfigFile, setConfigFile: setConfigFile, configExistsCheck: configExistsCheck, - CONFIG_ITEMS: CONFIG_ITEMS + CONFIG_ITEMS: CONFIG_ITEMS, + setLogger: setLogger } DEFAULT_CONFIG = { diff --git a/backend/subscriptions.js b/backend/subscriptions.js index d324d74..416b7c1 100644 --- a/backend/subscriptions.js +++ b/backend/subscriptions.js @@ -13,6 +13,9 @@ const db = low(adapter) const debugMode = process.env.YTDL_MODE === 'debug'; +var logger = null; +function setLogger(input_logger) { logger = input_logger; } + async function subscribe(sub) { const result_obj = { success: false, @@ -23,7 +26,7 @@ async function subscribe(sub) { sub.isPlaylist = sub.url.includes('playlist'); if (db.get('subscriptions').find({url: sub.url}).value()) { - console.log('Sub already exists'); + logger.info('Sub already exists'); result_obj.error = 'Subcription with URL ' + sub.url + ' already exists!'; resolve(result_obj); return; @@ -48,14 +51,14 @@ async function getSubscriptionInfo(sub) { let downloadConfig = ['--dump-json', '--playlist-end', '1'] youtubedl.exec(sub.url, downloadConfig, {}, function(err, output) { if (debugMode) { - console.log('Subscribe: got info for subscription ' + sub.id); + logger.info('Subscribe: got info for subscription ' + sub.id); } if (err) { - console.log(err.stderr); + logger.error(err.stderr); resolve(false); } else if (output) { if (output.length === 0 || (output.length === 1 && output[0] === '')) { - if (debugMode) console.log('Could not get info for ' + sub.id); + logger.verbose('Could not get info for ' + sub.id); resolve(false); } for (let i = 0; i < output.length; i++) { @@ -213,15 +216,13 @@ async function getVideosForSub(sub) { // get videos youtubedl.exec(sub.url, downloadConfig, {}, function(err, output) { - if (debugMode) { - console.log('Subscribe: got videos for subscription ' + sub.name); - } + logger.verbose('Subscribe: got videos for subscription ' + sub.name); if (err) { - console.log(err.stderr); + logger.error(err.stderr); resolve(false); } else if (output) { if (output.length === 0 || (output.length === 1 && output[0] === '')) { - if (debugMode) console.log('No additional videos to download for ' + sub.name); + logger.debug('No additional videos to download for ' + sub.name); resolve(true); } for (let i = 0; i < output.length; i++) { @@ -281,7 +282,7 @@ const deleteFolderRecursive = function(folder_to_delete) { function removeIDFromArchive(archive_path, id) { let data = fs.readFileSync(archive_path, {encoding: 'utf-8'}); if (!data) { - console.log('Archive could not be found.'); + logger.error('Archive could not be found.'); return; } @@ -312,5 +313,6 @@ module.exports = { unsubscribe : unsubscribe, deleteSubscriptionFile : deleteSubscriptionFile, getVideosForSub : getVideosForSub, - removeIDFromArchive : removeIDFromArchive + removeIDFromArchive : removeIDFromArchive, + setLogger : setLogger }