lib/semantic_logger/subscriber.rb in semantic_logger-4.0.0 vs lib/semantic_logger/subscriber.rb in semantic_logger-4.1.0

- old
+ new

@@ -5,11 +5,11 @@ class Subscriber < SemanticLogger::Base # Every logger has its own formatter attr_accessor :formatter attr_writer :application, :host - # Returns the current log level if set, otherwise it logs everything it receives + # Returns the current log level if set, otherwise it logs everything it receives. def level @level || :trace end # A subscriber should implement flush if it can. @@ -20,21 +20,21 @@ # A subscriber should implement close if it can. def close # NOOP end - # Returns [SemanticLogger::Formatters::Default] formatter default for this subscriber + # Returns [SemanticLogger::Formatters::Default] default formatter for this subscriber. def default_formatter SemanticLogger::Formatters::Default.new end - # Allow application name to be set globally or per subscriber + # Allow application name to be set globally or on a per subscriber basis. def application @application || SemanticLogger.application end - # Allow host name to be set globally or per subscriber + # Allow host name to be set globally or on a per subscriber basis. def host @host || SemanticLogger.host end private @@ -55,41 +55,36 @@ # RegExp: Only include log messages where the class name matches the supplied. # regular expression. All other messages will be ignored. # Proc: Only include log messages where the supplied Proc returns true # The Proc must return true or false. # - # host: [String] - # Name of this host to appear in log messages. - # Default: SemanticLogger.host - # # application: [String] # Name of this application to appear in log messages. # Default: SemanticLogger.application - def initialize(options={}, &block) - # Backward compatibility - options = {level: options} unless options.is_a?(Hash) - options = options.dup - level = options.delete(:level) - filter = options.delete(:filter) - @formatter = extract_formatter(options.delete(:formatter), &block) - @application = options.delete(:application) - @host = options.delete(:host) - raise(ArgumentError, "Unknown options: #{options.inspect}") if options.size > 0 + # + # host: [String] + # Name of this host to appear in log messages. + # Default: SemanticLogger.host + def initialize(level: nil, formatter: nil, filter: nil, application: nil, host: nil, &block) + @formatter = extract_formatter(formatter, &block) + @application = application + @host = host - # Subscribers don't take a class name, so use this class name if an subscriber - # is logged to directly + # Subscribers don't take a class name, so use this class name if a subscriber + # is logged to directly. super(self.class, level, filter) end - # Return the level index for fast comparisons + # Return the level index for fast comparisons. # Returns the lowest level index if the level has not been explicitly - # set for this instance + # set for this instance. def level_index @level_index || 0 end - # Return formatter that responds to call + # Return formatter that responds to call. + # # Supports formatter supplied as: # - Symbol # - Hash ( Symbol => { options }) # - Instance of any of SemanticLogger::Formatters # - Proc @@ -110,18 +105,9 @@ when respond_to?(:call) self else default_formatter end - end - - SUBSCRIBER_OPTIONS = [:level, :formatter, :filter, :application, :host].freeze - - # Returns [Hash] the subscriber common options from the supplied Hash - def extract_subscriber_options!(options) - subscriber_options = {} - SUBSCRIBER_OPTIONS.each { |key| subscriber_options[key] = options.delete(key) if options.has_key?(key) } - subscriber_options end end end