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 benchmark 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 benchmark_x was called
backtrace [Array<String>]
The backtrace captured at source when the log level >= SemanticLogger.backtrace_level
Constant Summary collapse
- MAX_EXCEPTIONS_TO_UNWRAP =
5
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.
-
#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
-
#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(&block) ⇒ Object
Call the block for exception and any nested exception.
-
#file_name_and_line ⇒ 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.
-
#level_to_s ⇒ Object
Returns [String] single character upper case log level.
-
#process_info(thread_name_length = 30) ⇒ Object
Returns [String] the available process info Example: 18934:thread 23 test_logging.rb:51.
Instance Attribute Details
#backtrace ⇒ Object
Returns the value of attribute backtrace
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def backtrace @backtrace end |
#duration ⇒ Object
Returns the value of attribute duration
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def duration @duration end |
#exception ⇒ Object
Returns the value of attribute exception
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def exception @exception end |
#level ⇒ Object
Returns the value of attribute level
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def level @level end |
#level_index ⇒ Object
Returns the value of attribute level_index
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def level_index @level_index end |
#message ⇒ Object
Returns the value of attribute message
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def @message end |
#metric ⇒ Object
Returns the value of attribute metric
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def metric @metric end |
#name ⇒ Object
Returns the value of attribute name
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def name @name end |
#payload ⇒ Object
Returns the value of attribute payload
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def payload @payload end |
#tags ⇒ Object
Returns the value of attribute tags
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def @tags end |
#thread_name ⇒ Object
Returns the value of attribute thread_name
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def thread_name @thread_name end |
#time ⇒ Object
Returns the value of attribute time
42 43 44 |
# File 'lib/semantic_logger/log.rb', line 42 def time @time end |
Instance Method Details
#cleansed_message ⇒ Object
Strip the standard Rails colorizing from the logged message
123 124 125 |
# File 'lib/semantic_logger/log.rb', line 123 def .to_s.gsub(/(\e(\[([\d;]*[mz]?))?)?/, '').strip end |
#duration_human ⇒ Object
Returns [String] the duration in human readable form
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/semantic_logger/log.rb', line 82 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 Time.at(seconds).strftime('%-Ss %Lms') else duration_to_s end end |
#duration_to_s ⇒ Object
71 72 73 |
# File 'lib/semantic_logger/log.rb', line 71 def duration_to_s "#{duration.to_i}ms" if duration end |
#each_exception(&block) ⇒ Object
Call the block for exception and any nested exception
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/semantic_logger/log.rb', line 46 def each_exception(&block) # 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 block.call(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 |
#file_name_and_line ⇒ Object
Returns [String, String] the file_name and line_number from the backtrace supplied in either the backtrace or exception
115 116 117 118 119 120 |
# File 'lib/semantic_logger/log.rb', line 115 def file_name_and_line if backtrace || (exception && exception.backtrace) stacktrace = backtrace || exception.backtrace stacktrace[0].split('/').last.split(':')[0..1] if stacktrace && stacktrace.size > 0 end end |
#formatted_time ⇒ Object
Return the Time as a formatted string JRuby only supports time in ms
130 131 132 |
# File 'lib/semantic_logger/log.rb', line 130 def formatted_time "#{time.strftime('%Y-%m-%d %H:%M:%S')}.#{'%03d' % (time.usec/1000)}" end |
#level_to_s ⇒ Object
Returns [String] single character upper case log level
99 100 101 |
# File 'lib/semantic_logger/log.rb', line 99 def level_to_s level.to_s[0..0].upcase end |
#process_info(thread_name_length = 30) ⇒ Object
Returns [String] the available process info Example:
18934:thread 23 test_logging.rb:51
106 107 108 109 110 111 |
# File 'lib/semantic_logger/log.rb', line 106 def process_info(thread_name_length = 30) file, line = file_name_and_line file_name = " #{file}:#{line}" if file "#{$$}:#{"%.#{thread_name_length}s" % thread_name}#{file_name}" end |