Browse Source

Improved logger

theflametrooper 8 năm trước cách đây
mục cha
commit
8972135f6d

+ 10 - 10
backend/logic/actions/news.js

@@ -47,10 +47,10 @@ module.exports = {
 			}
 		], (err, news) => {
 			if (err) {
-				logger.log("NEWS_INDEX", "ERROR", `Indexing news failed. "${err.message}"`);
+				logger.error("NEWS_INDEX", `Indexing news failed. "${err.message}"`);
 				return cb({status: 'failure', message: 'Something went wrong.'});
 			}
-			logger.log("NEWS_INDEX", "SUCCESS", `Indexing news successful.`);
+			logger.success("NEWS_INDEX", `Indexing news successful.`);
 			return cb({ status: 'success', data: news });
 		});
 	},
@@ -72,11 +72,11 @@ module.exports = {
 			}
 		], (err, news) => {
 			if (err) {
-				logger.log("NEWS_CREATE", "ERROR", `Creating news failed. "${err.message}"`);
+				logger.error("NEWS_CREATE", `Creating news failed. "${err.message}"`);
 				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
 			}
 			cache.pub('news.create', news);
-			logger.log("NEWS_CREATE", "SUCCESS", `Creating news successful.`);
+			logger.success("NEWS_CREATE", `Creating news successful.`);
 			return cb({ 'status': 'success', 'message': 'Successfully created News' });
 		});
 	}),
@@ -94,10 +94,10 @@ module.exports = {
 			}
 		], (err, news) => {
 			if (err) {
-				logger.log("NEWS_NEWEST", "ERROR", `Getting the latest news failed. "${err.message}"`);
+				logger.error("NEWS_NEWEST", `Getting the latest news failed. "${err.message}"`);
 				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
 			}
-			logger.log("NEWS_NEWEST", "SUCCESS", `Successfully got the latest news.`);
+			logger.success("NEWS_NEWEST", `Successfully got the latest news.`);
 			return cb({ status: 'success', data: news });
 		});
 	},
