class Rookout::Logger

Constants

LOG_LEVELS

Public Class Methods

new() click to toggle source
# File lib/rookout/logger.rb, line 13
def initialize
  # Detect unit tests
  if (Config.debug ||
      $PROGRAM_NAME.end_with?("minitest_runner.rb") ||
      $PROGRAM_NAME.end_with?("tunit_or_minitest_in_folder_runner.rb")) &&
     Dir.pwd.end_with?("ruby-sdk")
    Config.logger_log_level = :DEBUG
    Config.logger_log_to_stderr = true
  end

  @verbosity = LOG_LEVELS.index(Config.logger_log_level) || LOG_LEVELS.index(:INFO)

  @output = nil
  @handlers = []
  build_handlers
end

Public Instance Methods

debug(message, *args) click to toggle source
# File lib/rookout/logger.rb, line 38
def debug message, *args
  log :DEBUG, message, args
end
error(message, *args) click to toggle source
# File lib/rookout/logger.rb, line 50
def error message, *args
  log :ERROR, message, args
end
exception(message, exc) click to toggle source
# File lib/rookout/logger.rb, line 54
def exception message, exc
  error message, exc
end
info(message, *args) click to toggle source
# File lib/rookout/logger.rb, line 42
def info message, *args
  log :INFO, message, args
end
register_output(output) click to toggle source
# File lib/rookout/logger.rb, line 30
def register_output output
  @output = output
end
remove_output(_output) click to toggle source
# File lib/rookout/logger.rb, line 34
def remove_output _output
  @output = nil
end
warning(message, *args) click to toggle source
# File lib/rookout/logger.rb, line 46
def warning message, *args
  log :WARNING, message, args
end

Private Instance Methods

absolute_path?(path) click to toggle source
# File lib/rookout/logger.rb, line 172
def absolute_path? path
  path == File.absolute_path(path)
end
build_handlers() click to toggle source
# File lib/rookout/logger.rb, line 115
def build_handlers
  if Config.logger_log_to_stderr
    @handlers.push new_file_handler unless Config.logger_filename.nil? || Config.logger_filename.empty?
    @handlers.push new_stderr_handler
  end

  @handlers.push new_remote_handler
end
calculate_log_file_path() click to toggle source
# File lib/rookout/logger.rb, line 143
def calculate_log_file_path
  return Config.logger_filename if absolute_path? Config.logger_filename

  if RUBY_PLATFORM.include? "darwin"
    File.join ENV["HOME"], Config.logger_filename
  elsif RUBY_PLATFORM.match?(/cygwin|mswin|mingw|bccwin|wince|emx/)
    File.join ENV["USERPROFILE"], Config.logger_filename
  else
    File.join "/var/log", Config.logger_filename
  end
end
log(level, message, args) click to toggle source
# File lib/rookout/logger.rb, line 102
def log level, message, args
  level_no = LOG_LEVELS.index level
  if level_no.nil?
    level = :ERROR
    level_no = LOG_LEVELS.index level
  end

  return if level_no < @verbosity

  record = LogRecord.new level, message, args
  @handlers.each { |handler| handler.call record }
end
new_file_handler() click to toggle source
# File lib/rookout/logger.rb, line 124
def new_file_handler
  log_file_path = calculate_log_file_path
  directory = File.dirname log_file_path

  begin
    FileUtils.mkdir_p directory
    file = File.open log_file_path, "a"
  rescue SystemCallError => e
    file = nil

    if Config.debug
      STDERR.puts "[Rookout] Failed to open log file: #{log_file_path}"
      STDERR.puts e.backtrace
    end
  end

  ->(record) { file.write record.format + "\n" if file }
end
new_remote_handler() click to toggle source
# File lib/rookout/logger.rb, line 159
def new_remote_handler
  lambda do |record|
    return unless @output
    @output.send_log_message record.level,
                             record.time,
                             record.filename,
                             record.lineno,
                             record.message,
                             record.formatted_message,
                             record.arguments
  end
end
new_stderr_handler() click to toggle source
# File lib/rookout/logger.rb, line 155
def new_stderr_handler
  ->(record) { STDERR.puts record.format }
end