lib/zabbix-ruby-client/logger.rb in zabbix-ruby-client-0.0.15 vs lib/zabbix-ruby-client/logger.rb in zabbix-ruby-client-0.0.16

- old
+ new

@@ -1,20 +1,46 @@ require "logger" -class ZabbixRubyClient - module Logger +module ZabbixRubyClient + module Log extend self - def get_logger(dir, level) - logger = ::Logger.new(File.join(dir,"zrc.log")) - logger.level = get_level_constant(level) - logger.formatter = proc do |severity, datetime, progname, msg| + def set_logger(logfile, level="info") + if logfile.is_a?(String) && !Dir.exists?(File.dirname(logfile)) + FileUtils.mkdir_p(File.dirname(logfile)) + end + @logger = ::Logger.new(logfile) + @logger.level = get_level_constant(level) + @logger.formatter = proc do |severity, datetime, progname, msg| "[#{datetime.utc}] #{severity}: #{msg}\n" end - logger end + def logger + @logger ||= ::Logger.new(STDOUT) + end + + def debug(msg) + logger.debug(msg) + end + + def info(msg) + logger.info(msg) + end + + def warn(msg) + logger.warn(msg) + end + + def error(msg) + logger.error(msg) + end + + def fatal(msg) + logger.fatal(msg) + end + def get_level_constant(level) if level begin ::Logger.const_get(level.to_s.upcase) rescue NameError @@ -24,6 +50,6 @@ ::Logger::INFO end end end -end \ No newline at end of file +end