@@ -114,11 +114,11 @@ module.exports = {
 	remove: hooks.adminRequired((session, news, cb, userId) => {
 		db.models.news.remove({ _id: news._id }, err => {
 			if (err) {
-				logger.log("NEWS_REMOVE", "ERROR", `Removing news "${news._id}" failed for user "${userId}". "${err.message}"`);
+				logger.error("NEWS_REMOVE", `Removing news "${news._id}" failed for user "${userId}". "${err.message}"`);
 				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
 			} else {
 				cache.pub('news.remove', news);
-				logger.log("NEWS_REMOVE", "SUCCESS", `Removing news "${news._id}" successful by user "${userId}".`);
+				logger.success("NEWS_REMOVE", `Removing news "${news._id}" successful by user "${userId}".`);
 				return cb({ 'status': 'success', 'message': 'Successfully removed News' });
 			}
 		});
@@ -136,11 +136,11 @@ module.exports = {
 	update: hooks.adminRequired((session, _id, news, cb, userId) => {
 		db.models.news.update({ _id }, news, { upsert: true }, err => {
 			if (err) {
-				logger.log("NEWS_UPDATE", "ERROR", `Updating news "${_id}" failed for user "${userId}". "${err.message}"`);
+				logger.error("NEWS_UPDATE", `Updating news "${_id}" failed for user "${userId}". "${err.message}"`);
 				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
 			} else {
 				cache.pub('news.update', news);
-				logger.log("NEWS_UPDATE", "SUCCESS", `Updating news "${_id}" successful for user "${userId}".`);
+				logger.success("NEWS_UPDATE", `Updating news "${_id}" successful for user "${userId}".`);
 				return cb({ 'status': 'success', 'message': 'Successfully updated News' });
 			}
 		});

+ 24 - 24
backend/logic/actions/playlists.js

@@ -95,10 +95,10 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_GET_FIRST_SONG", "ERROR", `Getting the first song of playlist "${playlistId}" failed for user "${userId}". "${error}"`);
+				logger.error("PLAYLIST_GET_FIRST_SONG", `Getting the first song of playlist "${playlistId}" failed for user "${userId}". "${error}"`);
 				return cb({ status: 'failure', message: 'Something went wrong when getting the playlist'});
 			}
-			logger.log("PLAYLIST_GET_FIRST_SONG", "SUCCESS", `Successfully got the first song of playlist "${playlistId}" for user "${userId}".`);
+			logger.success("PLAYLIST_GET_FIRST_SONG", `Successfully got the first song of playlist "${playlistId}" for user "${userId}".`);
 			cb({
 				status: 'success',
 				song: song
@@ -123,10 +123,10 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_INDEX_FOR_USER", "ERROR", `Indexing playlists for user "${userId}" failed. "${error}"`);
+				logger.error("PLAYLIST_INDEX_FOR_USER", `Indexing playlists for user "${userId}" failed. "${error}"`);
 				return cb({ status: 'failure', message: error});
 			}
-			logger.log("PLAYLIST_INDEX_FOR_USER", "SUCCESS", `Successfully indexed playlists for user "${userId}".`);
+			logger.success("PLAYLIST_INDEX_FOR_USER", `Successfully indexed playlists for user "${userId}".`);
 			cb({
 				status: 'success',
 				data: playlists
@@ -165,11 +165,11 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_CREATE", "ERROR", `Creating private playlist failed for user "${userId}". "${error}"`);
+				logger.error("PLAYLIST_CREATE", `Creating private playlist failed for user "${userId}". "${error}"`);
 				return cb({ status: 'failure', message: error});
 			}
 			cache.pub('playlist.create', playlist._id);
-			logger.log("PLAYLIST_CREATE", "SUCCESS", `Successfully created private playlist for user "${userId}".`);
+			logger.success("PLAYLIST_CREATE", `Successfully created private playlist for user "${userId}".`);
 			cb({ 'status': 'success', 'message': 'Successfully created playlist' });
 		});
 	}),
@@ -197,10 +197,10 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_GET", "ERROR", `Getting private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
+				logger.error("PLAYLIST_GET", `Getting private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
 				return cb({ status: 'failure', message: error});
 			}
-			logger.log("PLAYLIST_GET", "SUCCESS", `Successfully got private playlist "${playlistId}" for user "${userId}".`);
+			logger.success("PLAYLIST_GET", `Successfully got private playlist "${playlistId}" for user "${userId}".`);
 			console.log(playlist);
 			cb({
 				status: 'success',
@@ -233,10 +233,10 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_UPDATE", "ERROR", `Updating private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
+				logger.error("PLAYLIST_UPDATE", `Updating private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
 				return cb({ status: 'failure', message: error});
 			}
-			logger.log("PLAYLIST_UPDATE", "SUCCESS", `Successfully updated private playlist "${playlistId}" for user "${userId}".`);
+			logger.success("PLAYLIST_UPDATE", `Successfully updated private playlist "${playlistId}" for user "${userId}".`);
 			cb({
 				status: 'success',
 				data: playlist
@@ -294,10 +294,10 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_ADD_SONG", "ERROR", `Adding song "${songId}" to private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
+				logger.error("PLAYLIST_ADD_SONG", `Adding song "${songId}" to private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
 				return cb({ status: 'failure', message: error});
 			}
-			logger.log("PLAYLIST_ADD_SONG", "SUCCESS", `Successfully added song "${songId}" to private playlist "${playlistId}" for user "${userId}".`);
+			logger.success("PLAYLIST_ADD_SONG", `Successfully added song "${songId}" to private playlist "${playlistId}" for user "${userId}".`);
 			cache.pub('playlist.addSong', { playlistId: playlist._id, song: newSong, userId: userId });
 			return cb({ status: 'success', message: 'Song has been successfully added to the playlist', data: playlist.songs });
 		});
@@ -344,10 +344,10 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_IMPORT", "ERROR", `Importing a YouTube playlist to private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
+				logger.error("PLAYLIST_IMPORT", `Importing a YouTube playlist to private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
 				return cb({ status: 'failure', message: error});
 			}
-			logger.log("PLAYLIST_IMPORT", "SUCCESS", `Successfully imported a YouTube playlist to private playlist "${playlistId}" for user "${userId}".`);
+			logger.success("PLAYLIST_IMPORT", `Successfully imported a YouTube playlist to private playlist "${playlistId}" for user "${userId}".`);
 			cb({ status: 'success', message: 'Playlist has been successfully imported.', data: playlist.songs });
 		});
 	}),
@@ -380,10 +380,10 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_REMOVE_SONG", "ERROR", `Removing song "${songId}" from private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
+				logger.error("PLAYLIST_REMOVE_SONG", `Removing song "${songId}" from private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
 				return cb({ status: 'failure', message: error});
 			}
-			logger.log("PLAYLIST_REMOVE_SONG", "SUCCESS", `Successfully removed song "${songId}" from private playlist "${playlistId}" for user "${userId}".`);
+			logger.success("PLAYLIST_REMOVE_SONG", `Successfully removed song "${songId}" from private playlist "${playlistId}" for user "${userId}".`);
 			cache.pub('playlist.removeSong', {playlistId: playlist._id, songId: songId, userId: userId});
 			return cb({ status: 'success', message: 'Song has been successfully removed from playlist', data: playlist.songs });
 		});
@@ -411,10 +411,10 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_UPDATE_DISPLAY_NAME", "ERROR", `Updating display name to "${displayName}" for private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
+				logger.error("PLAYLIST_UPDATE_DISPLAY_NAME", `Updating display name to "${displayName}" for private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
 				return cb({ status: 'failure', message: error});
 			}
-			logger.log("PLAYLIST_UPDATE_DISPLAY_NAME", "SUCCESS", `Successfully updated display name to "${displayName}" for private playlist "${playlistId}" for user "${userId}".`);
+			logger.success("PLAYLIST_UPDATE_DISPLAY_NAME", `Successfully updated display name to "${displayName}" for private playlist "${playlistId}" for user "${userId}".`);
 			cache.pub('playlist.updateDisplayName', {playlistId: playlistId, displayName: displayName, userId: userId});
 			return cb({ status: 'success', message: 'Playlist has been successfully updated' });
 		});
@@ -472,10 +472,10 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_MOVE_SONG_TO_TOP", "ERROR", `Moving song "${songId}" to the top for private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
+				logger.error("PLAYLIST_MOVE_SONG_TO_TOP", `Moving song "${songId}" to the top for private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
 				return cb({ status: 'failure', message: error});
 			}
-			logger.log("PLAYLIST_MOVE_SONG_TO_TOP", "SUCCESS", `Successfully moved song "${songId}" to the top for private playlist "${playlistId}" for user "${userId}".`);
+			logger.success("PLAYLIST_MOVE_SONG_TO_TOP", `Successfully moved song "${songId}" to the top for private playlist "${playlistId}" for user "${userId}".`);
 			cache.pub('playlist.moveSongToTop', {playlistId, songId, userId: userId});
 			return cb({ status: 'success', message: 'Playlist has been successfully updated' });
 		});
@@ -530,10 +530,10 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_MOVE_SONG_TO_BOTTOM", "ERROR", `Moving song "${songId}" to the bottom for private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
+				logger.error("PLAYLIST_MOVE_SONG_TO_BOTTOM", `Moving song "${songId}" to the bottom for private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
 				return cb({ status: 'failure', message: error});
 			}
-			logger.log("PLAYLIST_MOVE_SONG_TO_BOTTOM", "SUCCESS", `Successfully moved song "${songId}" to the bottom for private playlist "${playlistId}" for user "${userId}".`);
+			logger.success("PLAYLIST_MOVE_SONG_TO_BOTTOM", `Successfully moved song "${songId}" to the bottom for private playlist "${playlistId}" for user "${userId}".`);
 			cache.pub('playlist.moveSongToBottom', {playlistId, songId, userId: userId});
 			return cb({ status: 'success', message: 'Playlist has been successfully updated' });
 		});
@@ -557,10 +557,10 @@ let lib = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("PLAYLIST_REMOVE", "ERROR", `Removing private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
+				logger.error("PLAYLIST_REMOVE", `Removing private playlist "${playlistId}" failed for user "${userId}". "${error}"`);
 				return cb({ status: 'failure', message: error});
 			}
-			logger.log("PLAYLIST_REMOVE", "SUCCESS", `Successfully removed private playlist "${playlistId}" for user "${userId}".`);
+			logger.success("PLAYLIST_REMOVE", `Successfully removed private playlist "${playlistId}" for user "${userId}".`);
 			cache.pub('playlist.delete', {userId: userId, playlistId});
 			return cb({ status: 'success', message: 'Playlist successfully removed' });
 		});

+ 6 - 6
backend/logic/actions/queueSongs.js

@@ -40,10 +40,10 @@ module.exports = {
 			}
 		], (err, songs) => {
 			if (err) {
-				logger.log("QUEUE_INDEX", "ERROR", `Indexing queuesongs failed. "${err.message}"`);
+				logger.error("QUEUE_INDEX", `Indexing queuesongs failed. "${err.message}"`);
 				return cb({status: 'failure', message: 'Something went wrong.'});
 			}
-			logger.log("QUEUE_INDEX", "SUCCESS", `Indexing queuesongs successful.`);
+			logger.success("QUEUE_INDEX", `Indexing queuesongs successful.`);
 			return cb(songs);
 		});
 	}),
@@ -76,11 +76,11 @@ module.exports = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("QUEUE_UPDATE", "ERROR", `Updating queuesong "${songId}" failed for user ${userId}. "${err.message}"`);
+				logger.error("QUEUE_UPDATE", `Updating queuesong "${songId}" failed for user ${userId}. "${err.message}"`);
 				return cb({status: 'failure', message: error});
 			}
 			cache.pub('queue.updatedSong', songId);
-			logger.log("QUEUE_UPDATE", "SUCCESS", `User "${userId}" successfully update queuesong "${songId}".`);
+			logger.success("QUEUE_UPDATE", `User "${userId}" successfully update queuesong "${songId}".`);
 			return cb({status: 'success', message: 'Successfully updated song.'});
 		});
 	}),
