Skip to content

Commit

Permalink
Closes #858
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed May 15, 2013
1 parent c6314bf commit eb204c5
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 58 deletions.
22 changes: 11 additions & 11 deletions lib/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ internals.Auth.prototype.authenticate = function (request, next) {

request.auth.isAuthenticated = false;
request.auth.credentials = null;
request._log(['auth', 'unauthenticated']);
request.log(['hapi', 'auth', 'unauthenticated']);
return next();
}

Expand All @@ -213,10 +213,10 @@ internals.Auth.prototype.authenticate = function (request, next) {

if (err) {
if (options.log) {
request._log(['auth', 'error', config.strategies[strategyPos]].concat(options.log.tags), options.log.data);
request.log(['hapi', 'auth', 'error', config.strategies[strategyPos]].concat(options.log.tags), options.log.data);
}
else {
request._log(['auth', 'unauthenticated'], err);
request.log(['hapi', 'auth', 'unauthenticated'], err);
}

if (err instanceof Error === false || // Not an actual error (e.g. redirect, custom response)
Expand All @@ -227,7 +227,7 @@ internals.Auth.prototype.authenticate = function (request, next) {
request.auth.isAuthenticated = false;
request.auth.credentials = credentials;
request.auth.artifacts = options.artifacts;
request._log(['auth', 'unauthenticated', 'try'], err);
request.log(['hapi', 'auth', 'unauthenticated', 'try'], err);
return next();
}

Expand All @@ -254,7 +254,7 @@ internals.Auth.prototype.authenticate = function (request, next) {
if (config.scope &&
(!credentials || !credentials.scope || credentials.scope.indexOf(config.scope) === -1)) {

request._log(['auth', 'scope', 'error'], { got: credentials && credentials.scope, need: config.scope });
request.log(['hapi', 'auth', 'scope', 'error'], { got: credentials && credentials.scope, need: config.scope });
return next(Boom.forbidden('Insufficient scope (\'' + config.scope + '\' expected)'));
}

Expand All @@ -264,7 +264,7 @@ internals.Auth.prototype.authenticate = function (request, next) {
if (tos &&
(!credentials || !credentials.tos || !Semver.satisfies(credentials.tos, tos))) {

request._log(['auth', 'tos', 'error'], { min: tos, received: credentials && credentials.tos });
request.log(['hapi', 'auth', 'tos', 'error'], { min: tos, received: credentials && credentials.tos });
return next(Boom.forbidden('Insufficient TOS accepted'));
}

Expand All @@ -275,7 +275,7 @@ internals.Auth.prototype.authenticate = function (request, next) {
// Entity: 'any'

if (entity === 'any') {
request._log(['auth']);
request.log(['hapi', 'auth']);
request.auth.isAuthenticated = true;
return next();
}
Expand All @@ -284,23 +284,23 @@ internals.Auth.prototype.authenticate = function (request, next) {

if (entity === 'user') {
if (!credentials || !credentials.user) {
request._log(['auth', 'error'], 'User credentials required');
request.log(['hapi', 'auth', 'error'], 'User credentials required');
return next(Boom.forbidden('Application credentials cannot be used on a user endpoint'));
}

request._log(['auth']);
request.log(['hapi', 'auth']);
request.auth.isAuthenticated = true;
return next();
}

// Entity: 'app'

if (credentials && credentials.user) {
request._log(['auth', 'error'], 'App credentials required');
request.log(['hapi', 'auth', 'error'], 'App credentials required');
return next(Boom.forbidden('User credentials cannot be used on an application endpoint'));
}

request._log(['auth']);
request.log(['hapi', 'auth']);
request.auth.isAuthenticated = true;
return next();
};
Expand Down
6 changes: 3 additions & 3 deletions lib/ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internals.Ext.prototype.invoke = function (request, event, callback) {

Async.forEachSeries(handlers, function (ext, next) {

internals.Ext.runProtected(request._log.bind(request), event, next, function (enter, exit) {
internals.Ext.runProtected(request.log.bind(request), event, next, function (enter, exit) {

enter(function () {

Expand All @@ -110,7 +110,7 @@ internals.Ext.runProtected = function (log, tags, next, setup) { // setup
var finish = function () {

if (isFinished) {
log(['duplicate', 'next', 'error'].concat(tags || []));
log(['hapi', 'duplicate', 'next', 'error'].concat(tags || []));
return;
}

Expand All @@ -124,7 +124,7 @@ internals.Ext.runProtected = function (log, tags, next, setup) { // setup

domain.once('error', function (err) {

log(['uncaught'].concat(tags || []), err); // 'uncaught' treated special in request._log
log(['hapi', 'uncaught'].concat(tags || []), err); // 'uncaught' treated special in request.log
return finish(Boom.internal('Uncaught error', err));
});

Expand Down
36 changes: 15 additions & 21 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ exports = module.exports = internals.Request = function (server, req, res, optio
agent: this.raw.req.headers['user-agent']
};

this._log(['received'], about, now); // Must be last for object to be fully constructed
this.log(['hapi', 'received'], about, now); // Must be last for object to be fully constructed
};


Expand Down Expand Up @@ -154,12 +154,6 @@ internals.Request.prototype._setMethod = function (method) {
};


internals.Request.prototype._log = function (tags, data, timestamp) {

this.log(['hapi'].concat(tags), data, timestamp);
};


internals.Request.prototype.log = function (tags, data, timestamp) {

tags = (tags instanceof Array ? tags : [tags]);
Expand Down Expand Up @@ -311,7 +305,7 @@ internals.Request.prototype._execute = function (route) {
Async.forEachSeries(funcs, function (func, next) {

if (self._isReplied) {
self._log(['server', 'timeout']);
self.log(['hapi', 'server', 'timeout']);
return next(true); // Argument is ignored but aborts the series
}

Expand Down Expand Up @@ -375,7 +369,7 @@ internals.Request.prototype._reply = function (exit, callback) {

self._route.cache.drop(self.url.path, function (err) {

self._log(['cache', 'drop'], err);
self.log(['hapi', 'cache', 'drop'], err);
});
}

Expand All @@ -390,7 +384,7 @@ internals.Request.prototype._reply = function (exit, callback) {

var error = (self._response.isBoom ? self._response : self._response._err);
self.server.emit('internalError', self, error);
self._log(['internal'], error);
self.log(['hapi', 'internal'], error);
}

self.server.emit('response', self);
Expand Down Expand Up @@ -513,17 +507,17 @@ internals.Request.bindPre = function (pre) {

return function (request, next) {

Ext.runProtected(request._log.bind(request), 'pre', next, function (enter, exit) {
Ext.runProtected(request.log.bind(request), 'pre', next, function (enter, exit) {

var timer = new Utils.Timer();
var finalize = function (result) {

if (result instanceof Error) {
request._log(['pre', 'error'], { msec: timer.elapsed(), assign: pre.assign, mode: pre.mode, error: result });
request.log(['hapi', 'pre', 'error'], { msec: timer.elapsed(), assign: pre.assign, mode: pre.mode, error: result });
return exit(result);
}

request._log(['pre'], { msec: timer.elapsed(), assign: pre.assign, mode: pre.mode });
request.log(['hapi', 'pre'], { msec: timer.elapsed(), assign: pre.assign, mode: pre.mode });
if (pre.assign) {
request.pre[pre.assign] = result;
}
Expand Down Expand Up @@ -564,7 +558,7 @@ internals.handler = function (request, next) {

request._route.cache.getOrGenerate(request.url.path, generate, function (err, value, cached, report) { // request.url.path contains query

request._log(['cache', 'get'], report);
request.log(['hapi', 'cache', 'get'], report);

if (err) {
return store(err);
Expand Down Expand Up @@ -617,7 +611,7 @@ internals.handler = function (request, next) {
return callback(err);
}

Ext.runProtected(request._log.bind(request), 'handler', callback, function (enter, exit) {
Ext.runProtected(request.log.bind(request), 'handler', callback, function (enter, exit) {

var timer = new Utils.Timer();

Expand All @@ -628,11 +622,11 @@ internals.handler = function (request, next) {
if (response &&
(response.isBoom || response.varieties.error)) {

request._log(['handler', 'error'], { msec: timer.elapsed() });
request.log(['hapi', 'handler', 'error'], { msec: timer.elapsed() });
return exit(response);
}

request._log(['handler'], { msec: timer.elapsed() });
request.log(['hapi', 'handler'], { msec: timer.elapsed() });
return exit(null, response, !response.varieties.cacheable);
};

Expand Down Expand Up @@ -677,12 +671,12 @@ internals.Request.prototype._addTail = function (name) {
name = name || 'unknown';
var tailId = this._tailIds++;
this._tails[tailId] = name;
this._log(['tail', 'add'], { name: name, id: tailId });
this.log(['hapi', 'tail', 'add'], { name: name, id: tailId });

var drop = function () {

if (!self._tails[tailId]) {
self._log(['tail', 'remove', 'error'], { name: name, id: tailId }); // Already removed
self.log(['hapi', 'tail', 'remove', 'error'], { name: name, id: tailId }); // Already removed
return;
}

Expand All @@ -691,11 +685,11 @@ internals.Request.prototype._addTail = function (name) {
if (Object.keys(self._tails).length === 0 &&
self._isWagging) {

self._log(['tail', 'remove', 'last'], { name: name, id: tailId });
self.log(['hapi', 'tail', 'remove', 'last'], { name: name, id: tailId });
self.server.emit('tail', self);
}
else {
self._log(['tail', 'remove'], { name: name, id: tailId });
self.log(['hapi', 'tail', 'remove'], { name: name, id: tailId });
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/response/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ exports._respond = function (item, request, callback) {
response._transmit(request, function () {

request._response = response; // Error occurs late and should update request.response object
request._log(['response', response.variety]);
request.log(['hapi', 'response', response.variety]);
return callback();
});
};
Expand Down
8 changes: 1 addition & 7 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,6 @@ internals.Server.prototype._stop = function (options, callback) {
};


internals.Server.prototype._log = function (tags, data, timestamp) {

this.log(['hapi'].concat(tags), data, timestamp);
};


internals.Server.prototype.log = function (tags, data, timestamp) {

tags = (tags instanceof Array ? tags : [tags]);
Expand Down Expand Up @@ -417,7 +411,7 @@ internals.Server.prototype.helper = internals.Server.prototype.addHelper = funct

var key = settings.generateKey.apply(null, args);
if (key === null) { // Value can be ''
self._log(['helper', 'key', 'error'], { name: name, args: args });
self.log(['hapi', 'helper', 'key', 'error'], { name: name, args: args });
}

cache.getOrGenerate(key, generateFunc, function (err, value, cached, report) {
Expand Down
2 changes: 1 addition & 1 deletion lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ exports.parseCookies = function (request, next) {
if (request.server.settings.state.cookies.failAction === 'log' ||
request.server.settings.state.cookies.failAction === 'error') {

request._log(['state', 'error'], error);
request.log(['hapi', 'state', 'error'], error);
}

if (request.server.settings.state.cookies.failAction === 'error') {
Expand Down
2 changes: 1 addition & 1 deletion lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ exports.response = function (request, next) {
}

if (request.route.validate.response.failAction === 'log') {
request._log(['validation', 'error'], error.message);
request.log(['hapi', 'validation', 'error'], error.message);
return next();
}

Expand Down
18 changes: 9 additions & 9 deletions test/unit/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('Auth', function () {
}
},
auth: {},
_log: function () { },
log: function () { },
raw: {
res: {
setHeader: function () { }
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('Auth', function () {
strategies: ['default']
}
},
_log: function () { },
log: function () { },
server: server
};

Expand Down Expand Up @@ -235,7 +235,7 @@ describe('Auth', function () {
strategies: ['default']
}
},
_log: function () { },
log: function () { },
server: server
};

Expand Down Expand Up @@ -267,7 +267,7 @@ describe('Auth', function () {
strategies: ['default']
}
},
_log: function () { },
log: function () { },
server: server
};

Expand Down Expand Up @@ -297,7 +297,7 @@ describe('Auth', function () {
strategies: ['default']
}
},
_log: function () { },
log: function () { },
server: server
};

Expand Down Expand Up @@ -330,7 +330,7 @@ describe('Auth', function () {
strategies: ['default']
}
},
_log: function () { },
log: function () { },
server: server
};

Expand Down Expand Up @@ -363,7 +363,7 @@ describe('Auth', function () {
strategies: ['default']
}
},
_log: function () { },
log: function () { },
server: server
};

Expand Down Expand Up @@ -395,7 +395,7 @@ describe('Auth', function () {
strategies: ['default']
}
},
_log: function () { },
log: function () { },
server: server,
host: 'localhost'
};
Expand Down Expand Up @@ -427,7 +427,7 @@ describe('Auth', function () {
auth: {
credentials: {}
},
_log: function () { },
log: function () { },
raw: {
res: {
setHeader: function () { }
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Ext', function () {
});

var request = {
_log: function () { }
log: function () { }
};

ext.invoke(request, 'onRequest', function (err) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('State', function () {
state: settings || Defaults.server.state
}
},
_log: function (tags, data) {
log: function (tags, data) {

logged = true;
},
Expand Down
Loading

0 comments on commit eb204c5

Please sign in to comment.