Class: Sapience::Log
- Inherits:
-
Struct
- Object
- Struct
- Sapience::Log
- Defined in:
- lib/sapience/log.rb
Overview
rubocop:disable LineLength
Constant Summary collapse
- MAX_EXCEPTIONS_TO_UNWRAP =
5
- MILLISECONDS_IN_SECOND =
1_000
- MILLISECONDS_IN_MINUTE =
60_000
- MILLISECONDS_IN_HOUR =
3_600_000
- MILLISECONDS_IN_DAY =
86_400_000
- CALLER_REGEXP =
/^(.*):(\d+).*/
Instance Attribute Summary collapse
-
#backtrace ⇒ Object
Returns the value of attribute backtrace.
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#exception ⇒ Object
Returns the value of attribute exception.
-
#level ⇒ Object
Returns the value of attribute level.
-
#level_index ⇒ Object
Returns the value of attribute level_index.
-
#message ⇒ Object
Returns the value of attribute message.
-
#metric ⇒ Object
Returns the value of attribute metric.
-
#metric_amount ⇒ Object
Returns the value of attribute metric_amount.
-
#name ⇒ Object
Returns the value of attribute name.
-
#payload ⇒ Object
This filtering is specifically designed for Rack-based payloads which may have sensitive information such as “password” or “credit_card” in its hash.
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#thread_name ⇒ Object
Returns the value of attribute thread_name.
-
#time ⇒ Object
Returns the value of attribute time.
Instance Method Summary collapse
-
#backtrace_to_s ⇒ Object
Returns [String] the exception backtrace including all of the child / caused by exceptions.
-
#cleansed_message ⇒ Object
Strip the standard Rails colorizing from the logged message.
-
#duration_human ⇒ Object
Returns [String] the duration in human readable form.
-
#duration_to_s ⇒ Object
Returns [String] duration of the log entry as a string Returns nil if their is no duration.
-
#extract_file_and_line(stack, short_name = false) ⇒ Object
Extract the filename and line number from the last entry in the supplied backtrace.
-
#file_name_and_line(short_name = false) ⇒ Object
Returns [String, String] the file_name and line_number from the backtrace supplied in either the backtrace or exception.
-
#formatted_time ⇒ Object
Return the Time as a formatted string Ruby MRI supports micro seconds DEPRECATED.
-
#level_to_s ⇒ Object
Returns [String] single character upper case log level.
-
#payload? ⇒ Boolean
Returns [true|false] whether the log entry has a payload.
-
#payload_to_s ⇒ Object
Return the payload in text form Returns nil if payload is missing or empty.
-
#process_info(thread_name_length = 30) ⇒ Object
Returns [String] the available process info Example: 18934:thread 23 test_logging.rb:51.
-
#to_h(host = Sapience.config.host, app_name = Sapience.app_name) ⇒ Object
Returns [Hash] representation of this log entry.
Instance Attribute Details
#backtrace ⇒ Object
Returns the value of attribute backtrace
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def backtrace @backtrace end |
#duration ⇒ Object
Returns the value of attribute duration
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def duration @duration end |
#exception ⇒ Object
Returns the value of attribute exception
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def exception @exception end |
#level ⇒ Object
Returns the value of attribute level
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def level @level end |
#level_index ⇒ Object
Returns the value of attribute level_index
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def level_index @level_index end |
#message ⇒ Object
Returns the value of attribute message
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def @message end |
#metric ⇒ Object
Returns the value of attribute metric
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def metric @metric end |
#metric_amount ⇒ Object
Returns the value of attribute metric_amount
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def metric_amount @metric_amount end |
#name ⇒ Object
Returns the value of attribute name
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def name @name end |
#payload ⇒ Object
This filtering is specifically designed for Rack-based payloads which may have sensitive information such as “password” or “credit_card” in its hash. We need to obfuscate these fields.
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def payload @payload end |
#tags ⇒ Object
Returns the value of attribute tags
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def @tags end |
#thread_name ⇒ Object
Returns the value of attribute thread_name
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def thread_name @thread_name end |
#time ⇒ Object
Returns the value of attribute time
49 50 51 |
# File 'lib/sapience/log.rb', line 49 def time @time end |
Instance Method Details
#backtrace_to_s ⇒ Object
Returns [String] the exception backtrace including all of the child / caused by exceptions
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sapience/log.rb', line 57 def backtrace_to_s trace = "" each_exception do |exception, i| if i == 0 trace += (exception.backtrace || []).join("\n") else trace += "\nCause: #{exception.class.name}: #{exception.}\n#{(exception.backtrace || []).join("\n")}" end end trace end |
#cleansed_message ⇒ Object
Strip the standard Rails colorizing from the logged message
134 135 136 |
# File 'lib/sapience/log.rb', line 134 def .to_s.gsub(/(\e(\[([\d;]*[mz]?))?)?/, "").strip end |
#duration_human ⇒ Object
Returns [String] the duration in human readable form
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/sapience/log.rb', line 77 def duration_human # rubocop:disable AbcSize, CyclomaticComplexity, PerceivedComplexity return nil unless duration days, ms = duration.divmod(MILLISECONDS_IN_DAY) hours, ms = ms.divmod(MILLISECONDS_IN_HOUR) minutes, ms = ms.divmod(MILLISECONDS_IN_MINUTE) seconds, ms = ms.divmod(MILLISECONDS_IN_SECOND) str = "" str += "#{days}d" if days > 0 str += " #{hours}h" if hours > 0 str += " #{minutes}m" if minutes > 0 str += " #{seconds}s" if seconds > 0 str += " #{ms}ms" if ms > 0 if days > 0 || hours > 0 || minutes > 0 str.strip else if seconds >= 1.0 format "%.3fs", duration / MILLISECONDS_IN_SECOND.to_f else duration_to_s end end end |
#duration_to_s ⇒ Object
Returns [String] duration of the log entry as a string Returns nil if their is no duration
71 72 73 74 |
# File 'lib/sapience/log.rb', line 71 def duration_to_s return unless duration format((duration < 10.0 ? "%.3fms" : "%.1fms"), duration) end |
#extract_file_and_line(stack, short_name = false) ⇒ Object
Extract the filename and line number from the last entry in the supplied backtrace
120 121 122 123 |
# File 'lib/sapience/log.rb', line 120 def extract_file_and_line(stack, short_name = false) match = CALLER_REGEXP.match(stack.first) [short_name ? File.basename(match[1]) : match[1], match[2].to_i] end |
#file_name_and_line(short_name = false) ⇒ Object
Returns [String, String] the file_name and line_number from the backtrace supplied in either the backtrace or exception
127 128 129 130 131 |
# File 'lib/sapience/log.rb', line 127 def file_name_and_line(short_name = false) # rubocop:disable CyclomaticComplexity return unless backtrace || (exception && exception.backtrace) # rubocop: disable Style/SafeNavigation stack = backtrace || exception.backtrace extract_file_and_line(stack, short_name) if stack && !stack.empty? end |
#formatted_time ⇒ Object
Return the Time as a formatted string Ruby MRI supports micro seconds DEPRECATED
170 171 172 |
# File 'lib/sapience/log.rb', line 170 def formatted_time format("#{time.strftime("%Y-%m-%d %H:%M:%S")}.%06d", time.usec) # rubocop: disable Style/FormatStringToken end |
#level_to_s ⇒ Object
Returns [String] single character upper case log level
103 104 105 |
# File 'lib/sapience/log.rb', line 103 def level_to_s level.to_s[0..0].upcase end |
#payload? ⇒ Boolean
Returns [true|false] whether the log entry has a payload
163 164 165 |
# File 'lib/sapience/log.rb', line 163 def payload? !(payload.nil? || (payload.respond_to?(:empty?) && payload.empty?)) end |
#payload_to_s ⇒ Object
Return the payload in text form Returns nil if payload is missing or empty
140 141 142 |
# File 'lib/sapience/log.rb', line 140 def payload_to_s payload.inspect if payload? end |
#process_info(thread_name_length = 30) ⇒ Object
Returns [String] the available process info Example:
18934:thread 23 test_logging.rb:51
110 111 112 113 114 115 |
# File 'lib/sapience/log.rb', line 110 def process_info(thread_name_length = 30) file, line = file_name_and_line(true) file_name = " #{file}:#{line}" if file format "#{$PROCESS_ID}:%.#{thread_name_length}s#{file_name}", thread_name end |
#to_h(host = Sapience.config.host, app_name = Sapience.app_name) ⇒ Object
Returns [Hash] representation of this log entry
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/sapience/log.rb', line 175 def to_h(host = Sapience.config.host, app_name = Sapience.app_name) # rubocop:disable AbcSize, CyclomaticComplexity, PerceivedComplexity, LineLength # Header h = { name: name, pid: $PROCESS_ID, thread: thread_name, time: time, level: level, level_index: level_index, } h[:host] = host if host h[:app_name] = app_name if app_name file, line = file_name_and_line if file h[:file] = file h[:line] = line.to_i end # Tags h[:tags] = if && !.empty? # Duration if duration h[:duration_ms] = duration h[:duration] = duration_human end # Log message h[:message] = if # Payload if payload if payload.is_a?(Hash) h.merge!(payload) else h[:payload] = payload end end # Exceptions if exception root = h each_exception do |exception, i| name = i == 0 ? :exception : :cause root[name] = { name: exception.class.name, message: exception., stack_trace: exception.backtrace, } root = root[name] end end # Metric h[:metric] = metric if metric h end |