Permalink
Browse files

better logging

  • Loading branch information...
1 parent 07d7368 commit 1048d6439eb9abe5d82ab6291eefac325af878c3 @mnutt mnutt committed Jan 15, 2012
Showing with 66 additions and 7 deletions.
  1. +1 −0 .gitignore
  2. +6 −3 app.js
  3. +5 −3 lib/modules/github.js
  4. +52 −0 lib/winston.js
  5. 0 log/.gitkeep
  6. +2 −1 package.json
View
@@ -1,2 +1,3 @@
/node_modules
/conf.js
+/log/*
View
@@ -5,6 +5,7 @@ var conf = require('./conf');
var everyauth = require('everyauth');
var Proxy = require('./lib/proxy');
var github = require('./lib/modules/github');
+var log = require('./lib/winston');
var proxy = new Proxy(conf.proxyTo.host, conf.proxyTo.port);
@@ -13,12 +14,12 @@ github.setup(everyauth);
function userCanAccess(req) {
var auth = req.session.auth;
if(!auth) {
- console.log("User rejected because they haven't authenticated.");
+ log.debug("User rejected because they haven't authenticated.");
return false;
}
for(var authType in req.session.auth) {
- if(everyauth[authType].authorize(auth)) { return true; }
+ if(everyauth[authType] && everyauth[authType].authorize(auth)) { return true; }
}
return false;
@@ -43,7 +44,7 @@ var connectSession = connect.session({secret: conf.sessionSecret,
var proxyMiddleware = proxy.middleware();
var app = express.createServer(
- connect.logger(),
+ log.middleware(),
connect.cookieParser(),
connectSession,
checkUser,
@@ -64,3 +65,5 @@ app.on('upgrade', function(req, socket, head) {
});
app.listen(conf.port);
+
+log.notice("Doorman on duty, listening on port " + conf.port + " and proxying to " + conf.proxyTo.host + ":" + conf.proxyTo.port + ".");
@@ -1,15 +1,15 @@
var conf = require('../../conf');
var url = require('url');
+var log = require('../winston');
var checkOrganization = function(auth) {
for(var i = 0; i < auth.github.user.organizations.length; i++) {
var organization = auth.github.user.organizations[i];
if(organization.login == conf.github.requiredOrganization) { return true; }
}
- if(true || conf.github.debug == true) {
- console.debug("User rejected because they aren't in the '" + conf.github.requiredOrganization + "' organization.");
- }
+ log.notice("User rejected because they aren't in the '" + conf.github.requiredOrganization + "' organization.");
+
return false;
};
@@ -42,4 +42,6 @@ exports.setup = function(everyauth) {
everyauth.github.authorize = checkOrganization;
everyauth.github.title = "Github";
+
+ log.notice("Registered Github authentication for members of organization '" + conf.github.requiredOrganization + "'.");
};
View
@@ -0,0 +1,52 @@
+var winston = require('winston');
+var fs = require('fs');
+
+winston.setLevels(winston.config.syslog.levels);
+winston.remove(winston.transports.Console);
+
+var env = process.env.NODE_ENV || 'development';
+
+if(env == 'development') {
+ winston.add(winston.transports.Console, { colorize: true });
+}
+
+var logDir = __dirname + "/../log";
+if(!fs.statSync(logDir)) {
+ fs.mkdirSync();
+}
+
+winston.add(winston.transports.File, {
+ filename: 'log/' + env + '.log',
+ timestamp: true
+});
+
+// connect middleware
+winston.middleware = function(req, res, next) {
+ var self = this;
+
+ function quote(str) {
+ return '"' + str + '"';
+ }
+
+ return function(req, res, next) {
+ var remoteIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
+ var httpVersion = req.httpVersionMajor + '.' + req.httpVersionMinor;
+ var httpSignature = [req.method, req.originalUrl, 'HTTP/' + httpVersion].join(' ');
+
+ next();
+
+ self.info([
+ remoteIp,
+ '-',
+ '-',
+ '[' + new Date().toUTCString() + ']',
+ quote(httpSignature),
+ res.statusCode,
+ (res._headers || {})['content-length'],
+ quote(req.headers['referer'] || req.headers['referrer'] || ''),
+ quote(req.headers['user-agent'])
+ ].join(' '));
+ };
+};
+
+module.exports = winston;
View
No changes.
View
@@ -11,6 +11,7 @@
"http-proxy": "0.8.0",
"express": "2.5.6",
"connect": "1.8.5",
- "jade": "0.20.0"
+ "jade": "0.20.0",
+ "winston": "0.5.9"
}
}

0 comments on commit 1048d64

Please sign in to comment.