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_levels Array containing the levels that should be
logged.
# File lib/ramaze/log/informer.rb, line 54 def initialize(out = $stdout, log_levels = [:debug, :error, :info, :warn]) @colorize = false @out = case out when STDOUT, :stdout, 'stdout' $stdout when STDERR, :stderr, 'stderr' $stderr when IO out else if out.respond_to?(:puts) out else File.open(out.to_s, 'ab+') end end if @out.respond_to?(:tty?) and class_trait[:colorize] @colorize = @out.tty? end @log_levels = log_levels end
Is @out closed?
# File lib/ramaze/log/informer.rb, line 144 def closed? @out.respond_to?(:closed?) and @out.closed? 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 def log tag, *messages return if closed? || !@log_levels.include?(tag) messages.flatten! prefix = tag.to_s.upcase.ljust(5) if @colorize color = COLORS[tag] ||= :white prefix.replace prefix.send(color) end messages.each do |message| @out.puts(log_interpolate(prefix, message)) end @out.flush if @out.respond_to?(:flush) 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 def log_interpolate prefix, text, time = timestamp message = class_trait[:format].dup vars = { '%time' => time, '%prefix' => prefix, '%text' => text } vars.each{|from, to| message.gsub!(from, to) } message end
Generated with the Darkfish Rdoc Generator 2.