Skip to content

Instantly share code, notes, and snippets.

@andyl
Last active Aug 29, 2015
Embed
What would you like to do?
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