Class: Pmux::Gateway::LoggerWrapper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pmux-gw/logger_wrapper.rb

Constant Summary collapse

@@log_level_map =
{
  'debug' => Logger::DEBUG,
  'info' => Logger::INFO,
  'warn' => Logger::WARN,
  'error' => Logger::ERROR,
  'fatal' => Logger::FATAL
}

Instance Method Summary collapse

Instance Method Details

#closeObject



50
51
52
# File 'lib/pmux-gw/logger_wrapper.rb', line 50

def close()
  @logger.close if !@logger.nil?
end

#fixup_level(level) ⇒ Object



24
25
26
27
# File 'lib/pmux-gw/logger_wrapper.rb', line 24

def fixup_level level
  return level if @@log_level_map.key?(level)
  return "info"
end

#init(foreground) ⇒ Object



17
18
19
20
21
22
# File 'lib/pmux-gw/logger_wrapper.rb', line 17

def init foreground
  @syslog_wrapper = SyslogWrapper.instance()
  @foreground = foreground
  @logger = nil
  @serverity = Logger::INFO
end

#logging(serverity, msg) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pmux-gw/logger_wrapper.rb', line 54

def logging serverity, msg
  serverity = fixup_level(serverity)
  if !@logger.nil?
    case serverity
    when "debug"
      @logger.debug(msg)
    when "info"
      @logger.info(msg) 
    when "warn"
      @logger.warn(msg) 
    when "error"
      @logger.error(msg) 
    when "fatal"
      @logger.fatal(msg) 
    end
  end
  @syslog_wrapper.logging("log", serverity, msg) if @@log_level_map[serverity] >= @serverity
  puts "[#{serverity}] #{msg}" if @foreground && @@log_level_map[serverity] >= @serverity
end

#open(log_file_path, log_level) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pmux-gw/logger_wrapper.rb', line 29

def open log_file_path, log_level
  # ログをオープンする
  # すでに開いている状態で呼ばれるとリオープンする
  @serverity = @@log_level_map[fixup_level(log_level)]
  old_logger = @logger
  @logger = nil
  begin
    @logger = Logger.new(log_file_path, 'daily')
    @logger.level = @serverity
    old_logger.close() if !old_logger.nil?
  rescue Errno::ENOENT => e
    @logger = old_logger if !old_logger.nil?
    logging("error", "not found log file (#{log_file_path})")
    logging("error", "error: #{e}")
  rescue Errno::EACCES => e
    @logger = old_logger if !old_logger.nil?
    logging("error", "can not access log file (#{log_file_path})")
    logging("error", "error: #{e}")
  end
end