Class: SemanticLogger::Log
- Inherits:
-
Struct
- Object
- Struct
- SemanticLogger::Log
- Defined in:
- lib/semantic_logger/log.rb
Overview
Log Struct
Structure for holding all log entries
level
Log level of the supplied log call
:trace, :debug, :info, :warn, :error, :fatal
thread_name
Name of the thread in which the logging call was called
name
Class name supplied to the logging instance
message
Text to be logged
payload
Optional Hash or Ruby Exception object to be logged
time
The time at which the log entry was created
duration
The time taken to complete a measure call
tags
Any tags active on the thread when the log call was made
level_index
Internal index of the log level
exception
Ruby Exception object to log
metric [Object]
Object supplied when measure_x was called
backtrace [Array<String>]
The backtrace captured at source when the log level >= SemanticLogger.backtrace_level
metric_amount [Numeric]
Used for numeric or counter metrics.
For example, the number of inquiries or, the amount purchased etc.
Constant Summary collapse
- MAX_EXCEPTIONS_TO_UNWRAP =
5
- 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
Returns the value of attribute payload.
-
#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
-
#each_exception ⇒ Object
Call the block for exception and any nested exception.
-
#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 JRuby only supports time in ms DEPRECATED.
-
#has_payload? ⇒ Boolean
Returns [true|false] whether the log entry has a payload.
-
#level_to_s ⇒ Object
Returns [String] single character upper case log level.
-
#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 = SemanticLogger.host, application = SemanticLogger.application) ⇒ Object
Returns [Hash] representation of this log entry.
Instance Attribute Details
#backtrace ⇒ Object
Returns the value of attribute backtrace
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def backtrace @backtrace end |
#duration ⇒ Object
Returns the value of attribute duration
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def duration @duration end |
#exception ⇒ Object
Returns the value of attribute exception
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def exception @exception end |
#level ⇒ Object
Returns the value of attribute level
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def level @level end |
#level_index ⇒ Object
Returns the value of attribute level_index
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def level_index @level_index end |
#message ⇒ Object
Returns the value of attribute message
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def @message end |
#metric ⇒ Object
Returns the value of attribute metric
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def metric @metric end |
#metric_amount ⇒ Object
Returns the value of attribute metric_amount
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def metric_amount @metric_amount end |
#name ⇒ Object
Returns the value of attribute name
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def name @name end |
#payload ⇒ Object
Returns the value of attribute payload
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def payload @payload end |
#tags ⇒ Object
Returns the value of attribute tags
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def @tags end |
#thread_name ⇒ Object
Returns the value of attribute thread_name
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def thread_name @thread_name end |
#time ⇒ Object
Returns the value of attribute time
46 47 48 |
# File 'lib/semantic_logger/log.rb', line 46 def time @time end |
Instance Method Details
#backtrace_to_s ⇒ Object
Returns [String] the exception backtrace including all of the child / caused by exceptions
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/semantic_logger/log.rb', line 72 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
148 149 150 |
# File 'lib/semantic_logger/log.rb', line 148 def .to_s.gsub(/(\e(\[([\d;]*[mz]?))?)?/, '').strip end |
#duration_human ⇒ Object
Returns [String] the duration in human readable form
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/semantic_logger/log.rb', line 99 def duration_human return nil unless duration seconds = duration / 1000 if seconds >= 86400.0 # 1 day "#{(seconds / 86400).to_i}d #{Time.at(seconds).strftime('%-Hh %-Mm')}" elsif seconds >= 3600.0 # 1 hour Time.at(seconds).strftime('%-Hh %-Mm') elsif seconds >= 60.0 # 1 minute Time.at(seconds).strftime('%-Mm %-Ss') elsif seconds >= 1.0 # 1 second "#{'%.3f' % seconds}s" else duration_to_s end end |
#duration_to_s ⇒ Object
88 89 90 |
# File 'lib/semantic_logger/log.rb', line 88 def duration_to_s "#{duration.to_i}ms" if duration end |
#each_exception ⇒ Object
Call the block for exception and any nested exception
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/semantic_logger/log.rb', line 50 def each_exception # With thanks to https://github.com/bugsnag/bugsnag-ruby/blob/6348306e44323eee347896843d16c690cd7c4362/lib/bugsnag/notification.rb#L81 depth = 0 exceptions = [] ex = exception while ex != nil && !exceptions.include?(ex) && exceptions.length < MAX_EXCEPTIONS_TO_UNWRAP exceptions << ex yield(ex, depth) depth += 1 ex = if ex.respond_to?(:cause) && ex.cause ex.cause elsif ex.respond_to?(:continued_exception) && ex.continued_exception ex.continued_exception elsif ex.respond_to?(:original_exception) && ex.original_exception ex.original_exception end end end |
#extract_file_and_line(stack, short_name = false) ⇒ Object
Extract the filename and line number from the last entry in the supplied backtrace
133 134 135 136 |
# File 'lib/semantic_logger/log.rb', line 133 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
140 141 142 143 144 145 |
# File 'lib/semantic_logger/log.rb', line 140 def file_name_and_line(short_name = false) if backtrace || (exception && exception.backtrace) stack = backtrace || exception.backtrace extract_file_and_line(stack, short_name) if stack && stack.size > 0 end end |
#formatted_time ⇒ Object
Return the Time as a formatted string JRuby only supports time in ms DEPRECATED
167 168 169 |
# File 'lib/semantic_logger/log.rb', line 167 def formatted_time "#{time.strftime('%Y-%m-%d %H:%M:%S')}.#{'%03d' % (time.usec/1000)}" end |
#has_payload? ⇒ Boolean
Returns [true|false] whether the log entry has a payload
159 160 161 |
# File 'lib/semantic_logger/log.rb', line 159 def has_payload? !(payload.nil? || (payload.respond_to?(:empty?) && payload.empty?)) end |
#level_to_s ⇒ Object
Returns [String] single character upper case log level
116 117 118 |
# File 'lib/semantic_logger/log.rb', line 116 def level_to_s level.to_s[0..0].upcase end |
#payload_to_s ⇒ Object
Return the payload in text form Returns nil if payload is missing or empty
154 155 156 |
# File 'lib/semantic_logger/log.rb', line 154 def payload_to_s payload.inspect if has_payload? end |
#process_info(thread_name_length = 30) ⇒ Object
Returns [String] the available process info Example:
18934:thread 23 test_logging.rb:51
123 124 125 126 127 128 |
# File 'lib/semantic_logger/log.rb', line 123 def process_info(thread_name_length = 30) file, line = file_name_and_line(true) file_name = " #{file}:#{line}" if file "#{$$}:#{"%.#{thread_name_length}s" % thread_name}#{file_name}" end |
#to_h(host = SemanticLogger.host, application = SemanticLogger.application) ⇒ Object
Returns [Hash] representation of this log entry
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 232 233 234 235 236 |
# File 'lib/semantic_logger/log.rb', line 180 def to_h(host = SemanticLogger.host, application = SemanticLogger.application) # Header h = { name: name, pid: $$, thread: thread_name, time: time, level: level, level_index: level_index, } h[:host] = host if host h[:application] = application if application file, line = file_name_and_line if file h[:file] = file h[:line] = line.to_i end # Tags h[:tags] = if && (.size > 0) # 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 |