diff --git a/lib/bunnyrun/application.rb b/lib/bunnyrun/application.rb index 800551b..40c324b 100644 --- a/lib/bunnyrun/application.rb +++ b/lib/bunnyrun/application.rb @@ -103,28 +103,35 @@ module BunnyRun parser.opt :url, 'Connection string ' \ '(example: "amqp://guest:guest@127.0.0.1:5672/vhost")', short: 'U', type: :string, - default: ENV['RABBITMQ_URL'] || nil + default: ENV.fetch('RABBITMQ_URL', nil) + parser.opt :host, 'Host', short: 'H', type: :string, - default: ENV['RABBITMQ_HOST'] || '127.0.0.1' + default: ENV.fetch('RABBITMQ_HOST', '127.0.0.1') + parser.opt :port, 'Port', short: 'P', type: :int, - default: (ENV['RABBITMQ_PORT'] || 5672).to_i + default: ENV.fetch('RABBITMQ_PORT', '5672').to_i + parser.opt :ssl, 'Connect using SSL', short: 's', type: :bool, default: trufy?(ENV.fetch('RABBITMQ_SSL', false)) + parser.opt :vhost, 'Virtual host', short: 'V', type: :string, - default: ENV['RABBITMQ_VHOST'] || '/' + default: ENV.fetch('RABBITMQ_VHOST', '/') + parser.opt :user, 'Username', short: 'u', type: :string, - default: ENV['RABBITMQ_USER'] || 'guest' + default: ENV.fetch('RABBITMQ_USER', 'guest') + parser.opt :pass, 'Password', short: 'p', type: :string, - default: ENV['RABBITMQ_PASS'] || 'guest' + default: ENV.fetch('RABBITMQ_PASS', 'guest') + parser.opt :prefetch, 'Default prefetch count', short: :none, type: :int, - default: (ENV['RABBITMQ_PREFETCH'] || 1).to_i + default: ENV.fetch('RABBITMQ_PREFETCH', 1).to_i parser.conflicts :url, :host parser.conflicts :url, :port @@ -139,16 +146,19 @@ module BunnyRun parser.opt :log_target, 'Log target, file path or STDOUT', short: 't', type: :string, - default: ENV['LOG_TARGET'] || 'STDOUT' + default: ENV.fetch('LOG_TARGET', 'STDOUT') + parser.opt :log_level, 'Log level (debug, info, warn, error, fatal)', short: 'l', type: :string, - default: ENV['LOG_LEVEL'] || 'info' + default: ENV.fetch('LOG_LEVEL', 'info') + parser.opt :bunny_log_target, 'Log target used by Bunny', short: :none, type: :string, - default: ENV['BUNNY_LOG_TARGET'] || 'STDOUT' + default: ENV.fetch('BUNNY_LOG_TARGET', 'STDOUT') + parser.opt :bunny_log_level, 'Log level used by Bunny', short: :none, type: :string, - default: ENV['BUNNY_LOG_LEVEL'] || 'warn' + default: ENV.fetch('BUNNY_LOG_LEVEL', 'warn') end def define_application_options(parser) diff --git a/lib/bunnyrun/consumer.rb b/lib/bunnyrun/consumer.rb index 744a65c..9a4562d 100644 --- a/lib/bunnyrun/consumer.rb +++ b/lib/bunnyrun/consumer.rb @@ -5,6 +5,7 @@ module BunnyRun attr_reader :connection attr_reader :publish_channel attr_reader :default_prefetch + attr_reader :options attr_reader :logger class << self @@ -52,6 +53,7 @@ module BunnyRun @connection = opts[:connection] @publish_channel = opts[:publish_channel] @default_prefetch = opts[:default_prefetch] + @options = opts[:options] @logger = opts[:logger] end diff --git a/lib/bunnyrun/runner.rb b/lib/bunnyrun/runner.rb index 14836f8..510a901 100644 --- a/lib/bunnyrun/runner.rb +++ b/lib/bunnyrun/runner.rb @@ -34,11 +34,14 @@ module BunnyRun end def logger - @logger ||= begin - logger = Logger.new(log_target) - logger.level = log_level - logger - end + @logger ||= create_logger(options.log_target, options.log_level) + end + + def bunny_logger + @bunny_logger ||= create_logger( + options.bunny_log_target, + options.bunny_log_level + ) end private @@ -52,34 +55,39 @@ module BunnyRun connection: connection, publish_channel: publish_channel, default_prefetch: options.prefetch, + options: options, logger: logger ) + consumer.start end def connection_opts - return options[:url] if options[:url] + return options.url if options.url { - host: options[:host], - port: options[:port], - ssl: options[:ssl], - vhost: options[:vhost], - user: options[:user], - pass: options[:pass] + host: options.host, + port: options.port, + ssl: options.ssl, + vhost: options.vhost, + user: options.user, + pass: options.pass, + logger: bunny_logger } end - def log_target - if options[:log_target].casecmp('stdout').zero? - STDOUT - else - options[:log_target] + def create_logger(target, level) + Logger.new(normalize_log_target(target)).tap do |l| + l.level = Kernel.const_get("::Logger::#{level.upcase}") end end - def log_level - Kernel.const_get("::Logger::#{options[:log_level].upcase}") + def normalize_log_target(input) + if input.casecmp('stdout').zero? + STDOUT + else + input + end end end end