Merge pull request #2 from Joe-Dowd/logging-change

Added logger util and used in place of console log
This commit is contained in:
Joe Dowd 2020-08-21 21:06:22 +01:00 committed by GitHub
commit c1d4ba3c80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 74 additions and 34 deletions

View file

@ -1,5 +1,6 @@
var MysqlInterface = require('sync-mysql');
var config = require('../config.js');
var logger = require('../utils/logger.js');
class Mysql {
constructor(msConfig) {
@ -11,7 +12,7 @@ class Mysql {
}
prepare (type, query, params) {
(config.mode === "development") && console.log("prepare (mysql): type: " + type + ", query: " + query + ", params: " + params);
logger.debug("prepare (mysql): type: " + type + ", query: " + query + ", params: " + params);
if (type === 'get') {
return this.connection.query(query, params)[0];
} else if (type === 'run') {
@ -19,7 +20,7 @@ class Mysql {
} else if (type === 'all') {
return this.connection.query(query, params);
} else {
console.log('returning undefined...')
logger.warn('returning undefined...');
return undefined;
}
}

View file

@ -1,5 +1,6 @@
const { db } = require("./databases");
var config = require('../config.js');
const logger = require('../utils/logger.js');
class Sqlite {
constructor(connection) {
@ -18,8 +19,8 @@ class Sqlite {
} else if (type === 'all') {
return this.connection.prepare(query).all(...params);
} else {
(config.mode === "development") && console.log('returning undefined...')
(config.mode === "development") && console.log("prepare: type: " + type + ", query: " + query + ", params: " + params);
logger.debug('sqlite query: returning undefined')
logger.debug("prepare: type: " + type + ", query: " + query + ", params: " + params);
return undefined;
}
}

View file

@ -1,7 +1,6 @@
var fs = require('fs');
var config = require('../config.js');
const log = require('../utils/logger.js'); // log not logger to not interfere with function name
module.exports = function logger (req, res, next) {
(config.mode === "development") && console.log('Request recieved: ' + req.url);
log.info('Request recieved: ' + req.url);
next();
}

View file

@ -5,6 +5,7 @@ var databases = require('../databases/databases.js');
var db = databases.db;
var privateDB = databases.privateDB;
var logger = require('../utils/logger.js');
var getHash = require('../utils/getHash.js');
var getIP = require('../utils/getIP.js');
@ -166,7 +167,7 @@ function handleGetSegments(req, res) {
return segments;
} catch (error) {
console.error(error);
logger.error(error);
res.sendStatus(500);
return false;

View file

@ -1,6 +1,7 @@
var db = require('../databases/databases.js').db;
var getHash = require('../utils/getHash.js');
const logger = require('../utils/logger.js');
module.exports = function getUsername (req, res) {
let userID = req.query.userID;
@ -28,7 +29,7 @@ module.exports = function getUsername (req, res) {
});
}
} catch (err) {
console.log(err);
logger.error(err);
res.sendStatus(500);
return;

View file

@ -1,6 +1,6 @@
var db = require('../databases/databases.js').db;
var getHash = require('../utils/getHash.js');
var logger = require('../utils/logger.js');
module.exports = function getViewsForUser(req, res) {
let userID = req.query.userID;
@ -25,7 +25,7 @@ module.exports = function getViewsForUser(req, res) {
res.sendStatus(404);
}
} catch (err) {
console.log(err);
logger.error(err);
res.sendStatus(500);
return;

View file

@ -1,5 +1,3 @@
var config = require('../config.js');
var postSkipSegments = require('./postSkipSegments.js');
module.exports = async function submitSponsorTimes(req, res) {

View file

@ -4,6 +4,7 @@ var databases = require('../databases/databases.js');
var db = databases.db;
var privateDB = databases.privateDB;
var YouTubeAPI = require('../utils/youtubeAPI.js');
var logger = require('../utils/logger.js');
var request = require('request');
var isoDurations = require('iso8601-duration');
@ -25,7 +26,7 @@ function sendDiscordNotification(userID, videoID, UUID, segmentInfo) {
id: videoID
}, function (err, data) {
if (err || data.items.length === 0) {
err && console.log(err);
err && logger.error(err);
return;
}
@ -52,13 +53,13 @@ function sendDiscordNotification(userID, videoID, UUID, segmentInfo) {
}
}, (err, res) => {
if (err) {
console.log("Failed to send first time submission Discord hook.");
console.log(JSON.stringify(err));
console.log("\n");
logger.error("Failed to send first time submission Discord hook.");
logger.error(JSON.stringify(err));
logger.error("\n");
} else if (res && res.statusCode >= 400) {
console.log("Error sending first time submission Discord hook");
console.log(JSON.stringify(res));
console.log("\n");
logger.error("Error sending first time submission Discord hook");
logger.error(JSON.stringify(res));
logger.error("\n");
}
});
});
@ -105,7 +106,7 @@ async function autoModerateSubmission(submission, callback) {
}
} else {
if (config.mode === 'development') console.log("Skipped YouTube API");
logger.debug("Skipped YouTube API");
// Can't moderate the submission without calling the youtube API
// so allow by default.
@ -117,9 +118,9 @@ function proxySubmission(req) {
request.post(config.proxySubmission + '/api/skipSegments?userID='+req.query.userID+'&videoID='+req.query.videoID, {json: req.body}, (err, result) => {
if (config.mode === 'development') {
if (!err) {
console.log('Proxy Submission: ' + result.statusCode + ' ('+result.body+')');
logger.error('Proxy Submission: ' + result.statusCode + ' ('+result.body+')');
} else {
console.log("Proxy Submission: Failed to make call");
logger.debug("Proxy Submission: Failed to make call");
}
}
});
@ -264,7 +265,7 @@ module.exports = async function postSkipSegments(req, res) {
} catch (err) {
//a DB change probably occurred
res.sendStatus(502);
console.log("Error when putting sponsorTime in the DB: " + videoID + ", " + segmentInfo.segment[0] + ", " +
logger.error("Error when putting sponsorTime in the DB: " + videoID + ", " + segmentInfo.segment[0] + ", " +
segmentInfo.segment[1] + ", " + userID + ", " + segmentInfo.category + ". " + err);
return;
@ -274,7 +275,7 @@ module.exports = async function postSkipSegments(req, res) {
sendDiscordNotification(userID, videoID, UUID, segmentInfo);
}
} catch (err) {
console.error(err);
logger.error(err);
res.sendStatus(500);

View file

@ -3,6 +3,7 @@ var config = require('../config.js');
var db = require('../databases/databases.js').db;
var getHash = require('../utils/getHash.js');
const logger = require('../utils/logger.js');
module.exports = function setUsername(req, res) {
@ -45,7 +46,7 @@ module.exports = function setUsername(req, res) {
res.sendStatus(200);
} catch (err) {
console.log(err);
logger.error(err);
res.sendStatus(500);
return;

View file

@ -11,6 +11,7 @@ var db = databases.db;
var privateDB = databases.privateDB;
var YouTubeAPI = require('../utils/youtubeAPI.js');
var request = require('request');
const logger = require('../utils/logger.js');
function categoryVote(UUID, userID, isVIP, category, hashedIP, res) {
// Check if they've already made a vote
@ -203,7 +204,7 @@ async function voteOnSponsorTime(req, res) {
id: submissionInfoRow.videoID
}, function (err, data) {
if (err || data.items.length === 0) {
err && console.log(err);
err && logger.error(err);
return;
}
@ -232,13 +233,13 @@ async function voteOnSponsorTime(req, res) {
}
}, (err, res) => {
if (err) {
console.log("Failed to send reported submission Discord hook.");
console.log(JSON.stringify(err));
console.log("\n");
logger.error("Failed to send reported submission Discord hook.");
logger.error(JSON.stringify(err));
logger.error("\n");
} else if (res && res.statusCode >= 400) {
console.log("Error sending reported submission Discord hook");
console.log(JSON.stringify(res));
console.log("\n");
logger.error("Error sending reported submission Discord hook");
logger.error(JSON.stringify(res));
logger.error("\n");
}
});
});
@ -299,7 +300,7 @@ async function voteOnSponsorTime(req, res) {
res.sendStatus(200);
} catch (err) {
console.error(err);
logger.error(err);
res.status(500).json({error: 'Internal error creating segment vote'});
}

36
src/utils/logger.js Normal file
View file

@ -0,0 +1,36 @@
const config = require('../config.js');
const levels = {
ERROR: "ERROR",
WARN: "WARN",
INFO: "INFO",
DEBUG: "DEBUG"
};
const settings = {
ERROR: true,
WARN: true,
INFO: false,
DEBUG: false
};
if (config.mode === 'development') {
settings.INFO = true;
settings.DEBUG = true;
}
function log(level, string) {
if (!!settings[level]) {
if (level.length === 4) {level = level + " "}; // ensure logs are aligned
console.log(level + " " + new Date().toISOString() + " : " + string);
}
}
module.exports = {
levels,
log,
error: (string) => {log(levels.ERROR, string)},
warn: (string) => {log(levels.WARN, string)},
info: (string) => {log(levels.INFO, string)},
debug: (string) => {log(levels.DEBUG, string)},
};