@@ -103,11 +103,11 @@ module.exports = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("QUEUE_REMOVE", "ERROR", `Removing queuesong "${songId}" failed for user ${userId}. "${err.message}"`);
+				logger.error("QUEUE_REMOVE", `Removing queuesong "${songId}" failed for user ${userId}. "${err.message}"`);
 				return cb({status: 'failure', message: error});
 			}
 			cache.pub('queue.removedSong', songId);
-			logger.log("QUEUE_REMOVE", "SUCCESS", `User "${userId}" successfully removed queuesong "${songId}".`);
+			logger.success("QUEUE_REMOVE", `User "${userId}" successfully removed queuesong "${songId}".`);
 			return cb({status: 'success', message: 'Successfully updated song.'});
 		});
 	}),

+ 6 - 6
backend/logic/actions/reports.js

@@ -74,10 +74,10 @@ module.exports = {
 			}
 		], (err, reports) => {
 			if (err) {
-				logger.log("REPORTS_INDEX", "ERROR", `Indexing reports failed. "${err.message}"`);
+				logger.error("REPORTS_INDEX", `Indexing reports failed. "${err.message}"`);
 				return cb({ 'status': 'failure', 'message': 'Something went wrong'});
 			}
-			logger.log("REPORTS_INDEX", "SUCCESS", "Indexing reports successful.");
+			logger.success("REPORTS_INDEX", "Indexing reports successful.");
 			cb({ status: 'success', data: reports });
 		});
 	}),
