Class: SemanticLogger::CallSite
- Inherits:
-
Data
- Object
- Data
- SemanticLogger::CallSite
- Defined in:
- lib/semantic_logger/call_site.rb,
lib/semantic_logger/call_site.rb
Overview
Where a log entry was logged: the path and line of the code that made the log call, and the label of its frame, usually the method, such as "Order#submit".
Log#assign sets it when the entry's level is at or above SemanticLogger.call_site_level. When the log call supplies a backtrace, the call site is its first line. Otherwise it is the first frame of the calling thread's stack outside Semantic Logger and SemanticLogger.call_site_skip_paths, which costs far less than capturing a backtrace, since only the frames up to it are read.
A call site is frozen and holds only frozen Strings and an Integer, so that a log entry logged in another Ractor can be sent to the main one.
Attributes:
path: [String]
The file, as in a backtrace, such as "/app/models/order.rb".
line: [Integer]
The line number in the file.
label: [String|nil]
The label of the frame, as in a backtrace. On Ruby 3.4 and later it includes the class, such
as "Order#submit" or "Order.find_open", on Ruby 3.2 and 3.3 only the method, such as "submit".
A block's label starts with "block in", and code outside a method has a label such as
"<main>" or "<class:Order>".
Instance Attribute Summary collapse
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.capture ⇒ Object
Returns [CallSite|nil] the first frame of the calling thread's stack outside Semantic Logger, Ruby's own methods, and SemanticLogger.call_site_skip_paths, and other than the frame of a method such as public_send that called the logger for it.
-
.from(call_site) ⇒ Object
Returns [CallSite] the call site, or the call site of a Thread::Backtrace::Location.
-
.locate(call_site: nil, backtrace: nil) ⇒ Object
Returns [CallSite|nil] where a log entry was logged, from what the log call supplied: call_site: [CallSite|Thread::Backtrace::Location|false|nil] The call site, or the frame it is made from.
-
.parse(line) ⇒ Object
Returns [CallSite|nil] the call site in a line of a backtrace, such as "app.rb:12:in 'Order#submit'".
-
.supplied?(value) ⇒ Boolean
Whether the value can be supplied as a log entry's call site (see .locate).
Instance Method Summary collapse
-
#file_name_and_line(short_name: false) ⇒ Object
Returns [Array<String, Integer>] the file name and line number of the call site.
-
#to_s ⇒ Object
Returns [String] the call site as Ruby 3.4 and later write it in a backtrace, such as "/app/models/order.rb:42:in 'Order#submit'".
-
#well_formed? ⇒ Boolean
Whether the path is a String, the line an Integer, and the label a String or nil, as the formatters need.
Instance Attribute Details
#label ⇒ Object (readonly)
Returns the value of attribute label
23 24 25 |
# File 'lib/semantic_logger/call_site.rb', line 23 def label @label end |
#line ⇒ Object (readonly)
Returns the value of attribute line
23 24 25 |
# File 'lib/semantic_logger/call_site.rb', line 23 def line @line end |
#path ⇒ Object (readonly)
Returns the value of attribute path
23 24 25 |
# File 'lib/semantic_logger/call_site.rb', line 23 def path @path end |
Class Method Details
.capture ⇒ Object
Returns [CallSite|nil] the first frame of the calling thread's stack outside Semantic Logger, Ruby's own methods, and SemanticLogger.call_site_skip_paths, and other than the frame of a method such as public_send that called the logger for it. nil when every frame is skipped.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/semantic_logger/call_site.rb', line 120 def self.capture skip_paths = SemanticLogger.call_site_skip_paths # A Ractor other than the main one cannot use the main Ractor's cache. cache = RactorSupport.other_ractor? ? nil : skip_cache(skip_paths) start = 1 # One frame more than the window, which is only read as the caller of the frame before it. # caller_locations returns nil, or on JRuby an empty Array, once start is past the end of the stack. while (locations = caller_locations(start, WINDOW + 1)) && !locations.empty? locations.each_with_index do |location, index| break if index == WINDOW path = location.path next if skip?(path, skip_paths, cache) label = location.label next if dispatch?(location, label, locations[index + 1]) return new(-path, location.lineno, label && -label) end start += WINDOW end end |
.from(call_site) ⇒ Object
Returns [CallSite] the call site, or the call site of a Thread::Backtrace::Location. A call site whose Strings are frozen is returned as it is; otherwise its Strings are copied, frozen and deduplicated, since the same few paths and labels are logged again and again.
93 94 95 96 97 98 99 |
# File 'lib/semantic_logger/call_site.rb', line 93 def self.from(call_site) is_call_site = CallSite === call_site # rubocop:disable Style/CaseEquality return call_site if is_call_site && call_site.path.frozen? && (call_site.label.nil? || call_site.label.frozen?) label = call_site.label new(frozen_copy(call_site.path), is_call_site ? call_site.line : call_site.lineno, label && frozen_copy(label)) end |
.locate(call_site: nil, backtrace: nil) ⇒ Object
Returns [CallSite|nil] where a log entry was logged, from what the log call supplied:
call_site: [CallSite|Thread::Backtrace::Location|false|nil]
The call site, or the frame it is made from. false: the entry has no call site.
backtrace: [Array<String>|nil]
Without a call site, its first line is the call site.
Without either (or with an empty backtrace), the first frame of the calling thread's stack outside Semantic Logger (see .capture).
83 84 85 86 87 88 |
# File 'lib/semantic_logger/call_site.rb', line 83 def self.locate(call_site: nil, backtrace: nil) return if false.equal?(call_site) return from(call_site) if call_site backtrace.nil? || backtrace.empty? ? capture : parse(backtrace.first) end |
.parse(line) ⇒ Object
Returns [CallSite|nil] the call site in a line of a backtrace, such as "app.rb:12:in 'Order#submit'". nil when the line is not a String with a path and line number, or is not text that Ruby can match, such as invalid UTF-8.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/semantic_logger/call_site.rb', line 146 def self.parse(line) return unless line.is_a?(String) && line.encoding.ascii_compatible? && line.valid_encoding? return if line.include?(LINE_BREAK) # The first ":12:in '" is the end of the path, since a label can hold anything. The label's # closing quote must follow its opening one. match = LINE_AND_LABEL.match(line) if line.end_with?(QUOTE) if match&.begin(0)&.positive? && match.end(0) < line.size return new(-match.pre_match, match[1].to_i, -line[match.end(0)...-1]) end match = UNLABELED_LINE.match(line) new(-match[1], match[2].to_i, nil) if match end |
.supplied?(value) ⇒ Boolean
Whether the value can be supplied as a log entry's call site (see .locate). A value that parsed data, such as JSON, can hold is not, other than false, so that untrusted data cannot set the location of an entry. Never calls a method of the value, which can be any object in a payload.
111 112 113 114 115 |
# File 'lib/semantic_logger/call_site.rb', line 111 def self.supplied?(value) return true if false.equal?(value) || ::Thread::Backtrace::Location === value # rubocop:disable Style/CaseEquality CallSite === value && value.well_formed? # rubocop:disable Style/CaseEquality end |
Instance Method Details
#file_name_and_line(short_name: false) ⇒ Object
Returns [Array<String, Integer>] the file name and line number of the call site. With short_name the file name has no directory, such as "order.rb".
225 226 227 |
# File 'lib/semantic_logger/call_site.rb', line 225 def file_name_and_line(short_name: false) [short_name ? File.basename(path) : path, line] end |
#to_s ⇒ Object
Returns [String] the call site as Ruby 3.4 and later write it in a backtrace, such as "/app/models/order.rb:42:in 'Order#submit'".
231 232 233 |
# File 'lib/semantic_logger/call_site.rb', line 231 def to_s label ? "#{path}:#{line}:in '#{label}'" : "#{path}:#{line}" end |
#well_formed? ⇒ Boolean
Whether the path is a String, the line an Integer, and the label a String or nil, as the formatters need. A call site that Semantic Logger makes always is; one that a log call supplies is checked, and otherwise logged as part of the payload, like any other value that is not valid for its argument.
Like .supplied?, calls no method of the values, which can be any objects.
219 220 221 |
# File 'lib/semantic_logger/call_site.rb', line 219 def well_formed? String === path && Integer === line && (nil.equal?(label) || String === label) # rubocop:disable Style/CaseEquality end |