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

- old
+ new

@@ -45,11 +45,14 @@ # For example, the number of inquiries or, the amount purchased etc. # # context [Hash] # Named contexts that were captured when the log entry was created. class Log - attr_accessor :level, :thread_name, :name, :message, :payload, :time, :duration, :tags, :level_index, :exception, :metric, :backtrace, :metric_amount, :named_tags, :context + attr_accessor :level, :level_index, :name, :message, :time, :duration, + :payload, :exception, :thread_name, :backtrace, + :tags, :named_tags, :context, + :metric, :metric_amount, :dimensions def initialize(name, level, index = nil) @level = level @thread_name = Thread.current.name @name = name @@ -63,11 +66,21 @@ # # Returns [true|false] whether this log entry should be logged # # Example: # logger.info(name: 'value') - def assign(message: nil, payload: nil, min_duration: 0.0, exception: nil, metric: nil, metric_amount: 1, duration: nil, backtrace: nil, log_exception: :full, on_exception_level: nil) + def assign(message: nil, + payload: nil, + min_duration: 0.0, + exception: nil, + metric: nil, + metric_amount: nil, + duration: nil, + backtrace: nil, + log_exception: :full, + on_exception_level: nil, + dimensions: nil) # Elastic logging: Log when :duration exceeds :min_duration # Except if there is an exception when it will always be logged if duration self.duration = duration return false if (duration < min_duration) && exception.nil? @@ -93,18 +106,19 @@ self.level_index = SemanticLogger.level_to_index(level) end end if backtrace - self.backtrace = self.class.cleanse_backtrace(backtrace) + self.backtrace = Utils.cleanse_backtrace(backtrace) elsif level_index >= SemanticLogger.backtrace_level_index - self.backtrace = self.class.cleanse_backtrace + self.backtrace = Utils.cleanse_backtrace end if metric self.metric = metric self.metric_amount = metric_amount + self.dimensions = dimensions end self.payload = payload if payload && (payload.size > 0) true end @@ -123,10 +137,12 @@ exception = message message = nil elsif exception.nil? && payload && payload.respond_to?(:backtrace) && payload.respond_to?(:message) exception = payload payload = nil + elsif payload.is_a?(String) + message = message.nil? ? payload : "#{message} -- #{payload}" end # Add result of block as message or payload if not nil if block_given? && (result = yield) if result.is_a?(String) @@ -275,19 +291,12 @@ # Lazy initializes the context hash and assigns a key value pair. def set_context(key, value) (self.context ||= {})[key] = value end - private - - SELF_PATTERN = File.join('lib', 'semantic_logger') - - # Extract the backtrace leaving out Semantic Logger - def self.cleanse_backtrace(stack = caller) - while (first = stack.first) && first.include?(SELF_PATTERN) - stack.shift - end - stack + # A metric only event has a metric but no message, exception, or payload. + def metric_only? + metric && message.nil? && exception.nil? && payload.nil? end end end