From 8ebe31671584de9e2e5457143d361b93a5b10d66 Mon Sep 17 00:00:00 2001 From: Nathan Walters Date: Tue, 2 Apr 2019 17:11:18 -0500 Subject: [PATCH 1/3] Improve how logger handles errors and strings --- src/util/logger.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/util/logger.js b/src/util/logger.js index b11cd075..72c72b73 100644 --- a/src/util/logger.js +++ b/src/util/logger.js @@ -1,13 +1,22 @@ const { createLogger, transports, format } = require('winston') +const { inspect } = require('util') -const { combine, colorize, timestamp, align, printf } = format +const { combine, colorize, timestamp, errors, splat } = format + +const outputFormat = format.printf(info => { + const formattedMessage = + typeof info.message === 'string' ? info.message : inspect(info.message) + const message = info.stack || formattedMessage + return `${info.timestamp} [${info.level}]: ${message}` +}) module.exports = createLogger({ format: combine( + errors({ stack: true }), colorize(), timestamp(), - align(), - printf(info => `${info.timestamp} [${info.level}]: ${info.message}`) + splat(), + outputFormat ), transports: [ new transports.Console({ From c32aeb5c5d1386109102075768c53b230ab5cef3 Mon Sep 17 00:00:00 2001 From: Nathan Walters Date: Tue, 2 Apr 2019 17:11:36 -0500 Subject: [PATCH 2/3] Add verbose logging when socket initialization fails --- src/socket/server.js | 68 ++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/src/socket/server.js b/src/socket/server.js index 1265cad4..cf3bc07a 100644 --- a/src/socket/server.js +++ b/src/socket/server.js @@ -218,38 +218,44 @@ module.exports = newIo => { queueNamespace = io.of('/queue') queueNamespace.on('connection', socket => { socket.on('join', async (msg, callback) => { - if ('queueId' in msg) { - const { queueId } = msg - const queuePromise = Queue.findOne({ - where: { - id: queueId, - }, - }) - const userAuthzPromise = getAuthzForUser(socket.request.user) - const [queue, userAuthz] = await Promise.all([ - queuePromise, - userAuthzPromise, - ]) - const { courseId, isConfidential } = queue - const isStudent = isUserStudent(userAuthz, courseId) - let sendCompleteQuestionData = true - if (isConfidential && isStudent) { - // All users that shouldn't see confidential information are added - // to a "public" version of the room that receives the minimum - // possible set of information - socket.join(`queue-${queueId}-public`) - // Users will also join a specific room for themselves so that they - // receive updates about questions being answered, etc. - socket.join(`queue-${queueId}-user-${socket.request.user.id}`) - sendCompleteQuestionData = false - } else { - // For non-confidential queues, this room will consider receiving all - // updates for all users. For confidential queues, only admins and - // course staff will be subscribed to this room - socket.join(`queue-${queueId}`) + try { + if ('queueId' in msg) { + const { queueId } = msg + const queuePromise = Queue.findOne({ + where: { + id: queueId, + }, + }) + const userAuthzPromise = getAuthzForUser(socket.request.user) + const [queue, userAuthz] = await Promise.all([ + queuePromise, + userAuthzPromise, + ]) + const { courseId, isConfidential } = queue + const isStudent = isUserStudent(userAuthz, courseId) + let sendCompleteQuestionData = true + if (isConfidential && isStudent) { + // All users that shouldn't see confidential information are added + // to a "public" version of the room that receives the minimum + // possible set of information + socket.join(`queue-${queueId}-public`) + // Users will also join a specific room for themselves so that they + // receive updates about questions being answered, etc. + socket.join(`queue-${queueId}-user-${socket.request.user.id}`) + sendCompleteQuestionData = false + } else { + // For non-confidential queues, this room will consider receiving all + // updates for all users. For confidential queues, only admins and + // course staff will be subscribed to this room + socket.join(`queue-${queueId}`) + } + const { id: userId } = socket.request.user + sendInitialState(queueId, userId, sendCompleteQuestionData, callback) } - const { id: userId } = socket.request.user - sendInitialState(queueId, userId, sendCompleteQuestionData, callback) + } catch (err) { + logger.error('failed to initialize socket for message') + logger.error(err) + logger.error(msg) } }) }) From 345812a9e017093a40b3d18296f83882a583754d Mon Sep 17 00:00:00 2001 From: Nathan Walters Date: Tue, 2 Apr 2019 17:14:04 -0500 Subject: [PATCH 3/3] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d001e54e..5b65b72f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ with the current semantic version and the next changes should go under a **[Next ## [Next] * Fix logic for filtering confidential questions. ([@james9909](https://github.com/james9909) in [#257](https://github.com/illinois/queue/pull/257)) +* Add logging for new socket initialization error. ([@nwalters512](https://github.com/nwalters512) in [#258](https://github.com/illinois/queue/pull/258)) ## v1.0.4