A minimal logger for Ramaze, supports files, CLI, colors and some customization.
Which tag should be in what color
Create a new instance of Informer.
@example
Informer.new # => logs to stdout with all levels being shown. Informer.new($stderr) # => same, but to stderr # same, but logs to the file foo.log (or creates it if it doesn't # exist yet) Informer.new("foo.log") Informer.new($stdout, [:info]) #=> show only #info messages to stdout.
@param [String] out Specifies where the output should go. By default
this is set to STDOUT.
@param [Array] log_leves Array containing the levels that should be
logged.
# File lib/ramaze/log/informer.rb, line 54 54: def initialize(out = $stdout, log_levels = [:debug, :error, :info, :warn]) 55: @colorize = false 56: 57: @out = 58: case out 59: when STDOUT, :stdout, 'stdout' 60: $stdout 61: when STDERR, :stderr, 'stderr' 62: $stderr 63: when IO 64: out 65: else 66: if out.respond_to?(:puts) 67: out 68: else 69: File.open(out.to_s, 'ab+') 70: end 71: end 72: 73: if @out.respond_to?(:tty?) and class_trait[:colorize] 74: @colorize = @out.tty? 75: end 76: 77: @log_levels = log_levels 78: end
Is @out closed?
# File lib/ramaze/log/informer.rb, line 144 144: def closed? 145: @out.respond_to?(:closed?) and @out.closed? 146: end
Integration to Logging
@param [String] tag The log level for the current message(s). @param [Array] messages Array containing the data that should be logged.
# File lib/ramaze/log/informer.rb, line 96 96: def log tag, *messages 97: return if closed? || !@log_levels.include?(tag) 98: messages.flatten! 99: 100: prefix = tag.to_s.upcase.ljust(5) 101: 102: if @colorize 103: color = COLORS[tag] ||= :white 104: prefix.replace prefix.send(color) 105: end 106: 107: messages.each do |message| 108: @out.puts(log_interpolate(prefix, message)) 109: end 110: 111: @out.flush if @out.respond_to?(:flush) 112: end
Takes the prefix (tag), text and timestamp and applies it to the :format trait.
@param [String] prefix @param [String] text @param [Integer] time
# File lib/ramaze/log/informer.rb, line 122 122: def log_interpolate prefix, text, time = timestamp 123: message = class_trait[:format].dup 124: 125: vars = { '%time' => time, '%prefix' => prefix, '%text' => text } 126: vars.each{|from, to| message.gsub!(from, to) } 127: 128: message 129: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.