Sha256: 4a9a24b917ded121f27433606be6a328de41d0f1c24462877e33a8c7069a03f0

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

class ReeLogger::Config
  include Ree::BeanDSL

  bean :config do
    singleton
    factory :build

    link :to_obj, from: :ree_object
    link :is_blank, from: :ree_object
    link :validate_inclusion, from: :ree_validator
  end

  LEVELS = %w[warn info debug error fatal unknown].freeze
  RATE_LIMIT_INTERVAL = 60
  RATE_LIMIT_MAX_COUNT = 600

  def build
    to_obj({
      file_path: ENV['LOG_FILE_PATH'],
      file_auto_flush: parse_bool_string(ENV['LOG_FILE_AUTO_FLUSH']),
      levels: {
        file: parse_level(ENV['LOG_LEVEL_FILE']),
        stdout: parse_level(ENV['LOG_LEVEL_STDOUT']),
      },
      rate_limit: {
        interval: get_int_value('LOG_RATE_LIMIT_INTERVAL', RATE_LIMIT_INTERVAL),
        max_count: get_int_value('LOG_RATE_LIMIT_MAX_COUNT', RATE_LIMIT_MAX_COUNT),
      },
      default_filter_words: %w[
        password token credential bearer authorization
      ]
    })
  end

  private

  def get_int_value(name, default)
    value = ENV[name]

    v = if value.to_s.strip.empty?
      default
    else
      Integer(value)
    end

    if v < 0
      raise ArgumentError, "ENV['#{name}'] should be > 0"
    end

    v
  end

  def parse_bool_string(bool)
    return false if is_blank(bool)
    return bool.to_s.downcase == "true"
  end

  def parse_level(level)
    return if is_blank(level)
    validate_inclusion(level, LEVELS)
    level.to_sym
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ree_lib-1.0.4 lib/ree_lib/packages/ree_logger/package/ree_logger/config.rb
ree_lib-1.0.3 lib/ree_lib/packages/ree_logger/package/ree_logger/config.rb
ree_lib-1.0.2 lib/ree_lib/packages/ree_logger/package/ree_logger/config.rb
ree_lib-1.0.1 lib/ree_lib/packages/ree_logger/package/ree_logger/config.rb
ree_lib-1.0.0 lib/ree_lib/packages/ree_logger/package/ree_logger/config.rb