class Loga::Configuration

Constants

FRAMEWORK_EXCEPTIONS

Attributes

device[RW]
filter_exceptions[RW]
filter_parameters[RW]
format[R]
hide_pii[RW]
host[RW]
level[RW]
logger[R]
service_name[R]
service_version[RW]
sync[RW]
tags[RW]

Public Class Methods

new(user_options = {}, framework_options = {}) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/loga/configuration.rb, line 22
def initialize(user_options = {}, framework_options = {})
  options = default_options.merge(framework_options)
                           .merge(environment_options)
                           .merge(user_options)

  self.device            = options[:device]
  self.filter_exceptions = options[:filter_exceptions]
  self.filter_parameters = options[:filter_parameters]
  self.format            = options[:format]
  self.host              = options[:host]
  self.level             = options[:level]
  self.service_name      = options[:service_name]
  self.service_version   = options[:service_version] || ServiceVersionStrategies.call
  self.sync              = options[:sync]
  self.tags              = options[:tags]
  self.hide_pii          = options[:hide_pii]

  validate

  @logger = initialize_logger
end

Public Instance Methods

format=(name) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/loga/configuration.rb, line 45
def format=(name)
  @format = name.to_s.to_sym
end
service_name=(name) click to toggle source
# File lib/loga/configuration.rb, line 49
def service_name=(name)
  @service_name = name.to_s.strip
end
structured?() click to toggle source
# File lib/loga/configuration.rb, line 53
def structured?
  format == :gelf
end

Private Instance Methods

assign_formatter() click to toggle source
# File lib/loga/configuration.rb, line 101
def assign_formatter
  if format == :gelf
    Formatters::GELFFormatter.new(
      service_name:    service_name,
      service_version: service_version,
      host:            host,
    )
  else
    Formatters::SimpleFormatter.new
  end
end
constantized_log_level() click to toggle source
# File lib/loga/configuration.rb, line 91
def constantized_log_level
  Logger.const_get(level.to_s.upcase)
end
default_options() click to toggle source
# File lib/loga/configuration.rb, line 64
def default_options
  {
    device:            STDOUT,
    filter_exceptions: FRAMEWORK_EXCEPTIONS,
    filter_parameters: [],
    format:            :simple,
    host:              hostname,
    level:             :info,
    sync:              true,
    tags:              [],
    hide_pii:          true,
  }
end
environment_options() click to toggle source
# File lib/loga/configuration.rb, line 78
def environment_options
  { format: ENV['LOGA_FORMAT'].presence }.reject { |_, v| v.nil? }
end
hostname() click to toggle source
# File lib/loga/configuration.rb, line 95
def hostname
  Socket.gethostname
rescue SystemCallError
  'unknown.host'
end
initialize_logger() click to toggle source

Note: sidekiq 6 will extend the logger -> github.com/mperham/sidekiq/blob/v6.1.2/lib/sidekiq.rb#L210

# File lib/loga/configuration.rb, line 83
def initialize_logger
  device.sync      = sync
  logger           = Logger.new(device)
  logger.formatter = assign_formatter
  logger.level     = constantized_log_level
  TaggedLogging.new(logger)
end
validate() click to toggle source
# File lib/loga/configuration.rb, line 59
def validate
  raise ConfigurationError, 'Service name cannot be blank' if service_name.blank?
  raise ConfigurationError, 'Device cannot be blank' if device.blank?
end