add log level as a cli arg

This commit is contained in:
Alec
2021-11-17 00:59:11 -05:00
parent b6edc941fc
commit b85ee7dcfc
6 changed files with 57 additions and 20 deletions

View File

@@ -164,11 +164,11 @@ function handleRequestForGameState(namespace, logger, gameRunner, accessCode, pe
}
if (matchingPerson) {
if (matchingPerson.socketId === socket.id) {
logger.debug("matching person found with an established connection to the room: " + matchingPerson.name);
logger.trace("matching person found with an established connection to the room: " + matchingPerson.name);
ackFn(GameStateCurator.getGameStateFromPerspectiveOfPerson(game, matchingPerson));
} else {
if (!roomContainsSocketOfMatchingPerson(namespace, matchingPerson, logger, accessCode)) {
logger.debug("matching person found with a new connection to the room: " + matchingPerson.name);
logger.trace("matching person found with a new connection to the room: " + matchingPerson.name);
socket.join(accessCode);
matchingPerson.socketId = socket.id;
ackFn(GameStateCurator.getGameStateFromPerspectiveOfPerson(game, matchingPerson));
@@ -179,14 +179,14 @@ function handleRequestForGameState(namespace, logger, gameRunner, accessCode, pe
} else {
let personWithMatchingSocketId = findPersonWithMatchingSocketId(game.people, socket.id);
if (personWithMatchingSocketId) {
logger.debug("matching person found whose cookie got cleared after establishing a connection to the room: " + personWithMatchingSocketId.name);
logger.trace("matching person found whose cookie got cleared after establishing a connection to the room: " + personWithMatchingSocketId.name);
ackFn(GameStateCurator.getGameStateFromPerspectiveOfPerson(game, personWithMatchingSocketId));
} else {
let unassignedPerson = game.moderator.assigned === false
? game.moderator
: game.people.find((person) => person.assigned === false);
if (unassignedPerson) {
logger.debug("completely new person with a first connection to the room: " + unassignedPerson.name);
logger.trace("completely new person with a first connection to the room: " + unassignedPerson.name);
socket.join(accessCode);
unassignedPerson.assigned = true;
unassignedPerson.socketId = socket.id;

View File

@@ -1,26 +1,44 @@
module.exports = function (debugMode = false) {
const globals = require('../config/globals');
module.exports = function (logLevel = globals.LOG_LEVEL.INFO) {
return {
log (message = '') {
const now = new Date();
console.log('LOG ', now.toGMTString(), ': ', message);
},
warn (message = '') {
if (logLevel === globals.LOG_LEVEL.INFO) return;
const now = new Date();
console.error('WARN ', now.toGMTString(), ': ', message);
},
debug (message = '') {
if (!debugMode) return;
if (logLevel === globals.LOG_LEVEL.INFO || logLevel === globals.LOG_LEVEL.WARN) return;
const now = new Date();
console.debug('DEBUG ', now.toGMTString(), ': ', message);
},
error (message = '') {
if (!debugMode) return;
if (
logLevel === globals.LOG_LEVEL.INFO
|| logLevel === globals.LOG_LEVEL.WARN
|| logLevel === globals.LOG_LEVEL.DEBUG
) { return; }
const now = new Date();
console.error('ERROR ', now.toGMTString(), ': ', message);
},
warn (message = '') {
if (!debugMode) return;
trace(message = '') {
if (
logLevel === globals.LOG_LEVEL.INFO
|| logLevel === globals.LOG_LEVEL.WARN
|| logLevel === globals.LOG_LEVEL.DEBUG
|| logLevel === globals.LOG_LEVEL.ERROR
) { return; }
const now = new Date();
console.error('WARNING ', now.toGMTString(), ': ', message);
console.error('TRACE ', now.toGMTString(), ': ', message);
}
};
};