Class: Log4Ruby::Level

Inherits:
Object
  • Object
show all
Defined in:
lib/log4ruby/level.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, weight) ⇒ Level

Creates a new level.

Parameters:

  • name (String)

    the name of the level.

  • weight (Float)

    the weight for this level. Higher the weight, higher the severity.



58
59
60
61
# File 'lib/log4ruby/level.rb', line 58

def initialize(name, weight)
  @name = name
  @weight = weight
end

Instance Attribute Details

#nameObject (readonly)

The name of the level.



52
53
54
# File 'lib/log4ruby/level.rb', line 52

def name
  @name
end

#weightObject (readonly)

The weight of this level. Higher the value, more severe the log.



50
51
52
# File 'lib/log4ruby/level.rb', line 50

def weight
  @weight
end

Class Method Details

.parse(name) ⇒ Log4Ruby::Level

Parses the specified string in an attempt to convert it to a Level object.

Parameters:

  • name (String)

    the name of the level.

Returns:



44
45
46
47
# File 'lib/log4ruby/level.rb', line 44

def self.parse(name)
  name = name.upcase
  const_get(name)
end

.register(name, weight) ⇒ Object

Creates a new level with the specified name and weight. Only level’s created using this method are automatically made available on the Logger class. Any other level’s will have to be handled manually. The method with which it is made available is the same as the level, but with all lowercase characters.

Parameters:

  • name (String)

    the name of the new level. Will overwrite an existing level with the same name, if it exists. The name is converted to all uppercase characters.

  • weight (Float)

    the weight of the level. Higher the weight, higher the priority.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/log4ruby/level.rb', line 18

def self.register(name, weight)
  name = name.upcase
  method_name = name.downcase
  query_method_name = "#{method_name}?"
  level = Level.new(name, weight)
  const_set(name, level)
  # Create the logging method.
  Logger.send(:define_method, method_name) do |message, parameters = {}|
    return if self.closed?
    return if level < self.effective_level
    # Construct the log and dispatch it.
    parameters[:message] = message
    parameters[:level] = level
    parameters[:logger] = self
    self.process_log(Log.new(parameters))
  end
  # Create the query method - for checking the effective level of a logger / appender.
  Logger.send(:define_method, query_method_name) { self.effective_level <= level  }
  Appender.send(:define_method, query_method_name) { self.level <= level }
end

Instance Method Details

#<(other) ⇒ Object

Check if this level is less than the specified other log level. Comparison is done using the weight attribute

Parameters:



75
76
77
# File 'lib/log4ruby/level.rb', line 75

def <(other)
  self.weight < other.weight
end

#<=(other) ⇒ Object

Check if this level is less than or equal to the specified other log level. Comparison is done using the weight attribute

Parameters:



83
84
85
# File 'lib/log4ruby/level.rb', line 83

def <=(other)
  self.weight <= other.weight
end

#==(other) ⇒ Object

Check if this level is equal to the specified other log level. Comparison is done using the weight attribute

Parameters:



67
68
69
# File 'lib/log4ruby/level.rb', line 67

def ==(other)
  self.weight == other.weight
end

#>(other) ⇒ Object

Check if this level is greater than the specified other log level. Comparison is done using the weight attribute

Parameters:



91
92
93
# File 'lib/log4ruby/level.rb', line 91

def >(other)
  self.weight > other.weight
end

#>=(other) ⇒ Object

Check if this level is greater than or equal to the specified other log level. Comparison is done using the weight attribute

Parameters:



99
100
101
# File 'lib/log4ruby/level.rb', line 99

def >=(other)
  self.weight <= other.weight
end

#to_sString

Override to return the name of the level.

Returns:

  • (String)

    the level’s name.



106
107
108
# File 'lib/log4ruby/level.rb', line 106

def to_s
  @name
end