Class: Log4r::Outputter

Inherits:
Monitor
  • Object
show all
Defined in:
lib/log4r-color/outputter/outputter.rb,
lib/log4r-color/outputter/staticoutputter.rb,
lib/log4r-color/outputter/outputterfactory.rb

Defined Under Namespace

Classes: OutputterFactory

Constant Summary collapse

@@outputters =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_name, hash = {}) ⇒ Outputter

An Outputter needs a name. RootLogger will be loaded if not already done. The hash arguments are as follows:

:level

Logger level. Optional, defaults to root level

:formatter

A Formatter. Defaults to DefaultFormatter



28
29
30
31
32
33
34
35
36
# File 'lib/log4r-color/outputter/outputter.rb', line 28

def initialize(_name, hash={})
  super()
  if _name.nil?
    raise ArgumentError, "Bad arguments. Name and IO expected.", caller
  end
  @name = _name
  validate_hash(hash)
  @@outputters[@name] = self
end

Instance Attribute Details

#colorsObject

Returns the value of attribute colors.



19
20
21
# File 'lib/log4r-color/outputter/outputter.rb', line 19

def colors
  @colors
end

#formatterObject

Returns the value of attribute formatter.



19
20
21
# File 'lib/log4r-color/outputter/outputter.rb', line 19

def formatter
  @formatter
end

#levelObject

Returns the value of attribute level.



19
20
21
# File 'lib/log4r-color/outputter/outputter.rb', line 19

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/log4r-color/outputter/outputter.rb', line 19

def name
  @name
end

Class Method Details

.[](name) ⇒ Object

Retrieve an outputter.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/log4r-color/outputter/staticoutputter.rb', line 6

def self.[](name)
out = @@outputters[name]
  if out.nil?
    return case name
    when 'color'  then ColorOutputter.new 'color'
    when 'stdout' then StdoutOutputter.new 'stdout'
    when 'stderr' then StderrOutputter.new 'stderr'
    else nil end
  end          
  out
end

.[]=(name, outputter) ⇒ Object

Set an outputter.



21
22
23
# File 'lib/log4r-color/outputter/staticoutputter.rb', line 21

def self.[]=(name, outputter)
  @@outputters[name] = outputter
end

.colorObject



17
# File 'lib/log4r-color/outputter/staticoutputter.rb', line 17

def self.color;  Outputter['color'] end

.eachObject

Yields each outputter’s name and reference.



25
26
27
# File 'lib/log4r-color/outputter/staticoutputter.rb', line 25

def self.each
  @@outputters.each {|name, outputter| yield name, outputter}
end

.each_outputterObject



28
29
30
# File 'lib/log4r-color/outputter/staticoutputter.rb', line 28

def self.each_outputter
  @@outputters.each_value {|outputter| yield outputter}
end

.stderrObject



19
# File 'lib/log4r-color/outputter/staticoutputter.rb', line 19

def self.stderr; Outputter['stderr'] end

.stdoutObject



18
# File 'lib/log4r-color/outputter/staticoutputter.rb', line 18

def self.stdout; Outputter['stdout'] end

Instance Method Details

#flushObject

Call flush to force an outputter to write out any buffered log events. Similar to IO#flush, so use in a similar fashion.



92
93
# File 'lib/log4r-color/outputter/outputter.rb', line 92

def flush
end

#level_key(lv) ⇒ Object



59
60
61
# File 'lib/log4r-color/outputter/outputter.rb', line 59

def level_key lv
  Log4rTools.level_key(lv)
end

#only_at(*levels) ⇒ Object

Set the levels to log. All others will be ignored

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/log4r-color/outputter/outputter.rb', line 47

def only_at(*levels)
  raise ArgumentError, "Gimme some levels!", caller if levels.empty?
  raise ArgumentError, "Can't log only_at ALL", caller if levels.include? ALL
  levels.each {|level| Log4rTools.validate_level(level)}
  @level = levels.sort.first
  OutputterFactory.create_methods self, levels
  Logger.log_internal {
    "Outputter '#{@name}' writes only on " +\
    levels.collect{|l| LNAMES[l]}.join(", ")
  }
end