Class: Hyla::Logger2

Inherits:
Object
  • Object
show all
Defined in:
lib/hyla/logger2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode, log_yml_file, dirname = nil, logname = nil, level = nil, tracer = nil) ⇒ Logger2

Returns a new instance of Logger2.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hyla/logger2.rb', line 12

def initialize(mode, log_yml_file, dirname = nil, logname = nil, level = nil, tracer = nil)

  #
  # Change logging level within the YAML config file
  # ! Log4r::YamlConfigurator does not allow to change key/val for the level but only for formatter/outputter
  #
  if level.nil? then new_level = 'INFO' else new_level = level end
  if tracer.nil? then new_tracer = 'false' else new_tracer = tracer end
  if mode.nil? then new_mode = 'production' else new_mode = mode end

  log4r_hash = load_file(log_yml_file)

  #
  # TODO - improve it to avoid to hard code path
  #
  log4r_hash['log4r_config']['loggers'].each_with_index do |x, index|
    log4r_hash['log4r_config']['loggers'][index]['level'] = new_level
    log4r_hash['log4r_config']['loggers'][index]['tracer'] = new_tracer
  end

  cfg = Log4r::YamlConfigurator

  if dirname.nil?
    dir = [Dir.home, 'log'] * '/'
    Dir.mkdir(dir) unless Dir.exist?(dir)
    cfg['DIRNAME'] = dir
  else
    cfg['DIRNAME'] = dirname
  end

  if logname.nil? then cfg['LOGNAME'] = 'hyla.log' else cfg['LOGNAME'] = logname end

  cfg.decode_yaml log4r_hash['log4r_config']

  @log = Log4r::Logger[new_mode]
end

Instance Attribute Details

#levelsObject (readonly)

Returns the value of attribute levels.



10
11
12
# File 'lib/hyla/logger2.rb', line 10

def levels
  @levels
end

#logObject (readonly)

Returns the value of attribute log.



10
11
12
# File 'lib/hyla/logger2.rb', line 10

def log
  @log
end

Instance Method Details

#debug(msg) ⇒ Object



102
103
104
# File 'lib/hyla/logger2.rb', line 102

def debug(msg)
  @log.debug msg
end

#error(msg) ⇒ Object



114
115
116
# File 'lib/hyla/logger2.rb', line 114

def error(msg)
  @log.error msg
end

#fatal(msg) ⇒ Object



118
119
120
# File 'lib/hyla/logger2.rb', line 118

def fatal(msg)
  @log.fatal msg
end

#formatted_topic(topic) ⇒ Object

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc.

Returns the formatted topic statement



135
136
137
# File 'lib/hyla/logger2.rb', line 135

def formatted_topic(topic)
  "#{topic} ".rjust(20)
end

#info(msg) ⇒ Object



106
107
108
# File 'lib/hyla/logger2.rb', line 106

def info(msg)
  @log.info msg
end

#iterate(h, level) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hyla/logger2.rb', line 54

def iterate(h, level)
  h.each do |k,v|
    value = v || k
    if value.is_a?(Hash) || value.is_a?(Array)
      puts "evaluating: #{value} recursively..."
      iterate(value, level)
    else
      if k == "level"
        v = level
      end
    end
  end
end

#load_file(cfg_file) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hyla/logger2.rb', line 78

def load_file(cfg_file)
  SafeYAML::OPTIONS[:default_mode] = :safe

  if cfg_file.nil?
    f = [Configuration.configs,'log4r.yaml' ] * '/'
  else
    f = cfg_file
  end

  #
  # Find/Replace the logging level
  #
  #content = File.read(f)
  #new_contents = content.gsub(/LOGGING_LEVEL/, level)

  # To merely print the contents of the file, use:
  # puts new_contents

  # To write changes to the file, use:
  #File.open(f, "w") {|file| file.puts new_contents }

  YAML.load_file(f)
end

#message(topic, message) ⇒ Object

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns the formatted message



127
128
129
# File 'lib/hyla/logger2.rb', line 127

def message(topic, message)
  formatted_topic(topic) + message.to_s.gsub(/\s+/, ' ')
end

#nested_hash_value(obj, key) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/hyla/logger2.rb', line 68

def nested_hash_value(obj,key)
  if obj.respond_to?(:key?) && obj.key?(key)
    obj[key]
  elsif obj.respond_to?(:each)
    r = nil
    obj.find{ |*a| r=nested_hash_value(a.last,key) }
    r
  end
end

#warn(msg) ⇒ Object



110
111
112
# File 'lib/hyla/logger2.rb', line 110

def warn(msg)
  @log.warn msg
end