Sha256: e418a1f26e0dc46936bad9100f2957ea8348be1e3082c73f03623c378c036d13

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

module Specs
  class << self
    def log
      # Setup ENV variable handling with sane defaults
      @log ||= Nenv("celluloid_specs_log") do |env|
        env.create_method(:file) { |f| f || "log/default.log" }
        env.create_method(:sync?) { |s| s || !Nenv.ci? }

        env.create_method(:strategy) do |strategy|
          strategy || default_strategy
        end

        env.create_method(:level) { |level| default_level_for(env, level) }
      end
    end

    def logger
      @logger ||= default_logger.tap { |logger| logger.level = log.level }
    end

    attr_writer :logger

    private

    def default_logger
      case log.strategy
      when "stderr"
        Logger.new(STDERR)
      when "single"
        Logger.new(open_logfile(log.file, log.sync?))
      else
        fail "Unknown logger strategy: #{strategy.inspect}."\
          " Expected 'single' or 'stderr'."
      end
    end

    def open_logfile(rel_path, sync)
      root = Pathname(__FILE__).dirname.dirname.dirname
      log_path = root + rel_path
      logfile = File.open(log_path.to_s, "a")
      logfile.sync if sync
      logfile
    end

    def default_strategy
      (Nenv.ci? ? "stderr" : "single")
    end

    def default_level_for(env, level)
      Integer(level)
    rescue
      env.strategy == "stderr" ? Logger::WARN : Logger::DEBUG
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
celluloid-0.17.4 spec/support/logging.rb
celluloid-0.17.3 spec/support/logging.rb
celluloid-0.17.2 spec/support/logging.rb