@@ -106,11 +106,11 @@ module.exports = {
 			}
 		], (err) => {
 			if (err) {
-				logger.log("REPORTS_RESOLVE", "ERROR", `Resolving report "${reportId}" failed by user "${userId}". Mongo error. "${err.message}"`);
+				logger.error("REPORTS_RESOLVE", `Resolving report "${reportId}" failed by user "${userId}". Mongo error. "${err.message}"`);
 				return cb({ 'status': 'failure', 'message': 'Something went wrong'});
 			} else {
 				cache.pub('report.resolve', reportId);
-				logger.log("REPORTS_RESOLVE", "SUCCESS", `User "${userId}" resolved report "${reportId}".`);
+				logger.success("REPORTS_RESOLVE", `User "${userId}" resolved report "${reportId}".`);
 				cb({ status: 'success', message: 'Successfully resolved Report' });
 			}
 		});
@@ -171,12 +171,12 @@ module.exports = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("REPORTS_CREATE", "ERROR", `Creating report for "${data.songId}" failed by user "${userId}". "${error}"`);
+				logger.error("REPORTS_CREATE", `Creating report for "${data.songId}" failed by user "${userId}". "${error}"`);
 				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
 			}
 			else {
 				cache.pub('report.create', report);
-				logger.log("REPORTS_CREATE", "SUCCESS", `User "${userId}" created report for "${data.songId}".`);
+				logger.success("REPORTS_CREATE", `User "${userId}" created report for "${data.songId}".`);
 				return cb({ 'status': 'success', 'message': 'Successfully created report' });
 			}
 		});

+ 30 - 30
backend/logic/actions/users.js

