Class: Log4r::Log4rTools

Inherits:
Object
  • Object
show all
Defined in:
lib/log4r-color/base.rb

Overview

Some common functions

Class Method Summary collapse

Class Method Details

.comma_split(string) ⇒ Object

Splits comma-delimited lists with arbitrary s padding



86
87
88
# File 'lib/log4r-color/base.rb', line 86

def self.comma_split(string)
  string.split(/\s*,\s*/).collect {|s| s.strip}
end

.decode_bool(hash, symbol, default) ⇒ Object

Shortcut for decoding ‘true’, ‘false’, true, false or nil into a bool from a hash parameter. E.g., it looks for true/false values for the keys ‘symbol’ and :symbol.



75
76
77
78
79
80
81
82
83
# File 'lib/log4r-color/base.rb', line 75

def self.decode_bool(hash, symbol, default)
  data = hash[symbol]
  data = hash[symbol.to_s] if data.nil?
  return case data
    when 'true',true then true
    when 'false',false then false
    else default
    end
end

.level_key(level) ⇒ Object



63
64
65
# File 'lib/log4r-color/base.rb', line 63

def self.level_key level 
  valid_levels[level]
end

.max_level_str_sizeObject

:nodoc:



49
50
51
52
53
# File 'lib/log4r-color/base.rb', line 49

def self.max_level_str_size #:nodoc:
  size = 0
  LNAMES.each {|i| size = i.length if i.length > size}
  size
end

.valid_level?(lev) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/log4r-color/base.rb', line 45

def self.valid_level?(lev)
  not lev.nil? and lev.kind_of?(Numeric) and lev >= ALL and lev <= OFF
end

.valid_levelsObject



67
68
69
# File 'lib/log4r-color/base.rb', line 67

def self.valid_levels
  LNAMES.collect { |level| level.downcase.to_sym }
end

.validate_colors(colors) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
# File 'lib/log4r-color/base.rb', line 55

def self.validate_colors(colors)      
  raise ArgumentError, "Colors option must consist of a hash where each key is the log level to define a color scheme for" if !colors.kind_of? Hash
  invalid_levels = colors.keys.reject {|level| valid_levels.include? level}
  if !invalid_levels.empty?
    raise ArgumentError, "Color schemes cannot be defined for these invalid log levels: #{invalid_levels.join(', ')}"
  end
end

.validate_level(level, depth = 0) ⇒ Object

Raises ArgumentError if level argument is an invalid level. Depth specifies how many trace entries to remove.



38
39
40
41
42
43
# File 'lib/log4r-color/base.rb', line 38

def self.validate_level(level, depth=0)
  unless valid_level?(level)
    raise ArgumentError, "Log level must be in 0..#{LEVELS}",
          caller[1..-(depth + 1)]
  end
end