Skip to content

Instantly share code, notes, and snippets.

@wils3005
Last active December 29, 2019 03:34
Show Gist options
  • Save wils3005/989e9d06be249688aa826e84cbd53d18 to your computer and use it in GitHub Desktop.
Save wils3005/989e9d06be249688aa826e84cbd53d18 to your computer and use it in GitHub Desktop.
# Called by wrapping arbitrary code-to-be-logged in a block. Its return value
# will be the return value of whatever's in the block. e.g.
# require '/Users/jack/lib/trace_logger'
# def my_method
# ...
# foo = some_buggy_method
# ...
# end
# def my_method
# ...
# foo = tl { some_buggy_method }
# ...
# end
module TraceLogger
delegate :root, to: :Rails
def tl
raise(LocalJumpError, 'no block given (yield)') unless block_given?
logger = Logger.new(root.join('log', "trace_#{object_id}.log"))
trace_point =
TracePoint.new(:call) do |it|
payload = {
file: it.path,
line: it.lineno,
method: it.method_id
}.to_json
logger.info(payload)
end
trace_point.enable
block_call = yield
trace_point.disable
block_call
end
end
Module.include(TraceLogger)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment