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

@@ -24,6 +24,13 @@ const globals = {
ENVIRONMENT: {
LOCAL: "local",
PRODUCTION: "production"
},
LOG_LEVEL: {
INFO: "info",
DEBUG: "debug",
ERROR: "error",
WARN: "warn",
TRACE: "trace"
}
};

View File

@@ -15,23 +15,30 @@ app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
let main, environment;
const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('debug');
const localServer = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('local');
const useHttps = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('https');
const port = process.env.PORT || Array
.from(process.argv.map((arg) => {
return arg.trim().toLowerCase();
}))
let args = Array.from(process.argv.map((arg) => arg.trim().toLowerCase()));
const localServer = args.includes('local');
const useHttps = args.includes('https');
const port = process.env.PORT || args
.filter((arg) => {
return /port=\d+/.test(arg);
})
.map((arg) => {
return /port=(\d+)/.exec(arg)[1];
})[0] || 5000;
const logger = require('./modules/Logger')(debugMode);
const logLevel = process.env.LOG_LEVEL || args
.filter((arg) => {
return /loglevel=[a-zA-Z]+/.test(arg);
})
.map((arg) => {
return /loglevel=([a-zA-Z]+)/.exec(arg)[1];
})[0] || globals.LOG_LEVEL.INFO;
const logger = require('./modules/Logger')(logLevel);
logger.log('LOG LEVEL IS: ' + logLevel)
if (localServer) {
environment = globals.ENVIRONMENT.LOCAL;
@@ -99,5 +106,5 @@ inGame.on('connection', function (socket) {
});
main.listen(port, function () {
logger.log(`Starting server on port ${port} http://localhost:${port}` );
logger.log(`Starting server on port ${port}` );
});

View File

@@ -7,6 +7,7 @@ class Person {
this.gameRole = gameRole;
this.gameRoleDescription = gameRoleDescription;
this.assigned = assigned;
this.out = false;
}
}

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);
}
};
};