Created
October 21, 2014 15:46
-
-
Save huafu/21e32cb673ee0d073573 to your computer and use it in GitHub Desktop.
Very simple logger to instrument objects and methods
This file contains 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
logger = | |
_anonymousIndex: 0 | |
guessMethodName: (method) -> | |
unless (res = (->).toString.call(method).match(/function([^\(]*)/)[1].replace(/(^\s+|\s+)$/g, '')) | |
res = logger.autoNamespace() | |
res | |
autoNamespace: -> | |
"[anonymous##{ ++logger._anonymousIndex }]" | |
instrumentMethod: (method, name = logger.guessMethodName(method)) -> | |
name = name.replace /\.prototype\./g, '#' | |
console.log "[logger] instrumenting method #{ name }..." | |
newMethod = -> | |
isNew = @constructor is newMethod | |
arrow = if isNew then '== new ==' else '== ( ) ==' | |
console.log "[logger] =#{ arrow }> entering #{ name }" | |
try | |
res = method.apply @, arguments | |
catch err | |
console.warn "[logger] !! #{ name } threw #{ err }" | |
err.captureStackTrace() | |
throw err | |
console.log "[logger] <#{ arrow }= exiting #{ name }" | |
res | |
newMethod.prototype = method.prototype | |
for own k, v of method | |
newMethod[k] = v | |
newMethod | |
instrumentObject: (object, namespace = logger.autoNamespace()) -> | |
for own k, v of object when typeof v is 'function' | |
object[k] = @instrumentMethod v, "#{ namespace }.#{ k }" | |
@ | |
module.exports = logger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was having hard time finding where was happening a silent error in some async workflow so I wrote this, maybe it could be useful for someone else so here it is.
The goal is to instrument existing classes, objects and methods so that it logs when it enter the method, exit it and when an error is triggered by it:
second argument is strongly recommended to have easy reading output.
then you get something like that in the console: