Sha256: 0f68397d572b870ee77f2c293c601427f61cdcaa4c83c61bacd116fff41c807a
Contents?: true
Size: 1.09 KB
Versions: 1
Compression:
Stored size: 1.09 KB
Contents
require 'microevent' require 'paint' class MicroLogger include MicroEvent VERSION = "0.1.0".freeze 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
micrologger-0.1.0 | lib/micrologger.rb |