Parent

Included Modules

Class Index [+]

Quicksearch

Ramaze::Logger::Informer

A minimal logger for Ramaze, supports files, CLI, colors and some customization.

Constants

COLORS

Which tag should be in what color

Attributes

out[RW]
colorize[RW]
log_levels[RW]

Public Class Methods

new(out = $stdout, log_levels = [:debug, :error, :info, :warn]) click to toggle source

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

Public Instance Methods

closed?() click to toggle source

Is @out closed?

     # File lib/ramaze/log/informer.rb, line 144
144:       def closed?
145:         @out.respond_to?(:closed?) and @out.closed?
146:       end
log(tag, *messages) click to toggle source

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
log_interpolate(prefix, text, time = timestamp) click to toggle source

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
shutdown() click to toggle source

Close the file we log to if it isn’t closed already.

    # File lib/ramaze/log/informer.rb, line 83
83:       def shutdown
84:         if @out.respond_to?(:close)
85:           Log.debug("close, #{@out.inspect}")
86:           @out.close
87:         end
88:       end
timestamp() click to toggle source

This uses timestamp trait or a date in the format of

  %Y-%m-%d %H:%M:%S
  # => "2007-01-19 21:09:32"
     # File lib/ramaze/log/informer.rb, line 136
136:       def timestamp
137:         mask = class_trait[:timestamp]
138:         Time.now.strftime(mask || "%Y-%m-%d %H:%M:%S")
139:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.