lib/semantic_logger/subscriber.rb in semantic_logger-4.1.1 vs lib/semantic_logger/subscriber.rb in semantic_logger-4.2.0

- old
+ new

@@ -1,13 +1,13 @@ # Abstract Subscriber # -# Abstract base class for appender and metrics subscribers. +# Abstract base class for all appenders. module SemanticLogger class Subscriber < SemanticLogger::Base - # Every logger has its own formatter - attr_accessor :formatter - attr_writer :application, :host + # Every appender has its own formatter + attr_reader :formatter + attr_writer :application, :host, :logger # Returns the current log level if set, otherwise it logs everything it receives. def level @level || :trace end @@ -35,18 +35,42 @@ # Allow host name to be set globally or on a per subscriber basis. def host @host || SemanticLogger.host end + # Give each appender its own logger for logging. + # For example trace messages sent to services or errors when something fails. + def logger + @logger ||= begin + logger = SemanticLogger::Processor.logger.clone + logger.name = self.class.name + logger + end + end + + # Set the formatter from Symbol|Hash|Block + def formatter=(formatter) + @formatter = + if formatter.nil? + respond_to?(:call) ? self : default_formatter + else + Formatters.factory(formatter) + end + end + + # Whether this log entry meets the criteria to be logged by this appender. + def should_log?(log) + super(log) && !log.metric_only? + end + private # Initializer for Abstract Class SemanticLogger::Subscriber # # Parameters # level: [:trace | :debug | :info | :warn | :error | :fatal] # Override the log level for this subscriber. - # Default: :error # # formatter: [Object|Proc] # An instance of a class that implements #call, or a Proc to be used to format # the output from this subscriber # Default: Use the built-in formatter (See: #call) @@ -63,13 +87,13 @@ # # 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 + self.formatter = block || formatter + @application = application + @host = host # 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 @@ -77,37 +101,9 @@ # Return the level index for fast comparisons. # Returns the lowest level index if the level has not been explicitly # set for this instance. def level_index @level_index || 0 - end - - # Return formatter that responds to call. - # - # Supports formatter supplied as: - # - Symbol - # - Hash ( Symbol => { options }) - # - Instance of any of SemanticLogger::Formatters - # - Proc - # - Any object that responds to :call - # - If none of the above apply, then the supplied block is returned as the formatter. - # - Otherwise an instance of the default formatter is returned. - def extract_formatter(formatter, &block) - case - when formatter.is_a?(Symbol) - SemanticLogger::Appender.constantize_symbol(formatter, 'SemanticLogger::Formatters').new - when formatter.is_a?(Hash) && formatter.size > 0 - fmt, options = formatter.first - SemanticLogger::Appender.constantize_symbol(fmt.to_sym, 'SemanticLogger::Formatters').new(options) - when formatter.respond_to?(:call) - formatter - when block - block - when respond_to?(:call) - self - else - default_formatter - end end end end