Class: SemanticLogger::Log

Inherits:
Struct
  • Object
show all
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 message 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
CALLER_REGEXP =
/^(.*):(\d+).*/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backtraceObject

Returns the value of attribute backtrace

Returns:

  • (Object)

    the current value of backtrace



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def backtrace
  @backtrace
end

#durationObject

Returns the value of attribute duration

Returns:

  • (Object)

    the current value of duration



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def duration
  @duration
end

#exceptionObject

Returns the value of attribute exception

Returns:

  • (Object)

    the current value of exception



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def exception
  @exception
end

#levelObject

Returns the value of attribute level

Returns:

  • (Object)

    the current value of level



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def level
  @level
end

#level_indexObject

Returns the value of attribute level_index

Returns:

  • (Object)

    the current value of level_index



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def level_index
  @level_index
end

#messageObject

Returns the value of attribute message

Returns:

  • (Object)

    the current value of message



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def message
  @message
end

#metricObject

Returns the value of attribute metric

Returns:

  • (Object)

    the current value of metric



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def metric
  @metric
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def name
  @name
end

#payloadObject

Returns the value of attribute payload

Returns:

  • (Object)

    the current value of payload



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def payload
  @payload
end

#tagsObject

Returns the value of attribute tags

Returns:

  • (Object)

    the current value of tags



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def tags
  @tags
end

#thread_nameObject

Returns the value of attribute thread_name

Returns:

  • (Object)

    the current value of thread_name



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def thread_name
  @thread_name
end

#timeObject

Returns the value of attribute time

Returns:

  • (Object)

    the current value of time



42
43
44
# File 'lib/semantic_logger/log.rb', line 42

def time
  @time
end

Instance Method Details

#backtrace_to_sObject

Returns [String] the exception backtrace including all of the child / caused by exceptions



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/semantic_logger/log.rb', line 68

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.message}\n#{(exception.backtrace || []).join("\n")}"
    end
  end
  trace
end

#cleansed_messageObject

Strip the standard Rails colorizing from the logged message



144
145
146
# File 'lib/semantic_logger/log.rb', line 144

def cleansed_message
  message.to_s.gsub(/(\e(\[([\d;]*[mz]?))?)?/, '').strip
end

#duration_humanObject

Returns [String] the duration in human readable form



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/semantic_logger/log.rb', line 95

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_sObject



84
85
86
# File 'lib/semantic_logger/log.rb', line 84

def duration_to_s
  "#{duration.to_i}ms" if duration
end

#each_exceptionObject

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
  # 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



129
130
131
132
# File 'lib/semantic_logger/log.rb', line 129

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



136
137
138
139
140
141
# File 'lib/semantic_logger/log.rb', line 136

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_timeObject

Return the Time as a formatted string JRuby only supports time in ms



161
162
163
# File 'lib/semantic_logger/log.rb', line 161

def formatted_time
  "#{time.strftime('%Y-%m-%d %H:%M:%S')}.#{'%03d' % (time.usec/1000)}"
end

#level_to_sObject

Returns [String] single character upper case log level



112
113
114
# File 'lib/semantic_logger/log.rb', line 112

def level_to_s
  level.to_s[0..0].upcase
end

#payload_to_s(colorized = false) ⇒ Object

Return the payload in text form Returns nil if payload is missing or empty



150
151
152
153
154
155
156
# File 'lib/semantic_logger/log.rb', line 150

def payload_to_s(colorized = false)
  return if payload.nil? || (payload.respond_to?(:empty?) && payload.empty?)
  return payload.inspect if !colorized || !defined?(AwesomePrint) || !payload.respond_to?(:ai)

  # Colorize the payload if the AwesomePrint gem is loaded
  payload.ai(multiline: false) rescue payload.inspect
end

#process_info(thread_name_length = 30) ⇒ Object

Returns [String] the available process info Example:

18934:thread 23 test_logging.rb:51


119
120
121
122
123
124
# File 'lib/semantic_logger/log.rb', line 119

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_hObject

Returns [Hash] representation of this log entry



173
174
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
# File 'lib/semantic_logger/log.rb', line 173

def to_h
  # Header
  h          = {
    host:        SemanticLogger.host,
    application: SemanticLogger.application,
    name:        name,
    pid:         $$,
    thread:      thread_name,
    time:        time,
    level:       level,
    level_index: level_index,
  }
  file, line = file_name_and_line
  if file
    h[:file] = file
    h[:line] = line.to_i
  end

  # Tags
  h[:tags] = tags if tags && (tags.size > 0)

  # Duration
  if duration
    h[:duration_ms] = duration
    h[:duration]    = duration_human
  end

  # Log message
  h[:message] = cleansed_message if message

  # 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.message,
        stack_trace: exception.backtrace
      }
      root       = root[name]
    end
  end

  # Metric
  h[:metric] = metric if metric
  h
end