Sha256: a4ca1cf70ebfa832c2edda65d392e854004a56424aa6d5498e34c02ea7648ec1
Contents?: true
Size: 1.11 KB
Versions: 3
Compression:
Stored size: 1.11 KB
Contents
require_relative 'micrologger/version' require 'microevent' require 'paint' class MicroLogger include MicroEvent DEFAULT_HANDLERS = { stdout: lambda{ |message, extra| STDOUT.puts formatter(message, extra) }, stderr: lambda{ |message, extra| STDERR.puts Paint[formatter(message, extra), :red] }, } def log(message, level = :info, extra = {}) trigger level, message, {level: level, time: Time.now}.merge(extra) end def register(level = :info, handler = nil, &block) bind level, &resolve_handler(handler || block) end def unregister(level = :info, handler = nil, &block) if handler || block unbind level, &resolve_handler(handler || block) else unbind level end end private def resolve_handler(handler) if handler.is_a?(Proc) handler elsif default_handler = DEFAULT_HANDLERS[handler] default_handler else raise ArgumentError, "no suitable handler found for #{handler.inspect}" end end def formatter(message, extra) "#{extra[:time].strftime('%Y-%m-%d %H:%M')} | #{extra[:level]} | #{message}" end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
micrologger-0.1.3 | lib/micrologger.rb |
micrologger-0.1.2 | lib/micrologger.rb |
micrologger-0.1.1 | lib/micrologger.rb |