added logging to config api and subscriptions api, meaning the entire backend has the new logging system

pull/45/head
Isaac Grynsztein 5 years ago
parent 2f0bbca15c
commit 3990e25c18

@ -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({

@ -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 = {

@ -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
}

Loading…
Cancel
Save