Class: Log4r::Outputter

Inherits:
Monitor
  • Object
show all
Defined in:
lib/log4r/outputter/outputter.rb,
lib/log4r/outputter/staticoutputter.rb,
lib/log4r/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/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

#formatterObject

Returns the value of attribute formatter.



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

def formatter
  @formatter
end

#levelObject

Returns the value of attribute level.



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

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/log4r/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
# File 'lib/log4r/outputter/staticoutputter.rb', line 6

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

.[]=(name, outputter) ⇒ Object

Set an outputter.



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

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

.eachObject

Yields each outputter’s name and reference.



23
24
25
# File 'lib/log4r/outputter/staticoutputter.rb', line 23

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

.each_outputterObject



26
27
28
# File 'lib/log4r/outputter/staticoutputter.rb', line 26

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

.stderrObject



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

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

.stdoutObject



16
# File 'lib/log4r/outputter/staticoutputter.rb', line 16

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.



81
82
# File 'lib/log4r/outputter/outputter.rb', line 81

def flush
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/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