Created
January 22, 2016 15:46
-
-
Save lukejagodzinski/86c784f4b5127ef3ca7c to your computer and use it in GitHub Desktop.
Logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (Meteor.isServer) { | |
// Get connection object. | |
var connection = Meteor.server; | |
// Get connection constructor. | |
var Connection = connection.constructor; | |
// Get the original "apply" method. | |
var originalMethod = Connection.prototype.apply; | |
// Override the "apply" method. | |
Connection.prototype.apply = function(name, args, options, callback) { | |
if (!callback && typeof options === 'function') { | |
callback = options; | |
options = {}; | |
} | |
options = options || {}; | |
Log.trace({ | |
methodName: name | |
}, 'initializing method'); | |
// Asynchronous. | |
if (callback) { | |
return Log.withContext({ | |
methodName: name, | |
methodCallId: Random.id(), | |
userId: Meteor.userId && Meteor.userId() | |
}, function() { | |
callback = Meteor.bindEnvironment(callback); | |
var log = Meteor.bindEnvironment(function(err, result) { | |
Log.trace({ | |
xxx: 'xxx', | |
duration: Date.now() - start, | |
success: !err | |
}, 'meteor method called'); | |
return callback(err, result); | |
}); | |
return originalMethod.call(this, name, args, options, log); | |
}); | |
} | |
// Synchronous. | |
else { | |
return Log.withContext({ | |
methodName: name, | |
methodCallId: Random.id(), | |
userId: Meteor.userId && Meteor.userId() | |
}, function() { | |
try { | |
var start = Date.now(); | |
var err = true; | |
var result = originalMethod.call(this, name, args, options); | |
err = false; | |
return result; | |
} | |
finally { | |
Log.trace({ | |
xxx: 'xxx', | |
duration: Date.now() - start, | |
success: !err | |
}, 'meteor method called'); | |
} | |
}); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment