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,
  host:              nil,
  ap_options:        { multiline: false },
  appenders:         [{ stream: { io: STDOUT, formatter: :color } }],
  log_executor:      :single_thread_executor,
  metrics:           { datadog: { url: Sapience::DEFAULT_STATSD_URL } },
  error_handler:     { silent: {} },
  filter_parameters: %w(password password_confirmation),
}.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
37
38
39
# File 'lib/sapience/configuration.rb', line 24

def initialize(options = {}) # rubocop:disable AbcSize
  fail ArgumentError, "options need to be a hash #{options.inspect}" unless options.is_a?(Hash)
  @options             = DEFAULT.merge(options.dup.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.host              = @options[:host]
  self.app_name          = @options[:app_name]
  self.ap_options        = @options[:ap_options]
  self.appenders         = @options[:appenders]
  self.log_executor      = @options[:log_executor]
  self.filter_parameters = @options[:filter_parameters]
  self.metrics           = @options[:metrics]
  self.error_handler     = @options[:error_handler]
end

Instance Attribute Details

#ap_optionsObject

Returns the value of attribute ap_options.



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

def ap_options
  @ap_options
end

#app_nameObject

Returns the value of attribute app_name.



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

def app_name
  @app_name
end

#appendersObject

Returns the value of attribute appenders.



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

def appenders
  @appenders
end

#backtrace_levelObject

Returns the value of attribute backtrace_level.



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

def backtrace_level
  @backtrace_level
end

#backtrace_level_indexObject (readonly)

Returns the value of attribute backtrace_level_index.



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

def backtrace_level_index
  @backtrace_level_index
end

#default_levelObject

Returns the value of attribute default_level.



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

def default_level
  @default_level
end

#error_handlerObject

Returns the value of attribute error_handler.



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

def error_handler
  @error_handler
end

#filter_parametersObject

Returns the value of attribute filter_parameters.



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

def filter_parameters
  @filter_parameters
end

#hostObject

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



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

def host
  @host ||= Socket.gethostname
end

#log_executorObject

Returns the value of attribute log_executor.



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

def log_executor
  @log_executor
end

#metricsObject

Returns the value of attribute metrics.



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

def metrics
  @metrics
end

Instance Method Details

#default_level_indexObject



89
90
91
# File 'lib/sapience/configuration.rb', line 89

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

#fail_with_unkown_log_level!(level) ⇒ Object



70
71
72
73
74
# File 'lib/sapience/configuration.rb', line 70

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



49
50
51
# File 'lib/sapience/configuration.rb', line 49

def index_to_level(level_index)
  LEVELS[level_index]
end

#level_by_index_or_error(constant) ⇒ Object



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

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sapience/configuration.rb', line 55

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



77
78
79
80
81
82
83
# File 'lib/sapience/configuration.rb', line 77

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



116
117
118
119
# File 'lib/sapience/configuration.rb', line 116

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