Skip to content

Instantly share code, notes, and snippets.

@aviddiviner
Last active December 26, 2015 16:19
Show Gist options
  • Save aviddiviner/7178611 to your computer and use it in GitHub Desktop.
Save aviddiviner/7178611 to your computer and use it in GitHub Desktop.
A simple logger to STDERR
# A simple logger to STDERR.
#
# log = SimpleLogger.new
# log.info "Helpful log message"
# log.debug "Some debugging output"
#
class SimpleLogger
LEVELS = %w(debug info warn error fatal)
def initialize(level = :debug)
@visible = LEVELS[LEVELS.index(level.to_s)..(-1)]
end
def respond_to?(meth)
LEVELS.include?(meth.to_s) ? true : super
end
def method_missing(meth, *args, &block)
(respond_to?(meth)) ? print_log(meth.to_s, args.join) : super
end
def print_log(type, msg)
return unless @visible.include?(type)
STDERR.puts "#{Time.now.utc} [#{type.upcase}] #{msg}"
end
end
$log = SimpleLogger.new(:info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment