@@ -12,7 +12,7 @@ class LogEntry #:nodoc:
1212 BINARY_LIMIT_THRESHOLD = 1_000 . freeze
1313 DT_PRECISION = 6 . freeze
1414 MESSAGE_MAX_BYTES = 8192 . freeze
15- LOGTAIL_GEM_REGEX = / \/ logtail(?:-ruby|-rails|-rack)?(?:- \d +(?: \. \d +)*)? \/ lib$/ . freeze
15+ LOGGER_FILE = '/ logtail/logger.rb' . freeze
1616
1717 attr_reader :context_snapshot , :event , :level , :message , :progname , :tags , :time
1818
@@ -39,6 +39,7 @@ def initialize(level, time, progname, message, context_snapshot, event, options
3939 @tags = options [ :tags ]
4040 @context_snapshot = context_snapshot
4141 @event = event
42+ @runtime_context = current_runtime_context || { }
4243 end
4344
4445 # Builds a hash representation containing simple objects, suitable for serialization (JSON).
@@ -64,7 +65,7 @@ def to_hash(options = {})
6465
6566 hash [ :context ] ||= { }
6667 hash [ :context ] [ :runtime ] ||= { }
67- hash [ :context ] [ :runtime ] . merge! ( current_runtime_context || { } )
68+ hash [ :context ] [ :runtime ] . merge! ( @runtime_context )
6869
6970 if options [ :only ]
7071 hash . select do |key , _value |
@@ -114,32 +115,40 @@ def encode_string(string)
114115 end
115116
116117 def current_runtime_context
117- index = caller_locations . rindex { |x | logtail_frame? ( x ) }
118- frame = caller_locations [ index + 1 ] unless index . nil?
119- return convert_to_runtime_context ( frame ) unless frame . nil?
118+ last_logger_invocation_index = caller_locations . rindex { |frame | logtail_logger_frame? ( frame ) }
119+ return { } if last_logger_invocation_index . nil?
120+
121+ calling_frame_index = last_logger_invocation_index + 1
122+ frame = caller_locations [ calling_frame_index ]
123+
124+ return convert_to_runtime_context ( frame )
120125 end
121126
122127 def convert_to_runtime_context ( frame )
123128 {
124- file : relative_to_main_module ( frame . absolute_path ) ,
129+ file : path_relative_to_app_root ( frame ) ,
125130 line : frame . lineno ,
126131 frame_label : frame . label ,
127132 }
128133 end
129134
130- def logtail_frame? ( frame )
131- return false if frame . absolute_path . nil? || logtail_gem_paths . empty?
132- logtail_gem_paths . any? { |path | frame . absolute_path . start_with? ( path ) }
135+ def logtail_logger_frame? ( frame )
136+ !frame . absolute_path . nil? && frame . absolute_path . end_with? ( LOGGER_FILE )
133137 end
134138
135- def logtail_gem_paths
136- @logtail_gem_paths ||= $LOAD_PATH . select { | path | path . match ( LOGTAIL_GEM_REGEX ) }
139+ def path_relative_to_app_root ( frame )
140+ Pathname . new ( frame . absolute_path ) . relative_path_from ( root_path ) . to_s
137141 end
138142
139- def relative_to_main_module ( path )
140- base_file = caller_locations . last . absolute_path
141- base_path = Pathname . new ( File . dirname ( base_file || '/' ) )
142- Pathname . new ( path ) . relative_path_from ( base_path ) . to_s
143+ def root_path
144+ if Object . const_defined? ( 'Rails' )
145+ Rails . root . to_s
146+ elsif Object . const_defined? ( 'Rack::Directory' )
147+ Rack ::Directory . new ( '' ) . root
148+ else
149+ base_file = caller_locations . last . absolute_path
150+ Pathname . new ( File . dirname ( base_file || '/' ) )
151+ end
143152 end
144153 end
145154end
0 commit comments