Module: Raven::BreadcrumbLogger

Defined in:
lib/raven/breadcrumbs/logger.rb

Constant Summary collapse

LEVELS =
{
  ::Logger::DEBUG => 'debug',
  ::Logger::INFO  => 'info',
  ::Logger::WARN  => 'warn',
  ::Logger::ERROR => 'error',
  ::Logger::FATAL => 'fatal'
}.freeze
EXC_FORMAT =
/^([a-zA-Z0-9]+)\:\s(.*)$/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_exception(message) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/raven/breadcrumbs/logger.rb', line 15

def self.parse_exception(message)
  lines = message.split(/\n\s*/)
  # TODO: wat
  return nil unless lines.length > 2

  match = lines[0].match(EXC_FORMAT)
  return nil unless match

  _, type, value = match.to_a
  [type, value]
end

Instance Method Details

#add(*args) ⇒ Object


27
28
29
30
# File 'lib/raven/breadcrumbs/logger.rb', line 27

def add(*args)
  add_breadcrumb(*args)
  super
end

#add_breadcrumb(severity, message = nil, progname = nil) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/raven/breadcrumbs/logger.rb', line 32

def add_breadcrumb(severity, message = nil, progname = nil)
  message = progname if message.nil? # see Ruby's Logger docs for why
  return if ignored_logger?(progname)
  return if message.nil? || message == ""

  # some loggers will add leading/trailing space as they (incorrectly, mind you)
  # think of logging as a shortcut to std{out,err}
  message = message.strip

  last_crumb = Raven.breadcrumbs.peek
  # try to avoid dupes from logger broadcasts
  if last_crumb.nil? || last_crumb.message != message
    error = Raven::BreadcrumbLogger.parse_exception(message)
    # TODO(dcramer): we need to filter out the "currently captured error"
    if error
      Raven.breadcrumbs.record do |crumb|
        crumb.level = Raven::BreadcrumbLogger::LEVELS.fetch(severity, nil)
        crumb.category = progname || 'error'
        crumb.type = 'error'
        crumb.data = {
          :type => error[0],
          :value => error[1]
        }
      end
    else
      Raven.breadcrumbs.record do |crumb|
        crumb.level = Raven::BreadcrumbLogger::LEVELS.fetch(severity, nil)
        crumb.category = progname || 'logger'
        crumb.message = message
      end
    end
  end
end