Class: Sapience::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/sapience/configuration.rb,
lib/sapience/configuration/grape.rb

Overview

rubocop:disable ClassVars

Defined Under Namespace

Classes: Grape

Constant Summary collapse

DEFAULT =
{
  log_level:   :info,
  application: "Sapience",
  host:        nil,
  ap_options:  { multiline: false },
  appenders:   [{ stream: { io: STDOUT, formatter: :color } }],
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Initial default Level for all new instances of Sapience::Logger



19
20
21
22
23
24
25
26
27
28
# File 'lib/sapience/configuration.rb', line 19

def initialize(options = {}) # rubocop:disable AbcSize
  fail ArgumentError, "options need to be a hash" unless options.is_a?(Hash)
  @options             = DEFAULT.merge(options.deep_symbolize_keys!)
  self.default_level   = @options[:log_level].to_sym
  self.backtrace_level = @options[:log_level].to_sym
  self.application     = @options[:application]
  self.host            = @options[:host]
  self.ap_options      = @options[:ap_options]
  self.appenders       = @options[:appenders]
end

Instance Attribute Details

#ap_optionsObject

Returns the value of attribute ap_options.



8
9
10
# File 'lib/sapience/configuration.rb', line 8

def ap_options
  @ap_options
end

#appendersObject

Returns the value of attribute appenders.



8
9
10
# File 'lib/sapience/configuration.rb', line 8

def appenders
  @appenders
end

#applicationObject

Returns the value of attribute application.



8
9
10
# File 'lib/sapience/configuration.rb', line 8

def application
  @application
end

#backtrace_levelObject

Returns the value of attribute backtrace_level.



6
7
8
# File 'lib/sapience/configuration.rb', line 6

def backtrace_level
  @backtrace_level
end

#backtrace_level_indexObject (readonly)

Returns the value of attribute backtrace_level_index.



6
7
8
# File 'lib/sapience/configuration.rb', line 6

def backtrace_level_index
  @backtrace_level_index
end

#default_levelObject

Returns the value of attribute default_level.



6
7
8
# File 'lib/sapience/configuration.rb', line 6

def default_level
  @default_level
end

#hostObject

Returns [String] name of this host for logging purposes Note: Not all appenders use ‘host`



91
92
93
# File 'lib/sapience/configuration.rb', line 91

def host
  @host ||= Socket.gethostname
end

Instance Method Details

#default_level_indexObject



68
69
70
# File 'lib/sapience/configuration.rb', line 68

def default_level_index
  Thread.current[:sapience_silence] || @default_level_index
end

#index_to_level(level_index) ⇒ Object

Returns the symbolic level for the supplied level index



38
39
40
# File 'lib/sapience/configuration.rb', line 38

def index_to_level(level_index)
  LEVELS[level_index]
end

#level_to_index(level) ⇒ Object

Internal method to return the log level as an internal index Also supports mapping the ::Logger levels to Sapience levels



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sapience/configuration.rb', line 44

def level_to_index(level) # rubocop:disable AbcSize, PerceivedComplexity, CyclomaticComplexity
  return if level.nil?

  index =
  if level.is_a?(Symbol)
    LEVELS.index(level)
  elsif level.is_a?(String)
    level = level.downcase.to_sym
    LEVELS.index(level)
  elsif level.is_a?(Integer) && defined?(::Logger::Severity)
    # Mapping of Rails and Ruby Logger levels to Sapience levels
    @@map_levels ||= begin
      levels = []
      ::Logger::Severity.constants.each do |constant|
        levels[::Logger::Severity.const_get(constant)] = LEVELS.find_index(constant.downcase.to_sym) || LEVELS.find_index(:error) # rubocop:disable LineLength
      end
      levels
    end
    @@map_levels[level]
  end
  fail "Invalid level:#{level.inspect} being requested. Must be one of #{LEVELS.inspect}" unless index
  index
end