Skip to content

Commit

Permalink
Added a log system
Browse files Browse the repository at this point in the history
  • Loading branch information
Nouzbe committed Aug 16, 2015
1 parent 572b119 commit 811d2a3
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ modules/views/
/public/build/
/packages/contrib/
/packages/**/**/public/assets/lib
*.log


# --------------------
Expand Down
9 changes: 5 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var express = require('express'),
nodemailer = require('nodemailer'),
objectService = require('./server/services/objectService'),
profileService = require('./server/services/profileService'),
configUtil = require('./server/utils/configUtil');
configUtil = require('./server/utils/configUtil'),
logger = require('./server/utils/logUtil');

// load the configuration
configUtil.loadConfig();
Expand Down Expand Up @@ -51,12 +52,12 @@ app.get('/api/isloggedin', function(req, res) {
res.send(req.user);
}
else {
console.log(new Date + ' | WARNING | ' + req.user + ' | is not authenticated. Redirecting.');
logger.internalWarning('Just received an unauthenticated request. Redirecting.');
res.send('0');
}
});
app.get('/api/logout/:user', function (req, res){
console.log(new Date + ' | SUCCESS | ' + req.params.user + ' | just logged out.');
logger.info(req.params.user, 'just logged out.');
req.session.destroy(function (err) {
res.redirect('/');
});
Expand Down Expand Up @@ -85,7 +86,7 @@ app.get('*', function (req, res) {

// Once set up, the server should listen to the given port.
app.listen(configUtil.get('port'), function() {
console.log('I\'m listening.');
logger.internalInfo('Your node server just started up. It is currently listening on port ' + configUtil.get('port') + '.');
// reloading the configuration every minute
setInterval(function() {
configUtil.loadConfig();
Expand Down
12 changes: 8 additions & 4 deletions server/passport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var LocalStrategy = require('passport-local').Strategy,
User = require('../server/models/user.js');
var LocalStrategy = require('passport-local').Strategy,
logger = require('../server/utils/logUtil.js'),
User = require('../server/models/user.js');

module.exports = function(passport) {
passport.serializeUser(function(user, done) {
Expand All @@ -21,6 +22,7 @@ module.exports = function(passport) {
process.nextTick(function() {
User.findOne({'local.username' : username}, function(err, user) {
if(err) {
logger.error(username, 'tried to sign up: \n' + err);
return done(err);
}
if(user) {
Expand All @@ -33,9 +35,10 @@ module.exports = function(passport) {

newUser.save(function(err) {
if(err) {
logger.error(username, 'tried to sign up: \n' + err);
return done(err);
}
console.log(new Date + ' | SUCCESS | ' + username + ' | just signed up.');
logger.info(username, 'just signed up.');
return done(null, newUser, req.flash('message', 'ok'));
});
}
Expand All @@ -51,6 +54,7 @@ module.exports = function(passport) {
function(req, username, password, done) {
User.findOne({'local.username': username}, function(err, user) {
if(err) {
logger.error(username, 'tried to log in: \n' + err);
return done(err);
}
if(!user) {
Expand All @@ -59,7 +63,7 @@ module.exports = function(passport) {
if(!user.validPassword(password)) {
return done(null, false, req.flash('message', 'Wrong password.'));
}
console.log(new Date + ' | SUCCESS | ' + username + ' | just logged in.');
logger.info(username, 'just logged in.');
return done(null, user, req.flash('message', 'ok'));
});
}));
Expand Down
34 changes: 17 additions & 17 deletions server/services/objectService.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
var UserObject = require('../models/object.js');
var UserObject = require('../models/object.js'),
logger = require('../utils/logUtil.js');

module.exports.create = function(req, res) {
var user = req.params.user;
var object = new UserObject(req.body);
if(!req.isAuthenticated()) {
res.statusCode = 401;
console.log(new Date + ' | WARNING | ' + user + ' | tried to create an object without being authenticated. Redirecting.');
logger.internalWarning('Just received an unauthenticated createObject request. Redirecting.');
}
else {
console.log('Create : ' + object);
object.save(function (err, result){
if(err) {
console.log(new Date + ' | ERROR | ' + user + ' | was trying to create an object:\n' + object + '\n' + err);
logger.error(user, 'was trying to create an object:\n' + object + '\n' + err);
}
else {
res.json(result);
console.log(new Date + ' | SUCCESS | ' + user + ' | created an object:\n' + result);
logger.info(user, 'created an object:\n' + result);
}
});
}
Expand All @@ -25,20 +25,20 @@ module.exports.list = function(req, res) {
var user = req.params.user;
if(!req.isAuthenticated()) {
res.statusCode = 401;
console.log(new Date + ' | WARNING | ' + user + ' | tried to get his / her personal objects without being authenticated. Redirecting.');
logger.internalWarning('Just received an unauthenticated listPersonalObjects request. Redirecting.');
}
else {
UserObject.find({user_name: user}, function (err, results){
if(err) {
console.log(new Date + ' | ERROR | ' + user + ' | was trying to get his / her personal objects:\n' + err);
logger.error(user, 'was trying to get his / her personal objects:\n' + err);
}
else {
res.json(results);
var idList = [];
for(var result in results) {
idList.push(results[result]._id);
}
console.log(new Date + ' | SUCCESS | ' + user + ' | got his / her ' + idList.length + ' personal objects: [' + idList + ']');
logger.info(user, 'got his / her ' + idList.length + ' personal objects: [' + idList + ']');
}
});
}
Expand All @@ -48,20 +48,20 @@ module.exports.listPublic = function(req, res) {
var user = req.params.user;
if(!req.isAuthenticated()) {
res.statusCode = 401;
console.log(new Date + ' | WARNING | ' + user + ' | tried to get the public objects without being authenticated. Redirecting.');
logger.internalWarning('Just received an unauthenticated listPublicObjects request. Redirecting.');
}
else {
UserObject.find({pub: true}, function (err, results){
if(err) {
console.log(new Date + ' | ERROR | ' + user + ' | was trying to get the public objects:\n' + err);
logger.erro(user, 'was trying to get the public objects:\n' + err);
}
else {
res.json(results);
var idList = [];
for(var result in results) {
idList.push(results[result]._id);
}
console.log(new Date + ' | SUCCESS | ' + user + ' | got the ' + idList.length + ' public objects: [' + idList + ']');
logger.info(user, 'got the ' + idList.length + ' public objects: [' + idList + ']');
}
});
}
Expand All @@ -72,15 +72,15 @@ module.exports.delete = function(req, res) {
var objectId = req.params.objectId;
if(!req.isAuthenticated()) {
res.statusCode = 401;
console.log(new Date + ' | WARNING | ' + user + ' | tried to delete an object without being authenticated. Redirecting.');
logger.internalWarning('Just received an unauthenticated deleteObject request. Redirecting.');
}
else {
UserObject.remove({_id: objectId}, function( err, object){
if(err) {
console.log(new Date + ' | ERROR | ' + user + ' | was trying to delete ' + objectId);
logger.error(user, 'was trying to delete ' + objectId);
}
res.json({ message: 'Succesfully deleted !' });
console.log(new Date + ' | SUCCESS | ' + user + ' | deleted ' + objectId);
logger.info(user, 'deleted ' + objectId);
});
}
}
Expand All @@ -90,16 +90,16 @@ module.exports.publish = function(req, res) {
var objectId = req.params.objectId;
if(!req.isAuthenticated()) {
res.statusCode = 401;
console.log(new Date + ' | WARNING | ' + user + ' | tried to publish an object without being authenticated. Redirecting.');
logger.internalWarning('Just received an unauthenticated publishObject request. Redirecting.');
}
else {
UserObject.update({_id: objectId},{pub: true}, function(err, numAffected) {
if(err) {
console.log(new Date + ' | ERROR | ' + user + ' | was trying to publish ' + objectId);
logger.error(user, 'was trying to publish ' + objectId);
}
else {
res.json({ message: 'Succesfully updated '.concat(numAffected.toString()).concat(' object(s).')});
console.log(new Date + ' | SUCCESS | ' + user + ' | published ' + objectId);
logger.info(user, 'published ' + objectId);
}
});
}
Expand Down
42 changes: 22 additions & 20 deletions server/services/profileService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var User = require('../models/user.js'),
UserObject = require('../models/object.js'),
configUtil = require('../utils/configUtil.js'),
logger = require('../utils/logUtil.js'),
util = require('../utils/util.js');

module.exports.forgotPassword = function(req, res, transporter) {
Expand All @@ -11,12 +12,13 @@ module.exports.forgotPassword = function(req, res, transporter) {

User.update({'local.username': username},{'local.password': newHash}, function(err, numAffected) {
if(err) {
logger.error(username, 'was trying to reset his / her password:\n' + err);
res.send(err);
}
if(numAffected.n == 1) {
User.findOne({'local.username': username}, function (err, results){
if(err) {
console.log(new Date + ' | ERROR | ' + username + ' | was trying to reset his / her password:\n' + err);
logger.error(username, 'was trying to reset his / her password:\n' + err);
res.send(err);
}
else {
Expand All @@ -25,18 +27,18 @@ module.exports.forgotPassword = function(req, res, transporter) {
mailOptions.text = util.fillUp(mailOptions.text, {'username': username, 'newPassword': newOne});
transporter.sendMail(mailOptions, function(err, info){
if(err){
console.log(new Date + ' | ERROR | ' + username + ' | was trying to reset his / her password:\n' + err);
logger.error(username, 'was trying to reset his / her password:\n' + err);
res.json({message: 'ko: mailer issue'});
}
else{
res.json({ message: 'ok' });
console.log(new Date + ' | SUCCESS | ' + username + ' | just reset his / her password. An email was sent to him / her.');
logger.info(username, 'just reset his / her password. An email was sent to him / her.');
}
});
}
});
} else {
console.log(new Date + ' | ERROR | ' + username + ' | was trying to reset his / her password but several accounts had this username. This is abnormal.');
logger.error(username, 'was trying to reset his / her password but several accounts had this username. This is abnormal.');
res.json({message: 'ko'});
}

Expand All @@ -48,16 +50,16 @@ module.exports.getProfile = function(req, res) {
if(!req.isAuthenticated()) {
res.statusCode = 401;
res.json({message: 'ko'});
console.log(new Date + ' | WARNING | ' + username + ' | tried to get his / her profile without being authenticated. Redirecting.');
logger.internalWarning('Just received an unauthenticated getProfile request. Redirecting.');
}
else {
User.findById(req._passport.session.user, function (err, doc){
if(err) {
console.log(new Date + ' | ERROR | ' + username + ' | was trying to get his / her profile:\n' + err);
logger.error(username, 'was trying to get his / her profile:\n' + err);
}
else {
res.json({username: doc._doc.local.username, email: doc._doc.local.email});
console.log(new Date + ' | SUCCESS | ' + username + ' | got his / her profile.');
logger.info(username, 'got his / her profile.');
}
});
}
Expand All @@ -68,7 +70,7 @@ module.exports.changeEmail = function(req, res) {
if(!req.isAuthenticated()) {
res.statusCode = 401;
res.json({message: 'ko'});
console.log(new Date + ' | WARNING | ' + username + ' | tried to change his / her email without being authenticated. Redirecting.');
logger.internalWarning('Just received an unauthenticated changeEmail request. Redirecting.');
}
else {
var givenPassword = req.body.password;
Expand All @@ -84,15 +86,15 @@ module.exports.changeEmail = function(req, res) {
if(user.validPassword(givenPassword)) {
User.update({'local.username': username},{'local.email': newEmail}, function(err, numAffected) {
if(err){
console.log(new Date + ' | ERROR | ' + username + ' | was trying to change his / her email:\n' + err);
logger.error(username, 'was trying to change his / her email:\n' + err);
res.send(err);
}
if(numAffected.n == 1) {
res.json({ message: 'ok' });
console.log(new Date + ' | SUCCESS | ' + username + ' | changed his / her email.');
logger.info(username, 'changed his / her email.');
} else {
res.json({message: 'ko'});
console.log(new Date + ' | ERROR | ' + username + ' | was trying to change his / her email but several accounts had this username. This is abnormal.');
logger.error(username, 'was trying to change his / her email but several accounts had this username. This is abnormal.');
}
});
} else {
Expand All @@ -108,7 +110,7 @@ module.exports.changePassword = function(req, res) {
if(!req.isAuthenticated()) {
res.statusCode = 401;
res.json({message: 'ko: you are not authenticated.'});
console.log(new Date + ' | WARNING | ' + username + ' | tried to change his / her email without being authenticated. Redirecting.');
logger.internalWarning('Just received an unauthenticated changePassword request. Redirecting.');
}
else {
var givenPassword = req.body.password;
Expand All @@ -123,15 +125,15 @@ module.exports.changePassword = function(req, res) {
if(user.validPassword(givenPassword)) {
User.update({'local.username': username},{'local.password': newHash}, function(err, numAffected) {
if(err){
console.log(new Date + ' | ERROR | ' + username + ' | was trying to change his / her password:\n' + err);
logger.error(username, 'was trying to change his / her password:\n' + err);
res.send(err);
}
if(numAffected.n == 1) {
res.json({ message: 'ok' });
console.log(new Date + ' | SUCCESS | ' + username + ' | changed his / her password.');
logger.info(username, 'changed his / her password.');
} else {
res.json({message: 'ko'});
console.log(new Date + ' | ERROR | ' + username + ' | was trying to change his / her password but several accounts had this username. This is abnormal.');
logger.error(username, 'was trying to change his / her password but several accounts had this username. This is abnormal.');
}
});
} else {
Expand All @@ -147,7 +149,7 @@ module.exports.deleteAccount = function(req, res) {
if(!req.isAuthenticated()) {
res.statusCode = 401;
res.json({message: 'ko: you are not authenticated.'});
console.log(new Date + ' | WARNING | ' + username + ' | tried to delete his / her account without being authenticated. Redirecting.');
logger.internalWarning('Just received an unauthenticated deleteAccount request. Redirecting.');
}
else {
var givenPassword = req.body.password;
Expand All @@ -162,18 +164,18 @@ module.exports.deleteAccount = function(req, res) {
if(user.validPassword(givenPassword)) {
UserObject.remove({user_name: username}, function(err, product) {
if(err) {
console.log(new Date + ' | ERROR | ' + username + ' | was trying to delete his / her account:\n' + err);
logger.error(username, 'was trying to delete his / her account:\n' + err);
res.send(err);
}
else {
console.log(new Date + ' | SUCCESS | ' + username + ' | triggered the deletion of all his / her objects while deleting his / her account.');
logger.info(username, 'triggered the deletion of all his / her objects while deleting his / her account.');
User.remove({_id: req._passport.session.user}, function(err, product) {
if(err) {
console.log(new Date + ' | ERROR | ' + username + ' | was trying to delete his / her account:\n' + err);
logger.error(username, 'was trying to delete his / her account:\n' + err);
res.send(err);
}
else {
console.log(new Date + ' | SUCCESS | ' + username + ' | just deleted his / her account.');
logger.info(username, 'just deleted his / her account.');
}
})
}
Expand Down
10 changes: 5 additions & 5 deletions server/utils/configUtil.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
var configuration = {},
util = require('./util.js');
logger = require('./logUtil.js'),
util = require('./util.js');

module.exports.get = function(key) {
if(configuration.hasOwnProperty(key)) {
return configuration[key];
}
else {
if(util.isEmpty(configuration)) {
console.log(new Date + ' | INTERNAL ERROR | The configuration is not loaded.');
logger.internalError('The configuration is not loaded.');
}
else {
console.log(configuration.length);
console.log(new Date + ' | INTERNAL ERROR | The key : \'' + key + '\' should exist in the configuration but does not.');
logger.internalError('The key : \'' + key + '\' should exist in the configuration but does not.');
}
return '';
}
}

module.exports.loadConfig = function() {
configuration = require('../../configuration.json');
console.log(new Date + ' | INTERNAL SUCCESS | Configuration succesfully loaded.');
logger.internalInfo('Configuration succesfully loaded.');
}
Loading

0 comments on commit 811d2a3

Please sign in to comment.