@@ -67,10 +67,10 @@ module.exports = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("USER_PASSWORD_LOGIN", "ERROR", "Login failed with password for user " + identifier + '. "' + error + '"');
+				logger.error("USER_PASSWORD_LOGIN", "Login failed with password for user " + identifier + '. "' + error + '"');
 				return cb({ status: 'failure', message: error });
 			}
-			logger.log("USER_PASSWORD_LOGIN", "SUCCESS", "Login successful with password for user " + identifier);
+			logger.success("USER_PASSWORD_LOGIN", "Login successful with password for user " + identifier);
 			cb({ status: 'success', message: 'Login successful', user: {}, SID: sessionId });
 		});
 
@@ -157,7 +157,7 @@ module.exports = {
 				let error = 'An error occurred.';
 				if (typeof err === "string") error = err;
 				else if (err.message) error = err.message;
-				logger.log("USER_PASSWORD_REGISTER", "ERROR", "Register failed with password for user. " + '"' + error + '"');
+				logger.error("USER_PASSWORD_REGISTER", "Register failed with password for user. " + '"' + error + '"');
 				cb({status: 'failure', message: error});
 			} else {
 				module.exports.login(session, email, password, (result) => {
@@ -165,7 +165,7 @@ module.exports = {
 					if (result.status === 'success') {
 						obj.SID = result.SID;
 					}
-					logger.log("USER_PASSWORD_REGISTER", "SUCCESS", "Register successful with password for user '" + username + "'.");
+					logger.success("USER_PASSWORD_REGISTER", "Register successful with password for user '" + username + "'.");
 					cb({status: 'success', message: 'Successfully registered.'});
 				});
 			}
@@ -184,16 +184,16 @@ module.exports = {
 		cache.hget('sessions', session.sessionId, (err, session) => {
 			if (err || !session) {
 				//TODO Properly return err message
-				logger.log("USER_LOGOUT", "ERROR", "Logout failed. Couldn't get session.");
+				logger.error("USER_LOGOUT", "Logout failed. Couldn't get session.");
 				return cb({ 'status': 'failure', message: 'Something went wrong while logging you out.' });
 			}
 
 			cache.hdel('sessions', session.sessionId, (err) => {
 				if (err) {
-					logger.log("USER_LOGOUT", "ERROR", "Logout failed. Failed deleting session from cache.");
+					logger.error("USER_LOGOUT", "Logout failed. Failed deleting session from cache.");
 					return cb({ 'status': 'failure', message: 'Something went wrong while logging you out.' });
 				}
-				logger.log("USER_LOGOUT", "SUCCESS", "Logout successful.");
+				logger.success("USER_LOGOUT", "Logout successful.");
 				return cb({ 'status': 'success', message: 'You have been successfully logged out.' });
 			});
 		});
@@ -210,18 +210,18 @@ module.exports = {
 	findByUsername: (session, username, cb) => {
 		db.models.user.find({ username }, (err, account) => {
 			if (err) {
-				logger.log("FIND_BY_USERNAME", "ERROR", "Find by username failed for username '" + username + "'. Mongo error.");
+				logger.error("FIND_BY_USERNAME", "Find by username failed for username '" + username + "'. Mongo error.");
 				throw err;
 			}
 			else if (account.length == 0) {
-				logger.log("FIND_BY_USERNAME", "ERROR", "User not found for username '" + username + "'.");
+				logger.error("FIND_BY_USERNAME", "User not found for username '" + username + "'.");
 				return cb({
 					status: 'error',
 					message: 'Username cannot be found'
 				});
 			} else {
 				account = account[0];
-				logger.log("FIND_BY_USERNAME", "SUCCESS", "User found for username '" + username + "'.");
+				logger.success("FIND_BY_USERNAME", "User found for username '" + username + "'.");
 				return cb({
 					status: 'success',
 					data: {
@@ -250,19 +250,19 @@ module.exports = {
 	findBySession: (session, cb) => {
 		cache.hget('sessions', session.sessionId, (err, session) => {
 			if (err) {
-				logger.log("FIND_BY_SESSION", "ERROR", "Failed getting session. Redis error. '" + err + "'.");
+				logger.error("FIND_BY_SESSION", "Failed getting session. Redis error. '" + err + "'.");
 				return cb({ 'status': 'error', message: err.message });
 			}
 			if (!session) {
-				logger.log("FIND_BY_SESSION", "ERROR", "Session not found. Not logged in.");
+				logger.error("FIND_BY_SESSION", "Session not found. Not logged in.");
 				return cb({ 'status': 'error', message: 'You are not logged in' });
 			}
 			db.models.user.findOne({ _id: session.userId }, {username: 1, "email.address": 1}, (err, user) => {
 				if (err) {
-					logger.log("FIND_BY_SESSION", "ERROR", "User not found. Failed getting user. Mongo error.");
+					logger.error("FIND_BY_SESSION", "User not found. Failed getting user. Mongo error.");
 					throw err;
 				} else if (user) {
-					logger.log("FIND_BY_SESSION", "SUCCESS", "User found. '" + user.username + "'.");
+					logger.success("FIND_BY_SESSION", "User found. '" + user.username + "'.");
 					return cb({
 						status: 'success',
 						data: user
@@ -284,51 +284,51 @@ module.exports = {
 	updateUsername: hooks.loginRequired((session, newUsername, cb, userId) => {
 		db.models.user.findOne({ _id: userId }, (err, user) => {
 			if (err) {
-				logger.log("UPDATE_USERNAME", "ERROR", `Failed getting user. Mongo error. '${err.message}'.`);
+				logger.error("UPDATE_USERNAME", `Failed getting user. Mongo error. '${err.message}'.`);
 				return cb({ status: 'error', message: 'Something went wrong.' });
 			} else if (!user) {
-				logger.log("UPDATE_USERNAME", "ERROR", `User not found. '${userId}'`);
+				logger.error("UPDATE_USERNAME", `User not found. '${userId}'`);
 				return cb({ status: 'error', message: 'User not found' });
 			} else if (user.username !== newUsername) {
 				if (user.username.toLowerCase() !== newUsername.toLowerCase()) {
 					db.models.user.findOne({ username: new RegExp(`^${newUsername}$`, 'i') }, (err, _user) => {
 						if (err) {
-							logger.log("UPDATE_USERNAME", "ERROR", `Failed to get other user with the same username. Mongo error. '${err.message}'`);
+							logger.error("UPDATE_USERNAME", `Failed to get other user with the same username. Mongo error. '${err.message}'`);
 							return cb({ status: 'error', message: err.message });
 						}
 						if (_user) {
-							logger.log("UPDATE_USERNAME", "ERROR", `Username already in use.`);
+							logger.error("UPDATE_USERNAME", `Username already in use.`);
 							return cb({ status: 'failure', message: 'That username is already in use' });
 						}
 						db.models.user.update({ _id: userId }, { $set: { username: newUsername } }, (err) => {
 							if (err) {
-								logger.log("UPDATE_USERNAME", "ERROR", `Couldn't update user. Mongo error. '${err.message}'`);
+								logger.error("UPDATE_USERNAME", `Couldn't update user. Mongo error. '${err.message}'`);
 								return cb({ status: 'error', message: err.message });
 							}
 							cache.pub('user.updateUsername', {
 								username: newUsername,
 								_id: userId
 							});
-							logger.log("UPDATE_USERNAME", "SUCCESS", `Updated username. '${userId}' '${newUsername}'`);
+							logger.success("UPDATE_USERNAME", `Updated username. '${userId}' '${newUsername}'`);
 							cb({ status: 'success', message: 'Username updated successfully' });
 						});
 					});
 				} else {
 					db.models.user.update({ _id: userId }, { $set: { username: newUsername } }, (err) => {
 						if (err) {
-							logger.log("UPDATE_USERNAME", "ERROR", `Couldn't update user. Mongo error. '${err.message}'`);
+							logger.error("UPDATE_USERNAME", `Couldn't update user. Mongo error. '${err.message}'`);
 							return cb({ status: 'error', message: err.message });
 						}
 						cache.pub('user.updateUsername', {
 							username: newUsername,
 							_id: userId
 						});
-						logger.log("UPDATE_USERNAME", "SUCCESS", `Updated username. '${userId}' '${newUsername}'`);
+						logger.success("UPDATE_USERNAME", `Updated username. '${userId}' '${newUsername}'`);
 						cb({ status: 'success', message: 'Username updated successfully' });
 					});
 				}
 			} else {
-				logger.log("UPDATE_USERNAME", "ERROR", `New username is the same as the old username. '${newUsername}'`);
+				logger.error("UPDATE_USERNAME", `New username is the same as the old username. '${newUsername}'`);
 				cb({ status: 'error', message: 'Your new username cannot be the same as your old username' });
 			}
 		});
@@ -346,31 +346,31 @@ module.exports = {
 		newEmail = newEmail.toLowerCase();
 		db.models.user.findOne({ _id: userId }, (err, user) => {
 			if (err) {
-				logger.log("UPDATE_EMAIL", "ERROR", `Failed getting user. Mongo error. '${err.message}'.`);
+				logger.error("UPDATE_EMAIL", `Failed getting user. Mongo error. '${err.message}'.`);
 				return cb({ status: 'error', message: 'Something went wrong.' });
 			} else if (!user) {
-				logger.log("UPDATE_EMAIL", "ERROR", `User not found. '${userId}'`);
+				logger.error("UPDATE_EMAIL", `User not found. '${userId}'`);
 				return cb({ status: 'error', message: 'User not found.' });
 			} else if (user.email.address !== newEmail) {
 				db.models.user.findOne({"email.address": newEmail}, (err, _user) => {
 					if (err) {
-						logger.log("UPDATE_EMAIL", "ERROR", `Couldn't get other user with new email. Mongo error. '${newEmail}'`);
+						logger.error("UPDATE_EMAIL", `Couldn't get other user with new email. Mongo error. '${newEmail}'`);
 						return cb({ status: 'error', message: err.message });
 					} else if (_user) {
-						logger.log("UPDATE_EMAIL", "ERROR", `Email already in use.`);
+						logger.error("UPDATE_EMAIL", `Email already in use.`);
 						return cb({ status: 'failure', message: 'That email is already in use.' });
 					}
 					db.models.user.update({_id: userId}, {$set: {"email.address": newEmail}}, (err) => {
 						if (err) {
-							logger.log("UPDATE_EMAIL", "ERROR", `Couldn't update user. Mongo error. ${err.message}`);
+							logger.error("UPDATE_EMAIL", `Couldn't update user. Mongo error. ${err.message}`);
 							return cb({ status: 'error', message: err.message });
 						}
-						logger.log("UPDATE_EMAIL", "SUCCESS", `Updated email. '${userId}' ${newEmail}'`);
+						logger.success("UPDATE_EMAIL", `Updated email. '${userId}' ${newEmail}'`);
 						cb({ status: 'success', message: 'Email updated successfully.' });
 					});
 				});
 			} else {
-				logger.log("UPDATE_EMAIL", "ERROR", `New email is the same as the old email.`);
+				logger.error("UPDATE_EMAIL", `New email is the same as the old email.`);
 				cb({
 					status: 'error',
 					message: 'Email has not changed. Your new email cannot be the same as your old email.'

+ 24 - 11
backend/logic/logger.js

@@ -2,18 +2,31 @@
 
 let twoDigits = (num) => {
 	return (num < 10) ? '0' + num : num;
-}
+};
+
+let getTime = (cb) => {
+	let time = new Date();
+	return cb ({
+		year: time.getFullYear(),
+		month: time.getMonth() + 1,
+		day: time.getDate(),
+		hour: time.getHours(),
+		minute: time.getMinutes(),
+		second: time.getSeconds()
+	});
+};
 
 module.exports = {
-	log: function(type, level, message) {
-		let time = new Date();
-		let year = time.getFullYear();
-		let month = time.getMonth() + 1;
-		let day = time.getDate();
-		let hour = time.getHours();
-		let minute = time.getMinutes();
-		let second = time.getSeconds();
-		let timeString = year + '-' + twoDigits(month) + '-' + twoDigits(day) + ' ' + twoDigits(hour) + ':' + twoDigits(minute) + ':' + twoDigits(second);
-		console.log(timeString, level, type, "-", message);
+	success: (type, message) => {
+		getTime((time) => {
+			let timeString = `${time.year}-${twoDigits(time.month)}-${twoDigits(time.day)} ${twoDigits(time.hour)}:${twoDigits(time.minute)}:${twoDigits(time.second)}`;
+			console.info('\x1b[32m', timeString, 'SUCCESS', '-', message, '\x1b[0m');
+		});
+	},
+	error: (type, message) => {
+		getTime((time) => {
+			let timeString = `${time.year}-${twoDigits(time.month)}-${twoDigits(time.day)} ${twoDigits(time.hour)}:${twoDigits(time.minute)}:${twoDigits(time.second)}`;
+			console.warn('\x1b[31m', timeString, 'ERROR', type, '-', message, '\x1b[0m');
+		});
 	}
 };