lib/loga/configuration.rb in loga-2.0.0 vs lib/loga/configuration.rb in loga-2.1.0
- old
+ new
@@ -1,50 +1,44 @@
require 'active_support/core_ext/object/blank'
require 'active_support/version'
+require 'loga/formatters/gelf_formatter'
+require 'loga/formatters/simple_formatter'
require 'loga/service_version_strategies'
require 'logger'
require 'socket'
module Loga
class Configuration
- DEFAULT_KEYS = %i(
- device
- filter_exceptions
- filter_parameters
- format
- host
- level
- service_name
- service_version
- sync
- tags
- ).freeze
-
FRAMEWORK_EXCEPTIONS = %w(
ActionController::RoutingError
ActiveRecord::RecordNotFound
Sinatra::NotFound
).freeze
- attr_accessor(*DEFAULT_KEYS)
- attr_reader :logger, :service_version
- private_constant :DEFAULT_KEYS
+ attr_accessor :device, :filter_exceptions, :filter_parameters, :format,
+ :host, :level, :service_name, :service_version, :sync, :tags
+ attr_reader :logger
def initialize(user_options = {}, framework_options = {})
options = default_options.merge(framework_options)
.merge(environment_options)
.merge(user_options)
- DEFAULT_KEYS.each do |attribute|
- public_send("#{attribute}=", options[attribute])
- end
+ 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]
- raise ConfigurationError, 'Service name cannot be blank' if service_name.blank?
- raise ConfigurationError, 'Device cannot be blank' if device.blank?
+ validate
- @service_version = initialize_service_version
- @logger = initialize_logger
+ @logger = initialize_logger
end
def format=(name)
@format = name.to_s.to_sym
end
@@ -57,10 +51,15 @@
format == :gelf
end
private
+ def validate
+ raise ConfigurationError, 'Service name cannot be blank' if service_name.blank?
+ raise ConfigurationError, 'Device cannot be blank' if device.blank?
+ end
+
def default_options
{
device: STDOUT,
filter_exceptions: FRAMEWORK_EXCEPTIONS,
filter_parameters: [],
@@ -74,14 +73,10 @@
def environment_options
{ format: ENV['LOGA_FORMAT'].presence }.reject { |_, v| v.nil? }
end
- def initialize_service_version
- service_version || ServiceVersionStrategies.call
- end
-
def initialize_logger
device.sync = sync
logger = Logger.new(device)
logger.formatter = assign_formatter
logger.level = constantized_log_level
@@ -98,30 +93,16 @@
'unknown.host'
end
def assign_formatter
if format == :gelf
- Formatter.new(
+ Formatters::GELFFormatter.new(
service_name: service_name,
service_version: service_version,
host: host,
)
else
- active_support_simple_formatter
- end
- end
-
- def active_support_simple_formatter
- case ActiveSupport::VERSION::MAJOR
- when 3
- require 'active_support/core_ext/logger'
- Logger::SimpleFormatter.new
- when 4..5
- require 'active_support/logger'
- ActiveSupport::Logger::SimpleFormatter.new
- else
- raise Loga::ConfigurationError,
- "ActiveSupport #{ActiveSupport::VERSION::MAJOR} is unsupported"
+ Formatters::SimpleFormatter.new
end
end
end
end