Last active
December 26, 2015 16:19
-
-
Save aviddiviner/7178611 to your computer and use it in GitHub Desktop.
A simple logger to STDERR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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