Skip to content

Instantly share code, notes, and snippets.

@andyl
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyl/84b0a6d9842ba7147e6c to your computer and use it in GitHub Desktop.
Save andyl/84b0a6d9842ba7147e6c to your computer and use it in GitHub Desktop.
Ruby Log Functions
module Kernel
def err_log(*msgs)
base_log(*(msgs.map(&:to_s)), char: '>', color: 'red')
end
def info_log(*msgs)
base_log(*(msgs.map(&:to_s)), char: '*', color: 'blue')
end
def green_log(*msgs)
base_log(*(msgs.map(&:to_s)), char: '-', color: 'green')
end
def purple_log(*msgs)
base_log(*(msgs.map(&:to_s)), char: '-', color: 'purple')
end
def dev_log(*msgs)
base_log(*(msgs.map(&:to_s)))
end
def tst_log(*msgs, color: 'yellow')
base_log(*(msgs.map(&:to_s)), run_on_test: true, color: color)
end
def data_log(*msgs)
return if Rails.env == 'test'
msgs.map(&:to_s).each do |msg|
puts msg.purple
$stdout.flush
end
end
private
def base_log(*msgs, char: '=', color: 'yellow', run_on_test: false)
return if Rails.env == 'test' && run_on_test == false
ref = caller[1] # introspect to get the caller reference
file = ref.match(/([^\/:]+):\d+:/)[1].split('.')[0] # extract the caller file
line = ref.match(/\:(\d+)\:/)[1] # extract the caller line number
meth = ref.match(/\`(.+)\'/)[1] # extract the calling method
pref = "#{(' ' + file).rjust(20, char)}:#{line.ljust(2)} #{(meth + ' ').ljust(30,'-')}"
msgs.each do |msg|
string = "#{char}#{pref}> #{msg}"
puts string.send(color)
$stdout.flush
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment