lib/logtail/log_entry.rb in logtail-ruby-0.1.2 vs lib/logtail/log_entry.rb in logtail-ruby-0.1.3
- old
+ new
@@ -1,7 +1,8 @@
require "socket"
require "time"
+require "pathname"
require "logtail/contexts"
require "logtail/events"
module Logtail
@@ -9,10 +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
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
@@ -43,11 +45,11 @@
def to_hash(options = {})
options ||= {}
hash = {
:level => level,
:dt => formatted_dt,
- :message => message
+ :message => message,
}
if !tags.nil? && tags.length > 0
hash[:tags] = tags
end
@@ -58,10 +60,14 @@
if !context_snapshot.nil? && context_snapshot.length > 0
hash[:context] = context_snapshot
end
+ hash[:context] ||= {}
+ hash[:context][:runtime] ||= {}
+ hash[:context][:runtime].merge!(current_runtime_context || {})
+
if options[:only]
hash.select do |key, _value|
options[:only].include?(key)
end
elsif options[:except]
@@ -103,8 +109,37 @@
:undef => :replace,
:replace => '?'
})
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?
+ end
+
+ def convert_to_runtime_context(frame)
+ {
+ file: relative_to_main_module(frame.absolute_path),
+ 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) }
+ end
+
+ def logtail_gem_paths
+ @logtail_gem_paths ||= $LOAD_PATH.select { |path| path.match(LOGTAIL_GEM_REGEX) }
+ 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
end
end
end