lib/semantic_logger/base.rb in semantic_logger-2.21.0 vs lib/semantic_logger/base.rb in semantic_logger-3.0.0

- old
+ new

@@ -326,22 +326,27 @@ if self.payload payload = payload.nil? ? self.payload : self.payload.merge(payload) end # Add caller stack trace - backtrace = - if index >= SemanticLogger.backtrace_level_index - trace = caller - # Remove call to this internal method - trace.shift(1) - trace - end + backtrace = extract_backtrace if index >= SemanticLogger.backtrace_level_index struct = Log.new(level, Thread.current.name, name, message, payload, Time.now, nil, tags, index, exception, nil, backtrace) log(struct) if include_message?(struct) end + SELF_PATTERN = File.join('lib', 'semantic_logger') + + # Extract the callers backtrace leaving out Semantic Logger + def extract_backtrace + stack = caller + while (first = stack.first) && first.include?(SELF_PATTERN) + stack.shift + end + stack + end + # Measure the supplied block and log the message def benchmark_internal(level, index, message, params) start = Time.now exception = nil begin @@ -406,18 +411,10 @@ log(struct) if include_message?(struct) raise exception elsif duration >= min_duration # Only log if the block took longer than 'min_duration' to complete # Add caller stack trace - backtrace = - if index >= SemanticLogger.backtrace_level_index - trace = caller - # Remove call to this internal method - trace.shift - # Ruby 1.9 has additional stack entry for parent that calls this method - trace.shift if RUBY_VERSION.to_f <= 2.0 - trace - end + backtrace = extract_backtrace if index >= SemanticLogger.backtrace_level_index struct = Log.new(level, Thread.current.name, name, message, payload, end_time, duration, tags, index, nil, metric, backtrace) log(struct) if include_message?(struct) end end