lib/phobos.rb in phobos-2.1.0 vs lib/phobos.rb in phobos-2.1.1
- old
+ new
@@ -11,11 +11,11 @@
require 'exponential_backoff'
require 'kafka'
require 'logging'
require 'erb'
-require 'phobos/deep_struct'
+require 'phobos/configuration'
require 'phobos/version'
require 'phobos/constants'
require 'phobos/log'
require 'phobos/instrumentation'
require 'phobos/errors'
@@ -47,26 +47,15 @@
def pop_tags(*args); end
end
end
module Phobos
+ extend Configuration
class << self
attr_reader :config, :logger
attr_accessor :silence_log
- def configure(configuration)
- @config = fetch_configuration(configuration)
- @config.class.send(:define_method, :producer_hash) do
- Phobos.config.producer&.to_hash&.except(:kafka)
- end
- @config.class.send(:define_method, :consumer_hash) do
- Phobos.config.consumer&.to_hash&.except(:kafka)
- end
- @config.listeners ||= []
- configure_logger
- end
-
def add_listeners(configuration)
listeners_config = fetch_configuration(configuration)
@config.listeners += listeners_config.listeners
end
@@ -90,86 +79,7 @@
def deprecate(message)
location = caller.find { |line| line !~ %r{/phobos/} }
warn "DEPRECATION WARNING: #{message}: #{location}"
end
- # :nodoc:
- def configure_logger
- Logging.backtrace(true)
- Logging.logger.root.level = silence_log ? :fatal : config.logger.level
-
- configure_ruby_kafka_logger
- configure_phobos_logger
-
- logger.info do
- Hash(message: 'Phobos configured', env: ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'N/A')
- end
- end
-
- private
-
- def fetch_configuration(configuration)
- DeepStruct.new(read_configuration(configuration))
- end
-
- def read_configuration(configuration)
- return configuration.to_h if configuration.respond_to?(:to_h)
-
- YAML.safe_load(
- ERB.new(
- File.read(File.expand_path(configuration))
- ).result,
- [Symbol],
- [],
- true
- )
- end
-
- def configure_phobos_logger
- if config.custom_logger
- @logger = config.custom_logger
- else
- @logger = Logging.logger[self]
- @logger.appenders = logger_appenders
- end
- end
-
- def configure_ruby_kafka_logger
- if config.custom_kafka_logger
- @ruby_kafka_logger = config.custom_kafka_logger
- elsif config.logger.ruby_kafka
- @ruby_kafka_logger = Logging.logger['RubyKafka']
- @ruby_kafka_logger.appenders = logger_appenders
- @ruby_kafka_logger.level = silence_log ? :fatal : config.logger.ruby_kafka.level
- else
- @ruby_kafka_logger = nil
- end
- end
-
- def logger_appenders
- appenders = [Logging.appenders.stdout(layout: stdout_layout)]
-
- if log_file
- FileUtils.mkdir_p(File.dirname(log_file))
- appenders << Logging.appenders.file(log_file, layout: json_layout)
- end
-
- appenders
- end
-
- def log_file
- config.logger.file
- end
-
- def json_layout
- Logging.layouts.json(date_pattern: Constants::LOG_DATE_PATTERN)
- end
-
- def stdout_layout
- if config.logger.stdout_json == true
- json_layout
- else
- Logging.layouts.pattern(date_pattern: Constants::LOG_DATE_PATTERN)
- end
- end
end
end