Skip to content
This repository has been archived by the owner on May 9, 2020. It is now read-only.

Commit

Permalink
added basic logging
Browse files Browse the repository at this point in the history
  • Loading branch information
krasu committed Nov 11, 2013
1 parent 6de0c60 commit 3bd652f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 43 deletions.
38 changes: 20 additions & 18 deletions app.js
Expand Up @@ -4,14 +4,21 @@
* Time: 9:14 PM
*/
var http = require('http'),
path = require('path'),
express = require('express'),
cfg = require('./config'),
MongoStore = require('connect-mongo')(express),
mongoose = require('mongoose'),
app = express();
path = require('path'),
express = require('express'),
cfg = require('./config'),
logger = require('./app/utils/logger'),
MongoStore = require('connect-mongo')(express),
mongoose = require('mongoose'),
app = express();

mongoose.connect(cfg.mongodbUri)
mongoose.connection.on('error', function (err) {
logger.error('Mongo connection error', err.message);
});
mongoose.connection.once('open', function callback () {
logger.info("Connected to MongoDB");
});

// Bootstrap models
require('./app/models')
Expand Down Expand Up @@ -39,30 +46,25 @@ app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser(cfg.sessionSecret));
app.use(express.session({
secret: cfg.sessionSecret,
store: sessionStore
secret: cfg.sessionSecret,
store: sessionStore
}));
app.use(passport.initialize());
app.use(passport.session());
require('./app/middleware/users')(app)
app.use(app.router);
require('./app/routes')(app)
// development only
if ('development' == cfg.env) {
app.use(express.errorHandler());
} else {
require('./app/middleware/errors')(app)
}
require('./app/middleware/errors')(app)

var server = http.createServer(app)

require('./app/utils/socket.io')(server, sessionStore)

require('./app/utils/mailer').fillTemplates(path.join(__dirname, 'app/views/emails/'), function (error) {
if (error) throw error;
if (error) throw error;

server.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
server.listen(app.get('port'), function () {
logger.info('Express server listening on port ' + app.get('port'));
});
})

51 changes: 29 additions & 22 deletions app/middleware/errors.js
Expand Up @@ -3,33 +3,40 @@
* Date: 8/18/13
* Time: 7:35 PM
*/
var logger = require('winston')

module.exports = function (app) {
//handling request errors
app.use(function (req, res, next) {
res.status(404);
//handling request errors
app.use(function (req, res, next) {
res.status(404);
logger.warn('404 not found: %s', req.originalUrl)

if (req.accepts('html')) {
res.render('404');
return;
}
if (req.accepts('html')) {
res.render('404');
return;
}

if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}

res.type('txt').send('Not found');
});
res.type('txt').send('Not found');
});

app.use(function (err, req, res, next) {
res.status(err.status || 500);
app.use(function (err, req, res, next) {
res.status(err.status || 500);
logger.error('Route Error', {
error: err,
req: req
})

if (req.accepts('html')) {
return res.render('500', { error: err });
}
if (req.accepts('html')) {
return res.render('500', { error: err });
}

if (req.accepts('json')) {
return res.send({ error: err });
}
});
if (req.accepts('json')) {
return res.send({ error: err });
}
});
}
3 changes: 2 additions & 1 deletion app/middleware/passport.js
Expand Up @@ -7,6 +7,7 @@ var passport = require('passport'),
cfg = require('../../config'),
GitHubStrategy = require('passport-github').Strategy,
mongoose = require('mongoose'),
logger = require('winston'),
User = mongoose.model('User')

passport.serializeUser(function (user, done) {
Expand All @@ -20,7 +21,7 @@ passport.deserializeUser(function (obj, done) {
});

var callbackUrl = cfg.fullUrl() + '/auth/github/callback'
console.log('GitHub callback url is', callbackUrl)
logger.info('GitHub callback url is', callbackUrl)

passport.use(new GitHubStrategy({
clientID: cfg.github.clientId,
Expand Down
23 changes: 23 additions & 0 deletions app/utils/logger.js
@@ -0,0 +1,23 @@
/**
* Author: krasu
* Date: 10/17/13
* Time: 3:16 PM
*/
var winston = require('winston'),
cfg = require('../../config')

winston.remove(winston.transports.Console)
winston.add(winston.transports.Console, {
json: false,
timestamp: true,
handleExceptions: true
})
winston.add(winston.transports.DailyRotateFile, {
filename: cfg.logsDir + '/iloveopensource.log',
datePattern: '.yyyy-MM-dd',
handleExceptions: true
})



module.exports = winston;
3 changes: 2 additions & 1 deletion app/utils/mailer.js
Expand Up @@ -8,12 +8,13 @@ var cfg = require('../../config'),
_ = require('lodash'),
ejs = require('ejs'),
fs = require('fs'),
logger = require('winston'),
templates = {}

//TODO: add mail queue
module.exports.fillTemplates = function (dir, callback) {
if (!_.isEmpty(templates)) return
console.log('Reading email templates from', dir)
logger.info('Reading email templates from', dir)

fs.readdir(dir, function (err, files) {
if (err) return callback(err);
Expand Down
2 changes: 2 additions & 0 deletions config/index.js
Expand Up @@ -16,6 +16,8 @@ var config = {};
config.env = process.env.NODE_ENV || 'development';
config.isDev = config.env === 'development';

//config.logsDir absolute path for directory with logs, be sure it is writable
config.logsDir = '/var/log/iloveopensource';
//config.hostname used to create site links
config.hostname = 'www.iloveopensource.io';
//config.isHttps used to create site links
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -18,7 +18,8 @@
"passport.socketio": "~1.2.1",
"q": "~0.9.7",
"html-css-sanitizer": "0.0.3",
"moment": "~2.3.0"
"moment": "~2.3.0",
"winston": "~0.7.2"
},
"devDependencies": {
"grunt-contrib-less": "~0.6.5",
Expand Down

0 comments on commit 3bd652f

Please sign in to comment.