Class: Sapience::Configuration

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

Overview

rubocop:disable ClassVars

Constant Summary collapse

SUPPORTED_EXECUTORS =
%i(single_thread_executor immediate_executor).freeze
DEFAULT =
{
  log_level:   :info,
  application: "Sapience",
  host:        nil,
  ap_options:  { multiline: false },
  appenders:   [{ stream: { io: STDOUT, formatter: :color } }],
  log_executor: :single_thread_executor,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

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



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sapience/configuration.rb', line 24

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_keyz!)
  @options[:log_executor] &&= @options[:log_executor].to_sym
  validate_log_executor!(@options[:log_executor])
  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]
  self.log_executor    = @options[:log_executor]
end

Instance Attribute Details

#ap_optionsObject

Returns the value of attribute ap_options.



11
12
13
# File 'lib/sapience/configuration.rb', line 11

def ap_options
  @ap_options
end

#appendersObject

Returns the value of attribute appenders.



11
12
13
# File 'lib/sapience/configuration.rb', line 11

def appenders
  @appenders
end

#applicationObject

Returns the value of attribute application.



11
12
13
# File 'lib/sapience/configuration.rb', line 11

def application
  @application
end

#backtrace_levelObject

Returns the value of attribute backtrace_level.



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

def backtrace_level
  @backtrace_level
end

#backtrace_level_indexObject (readonly)

Returns the value of attribute backtrace_level_index.



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

def backtrace_level_index
  @backtrace_level_index
end

#default_levelObject

Returns the value of attribute default_level.



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

def default_level
  @default_level
end

#hostObject

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



109
110
111
# File 'lib/sapience/configuration.rb', line 109

def host
  @host ||= Socket.gethostname
end

#log_executorObject

Returns the value of attribute log_executor.



11
12
13
# File 'lib/sapience/configuration.rb', line 11

def log_executor
  @log_executor
end

Instance Method Details

#default_level_indexObject



86
87
88
# File 'lib/sapience/configuration.rb', line 86

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

#fail_with_unkown_log_level!(level) ⇒ Object



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

def fail_with_unkown_log_level!(level)
  fail UnkownLogLevel,
    "Invalid level:#{level.inspect} being requested." \
    " Must be one of #{LEVELS.inspect}"
end

#index_to_level(level_index) ⇒ Object

Returns the symbolic level for the supplied level index



46
47
48
# File 'lib/sapience/configuration.rb', line 46

def index_to_level(level_index)
  LEVELS[level_index]
end

#level_by_index_or_error(constant) ⇒ Object



82
83
84
# File 'lib/sapience/configuration.rb', line 82

def level_by_index_or_error(constant)
  LEVELS.find_index(constant.downcase.to_sym) || LEVELS.find_index(:error)
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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sapience/configuration.rb', line 52

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

  case level
  when Symbol
    LEVELS.index(level)
  when String
    LEVELS.index(level.downcase.to_sym)
  when Integer
    map_levels[level] || fail_with_unkown_log_level!(level)
  else
    fail_with_unkown_log_level!(level)
  end
end

#map_levelsObject

Mapping of Rails and Ruby Logger levels to Sapience levels



74
75
76
77
78
79
80
# File 'lib/sapience/configuration.rb', line 74

def map_levels
  return [] unless defined?(::Logger::Severity)
  @@map_levels ||=
    ::Logger::Severity.constants.each_with_object([]) do |constant, levels|
      levels[::Logger::Severity.const_get(constant)] = level_by_index_or_error(constant)
    end
end

#validate_log_executor!(log_executor) ⇒ Object



113
114
115
116
# File 'lib/sapience/configuration.rb', line 113

def validate_log_executor!(log_executor)
  return true if SUPPORTED_EXECUTORS.include?(log_executor)
  fail InvalidLogExecutor, "#{log_executor} is unsupported. Use (#{SUPPORTED_EXECUTORS.join(", ")})"
end