lib/logtail/log_entry.rb in logtail-0.1.2 vs lib/logtail/log_entry.rb in logtail-0.1.3

- old
+ new

@@ -10,11 +10,11 @@ # `Logger` and the log device that you set it up with. class LogEntry #:nodoc: BINARY_LIMIT_THRESHOLD = 1_000.freeze DT_PRECISION = 6.freeze MESSAGE_MAX_BYTES = 8192.freeze - LOGTAIL_GEM_REGEX = /\/logtail(?:-ruby|-rails|-rack)?(?:-\d+(?:\.\d+)*)?\/lib$/.freeze + LOGGER_FILE = '/logtail/logger.rb'.freeze attr_reader :context_snapshot, :event, :level, :message, :progname, :tags, :time # Creates a log entry suitable to be sent to the Logtail API. # @param level [Integer] the log level / severity @@ -37,10 +37,11 @@ @message = message.is_a?(String) ? message : message.inspect @message = @message.byteslice(0, MESSAGE_MAX_BYTES) @tags = options[:tags] @context_snapshot = context_snapshot @event = event + @runtime_context = current_runtime_context || {} end # Builds a hash representation containing simple objects, suitable for serialization (JSON). def to_hash(options = {}) options ||= {} @@ -62,11 +63,11 @@ hash[:context] = context_snapshot end hash[:context] ||= {} hash[:context][:runtime] ||= {} - hash[:context][:runtime].merge!(current_runtime_context || {}) + hash[:context][:runtime].merge!(@runtime_context) if options[:only] hash.select do |key, _value| options[:only].include?(key) end @@ -112,34 +113,42 @@ rescue Exception nil end def current_runtime_context - index = caller_locations.rindex { |x| logtail_frame?(x) } - frame = caller_locations[index + 1] unless index.nil? - return convert_to_runtime_context(frame) unless frame.nil? + last_logger_invocation_index = caller_locations.rindex { |frame| logtail_logger_frame?(frame) } + return {} if last_logger_invocation_index.nil? + + calling_frame_index = last_logger_invocation_index + 1 + frame = caller_locations[calling_frame_index] + + return convert_to_runtime_context(frame) end def convert_to_runtime_context(frame) { - file: relative_to_main_module(frame.absolute_path), + file: path_relative_to_app_root(frame), line: frame.lineno, frame_label: frame.label, } end - def logtail_frame?(frame) - return false if frame.absolute_path.nil? || logtail_gem_paths.empty? - logtail_gem_paths.any? { |path| frame.absolute_path.start_with?(path) } + def logtail_logger_frame?(frame) + !frame.absolute_path.nil? && frame.absolute_path.end_with?(LOGGER_FILE) end - def logtail_gem_paths - @logtail_gem_paths ||= $LOAD_PATH.select { |path| path.match(LOGTAIL_GEM_REGEX) } + def path_relative_to_app_root(frame) + Pathname.new(frame.absolute_path).relative_path_from(root_path).to_s end - def relative_to_main_module(path) - base_file = caller_locations.last.absolute_path - base_path = Pathname.new(File.dirname(base_file || '/')) - Pathname.new(path).relative_path_from(base_path).to_s + def root_path + if Object.const_defined?('Rails') + Rails.root.to_s + elsif Object.const_defined?('Rack::Directory') + Rack::Directory.new('').root + else + base_file = caller_locations.last.absolute_path + Pathname.new(File.dirname(base_file || '/')) + end end end end