Skip to content

Instantly share code, notes, and snippets.

@pulkitsinghal
Created September 15, 2015 06:29
Show Gist options
  • Save pulkitsinghal/6c645ee365f9bf25aa36 to your computer and use it in GitHub Desktop.
Save pulkitsinghal/6c645ee365f9bf25aa36 to your computer and use it in GitHub Desktop.
Logging Permutations - Path to insanity or Enlightenment?
'use strict';
var loopback = require('loopback');
var Promise = require('bluebird');
var path = require('path');
var fileName = path.basename(__filename, '.js'); // gives the filename without the .js extension
var log = require('debug')('common:models:'+fileName);
//var logger = require('./../lib/logger')('common:models:'+fileName);
/*var logE = function(){
//console.log(arguments);
var params = Array.prototype.slice.call(arguments);
if(params && params.length > 0) {
var ctx = loopback.getCurrentContext();
var currentUser = ctx && ctx.get('currentUser');
if (currentUser) {
params.unshift(currentUser.username);
log.apply(null,params);
}
}
};*/
//var logF = function(){
// //console.log(arguments);
// var params = Array.prototype.slice.call(arguments);
// if(params && params.length > 0) {
// var ctx = loopback.getCurrentContext();
// if (ctx) {
// var identifier = ctx.get('ip') +
// '-' + ctx.get('username')/* +
// '-' + ctx.get('accessToken') +
// '-' + ctx.get('requestId')*/;
// params.unshift(identifier);
// /*var stack = new Error().stack;
// params.push(stack);*/
// log.apply(null,params);
// }
// }
//};
var loggerF = require('./../lib/loggerF')('common:models:'+fileName);
module.exports = function(Model, options) {
Model.getCurrentUserModel = function(cb) {
var ctx = loopback.getCurrentContext();
var currentUser = ctx && ctx.get('currentUser');
if (currentUser) {
//logE('inside ' + Model.definition.name + '.getCurrentUserModel() - currentUser: ', currentUser.username);
//logF('inside ' + Model.definition.name + '.getCurrentUserModel() - currentUser: ', currentUser.username);
//logger('getCurrentUserModel').debug('currentUser: ', currentUser.username);
loggerF('getCurrentUserModel').debug('currentUser: ', currentUser.username);
//return currentUser;
return Promise.promisifyAll(
currentUser,
{
filter: function(name, func, target){
return !( name == 'validate');
}
}
);
}
else {
// TODO: when used with core invocations, the call stack can end up here
// this error only makes sense to point out failures in RESTful calls
// how can this sanity check be made any better?
cb('401 - unauthorized - how did we end up here? should we manage ACL access to remote methods ourselves?');
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment