mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
add log level as a cli arg
This commit is contained in:
@@ -38,6 +38,10 @@ h1 {
|
||||
}
|
||||
|
||||
#game-link {
|
||||
user-select: none;
|
||||
-ms-user-select: none;
|
||||
-webkit-user-select: none;
|
||||
-moz-user-select: none;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
|
||||
@@ -24,6 +24,13 @@ const globals = {
|
||||
ENVIRONMENT: {
|
||||
LOCAL: "local",
|
||||
PRODUCTION: "production"
|
||||
},
|
||||
LOG_LEVEL: {
|
||||
INFO: "info",
|
||||
DEBUG: "debug",
|
||||
ERROR: "error",
|
||||
WARN: "warn",
|
||||
TRACE: "trace"
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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}` );
|
||||
});
|
||||
|
||||
@@ -7,6 +7,7 @@ class Person {
|
||||
this.gameRole = gameRole;
|
||||
this.gameRoleDescription = gameRoleDescription;
|
||||
this.assigned = assigned;
|
||||
this.out = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user