lib/semantic_logger/subscriber.rb in semantic_logger-4.2.2 vs lib/semantic_logger/subscriber.rb in semantic_logger-4.3.0

- old
+ new

@@ -3,11 +3,11 @@ # Abstract base class for all appenders. module SemanticLogger class Subscriber < SemanticLogger::Base # Every appender has its own formatter attr_reader :formatter - attr_writer :application, :host, :logger + attr_writer :application, :host, :logger, :metrics # Returns the current log level if set, otherwise it logs everything it receives. def level @level || :trace end @@ -57,11 +57,11 @@ end end # Whether this log entry meets the criteria to be logged by this appender. def should_log?(log) - super(log) && !log.metric_only? + super(log) && (log.metric_only? ? metrics? : true) end private # Initializer for Abstract Class SemanticLogger::Subscriber @@ -86,14 +86,19 @@ # Default: SemanticLogger.application # # 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) + # + # metrics: [Boolean] + # Whether to log metric only entries with this subscriber. + # Default: false + def initialize(level: nil, formatter: nil, filter: nil, application: nil, host: nil, metrics: false, &block) self.formatter = block || formatter @application = application @host = host + @metrics = metrics # 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 @@ -101,8 +106,13 @@ # 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 + + # Whether to log metric only entries with this subscriber + def metrics? + @metrics end end end