Module: DTR::LoggerExt

Included in:
DTR
Defined in:
lib/dtr/shared/utils/logger.rb

Defined Under Namespace

Modules: OutputErrorIntoConsole

Constant Summary collapse

DATETIME_FORMAT =
"%m-%d %H:%M:%S"
LOGGER_LEVEL =
{:info => Logger::INFO, :error => Logger::ERROR, :debug => Logger::DEBUG}

Instance Method Summary collapse

Instance Method Details

#create_default_logger(file) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/dtr/shared/utils/logger.rb', line 44

def create_default_logger(file)
  dir = 'log'
  FileUtils.mkdir_p(dir)
  log_file = File.join(dir,  file || 'dtr.log')
  do_println "DTR logfile at #{Dir.pwd}/#{log_file}"
  logger = Logger.new(log_file, 1, 5*1024*1024)
  logger.datetime_format = DATETIME_FORMAT
  logger.level = (ENV['DTR_LOG_LEVEL'] || Logger::INFO).to_i
  logger
end

#debug(message = nil, &block) ⇒ Object



65
66
67
# File 'lib/dtr/shared/utils/logger.rb', line 65

def debug(message=nil, &block)
  output(:debug, message, &block)
end

#do_print(str) ⇒ Object



55
56
57
58
59
# File 'lib/dtr/shared/utils/logger.rb', line 55

def do_print(str)
  unless ENV['DTR_ENV'] == 'test'
    print str
  end
end

#do_println(str) ⇒ Object



61
62
63
# File 'lib/dtr/shared/utils/logger.rb', line 61

def do_println(str)
  do_print("#{str}\n")
end

#error(message = nil, &block) ⇒ Object



73
74
75
# File 'lib/dtr/shared/utils/logger.rb', line 73

def error(message=nil, &block)
  output(:error, message, &block)
end

#info(message = nil, &block) ⇒ Object



69
70
71
# File 'lib/dtr/shared/utils/logger.rb', line 69

def info(message=nil, &block)
  output(:info, message, &block)
end

#logger(file = nil) ⇒ Object



36
37
38
# File 'lib/dtr/shared/utils/logger.rb', line 36

def logger(file=nil)
  @logger ||= create_default_logger(file)
end

#logger=(logger) ⇒ Object



40
41
42
# File 'lib/dtr/shared/utils/logger.rb', line 40

def logger=(logger)
  @logger = logger
end

#output(level, msg = nil, &block) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/dtr/shared/utils/logger.rb', line 77

def output(level, msg=nil, &block)
  return if LOGGER_LEVEL[level] < logger.level

  msg = block.call if block_given?
  # puts "log: #{msg}"
  logger.send(level, msg)
end