Skip to content

Instantly share code, notes, and snippets.

@ryandotsmith
Last active December 15, 2015 23:49
Show Gist options
  • Save ryandotsmith/5343120 to your computer and use it in GitHub Desktop.
Save ryandotsmith/5343120 to your computer and use it in GitHub Desktop.
A simple ruby logger that emits l2met conventional data
def log(data)
result = nil
if data.key?(:measure)
name = data.delete(:measure).insert(0, ENV["APP_NAME"] + ".")
end
if block_given?
start = Time.now
result = yield
elapsed = (Time.now.to_f - start.to_f) * 1000
data.merge!("measure.#{name}" => elapsed.round)
else
data.merge!("measure.#{name}" => 1)
end
data.reduce(out=String.new) do |s, tup|
quoted_tup = tup.last.is_a?(String) ? "\"#{tup.last}\"" : tup.last
s << [tup.first, quoted_tup].join("=") << " "
end
$stdout.puts(out)
return result
end
@ryandotsmith
Copy link
Author

Usage:

ENV["APP_NAME"] = "myapp"
def db_write(some_data)
  log(measure: "db.write") do
    DB.write(some_data)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment