lib/semantic_logger/logger.rb in semantic_logger-3.0.1 vs lib/semantic_logger/logger.rb in semantic_logger-3.1.0

- old
+ new

@@ -1,10 +1,12 @@ require 'concurrent' module SemanticLogger # Logger stores the class name to be used for all log messages so that every # log message written by this instance will include the class name class Logger < Base + include SemanticLogger::Concerns::Compatibility + # Returns a Logger instance # # Return the logger for a specific class, supports class specific log levels # logger = SemanticLogger::Logger.new(self) # OR @@ -93,24 +95,33 @@ def self.cache_count warn '[DEPRECATION] SemanticLogger::Logger.cache_count is deprecated. Please use SemanticLogger::Logger.queue_size instead.' queue_size end - # Supply a block to be called whenever a metric is seen during benchmark logging + # Supply a block to be called whenever a metric is seen during measure logging # # Parameters # block # The block to be called # # Example: - # SemanticLogger.on_metric do |log_struct| - # puts "#{log_struct.metric} was received. Log Struct: #{log_struct.inspect}" + # SemanticLogger.on_metric do |log| + # puts "#{log.metric} was received. Log Struct: #{log.inspect}" # end - def self.on_metric(&block) - (@@metric_subscribers ||= Concurrent::Array.new) << block + def self.on_metric(object = nil, &block) + raise('When supplying an object, it must support the #call method') if object && !object.respond_to?(:call) + (@@metric_subscribers ||= Concurrent::Array.new) << (object || block) end + # Place log request on the queue for the Appender thread to write to each + # appender in the order that they were registered + def log(log, message = nil, progname = nil, &block) + # Compatibility with ::Logger + return add(log, message, progname, &block) unless log.is_a?(SemanticLogger::Log) + self.class.queue << log if @@appender_thread + end + private @@appender_thread = nil @@queue = Queue.new @@metric_subscribers = nil @@ -118,16 +129,10 @@ # Queue to hold messages that need to be logged to the various appenders def self.queue @@queue end - # Place log request on the queue for the Appender thread to write to each - # appender in the order that they were registered - def log(log) - self.class.queue << log if @@appender_thread - end - # Internal logger for SemanticLogger # For example when an appender is not working etc.. # By default logs to STDERR def self.logger @@logger ||= begin @@ -217,16 +222,16 @@ end end end # Call Metric subscribers - def self.call_metric_subscribers(log_struct) + def self.call_metric_subscribers(log) # If no subscribers registered, then return immediately return unless @@metric_subscribers @@metric_subscribers.each do |subscriber| begin - subscriber.call(log_struct) + subscriber.call(log) rescue Exception => exc logger.error 'Exception calling metrics subscriber', exc end end end