Class: Raven::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/raven/event.rb

Constant Summary collapse

LOG_LEVELS =
{
  "debug" => 10,
  "info" => 20,
  "warn" => 30,
  "warning" => 30,
  "error" => 40,
  "fatal" => 50,
}
BACKTRACE_RE =
/^(.+?):(\d+)(?::in `(.+?)')?$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Event

Returns a new instance of Event.

Raises:



27
28
29
30
31
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
# File 'lib/raven/event.rb', line 27

def initialize(options={}, &block)
  @configuration = options[:configuration] || Raven.configuration
  @interfaces = {}

  @id = options[:id] || UUIDTools::UUID.random_create.hexdigest
  @message = options[:message]
  @timestamp = options[:timestamp] || Time.now.utc
  @level = options[:level] || :error
  @logger = options[:logger] || 'root'
  @culprit = options[:culprit]
  @extra = options[:extra]
  @tags = options[:tags]

  # Try to resolve the hostname to an FQDN, but fall back to whatever the load name is
  hostname = Socket.gethostname
  hostname = Socket.gethostbyname(hostname).first rescue hostname
  @server_name = options[:server_name] || hostname

  # Older versions of Rubygems don't support iterating over all specs
  if @configuration.send_modules && Gem::Specification.respond_to?(:map)
    options[:modules] ||= Hash[Gem::Specification.map {|spec| [spec.name, spec.version.to_s]}]
  end
  @modules = options[:modules]

  block.call(self) if block

  # Some type coercion
  @timestamp = @timestamp.strftime('%Y-%m-%dT%H:%M:%S') if @timestamp.is_a?(Time)
  @level = LOG_LEVELS[@level.to_s.downcase] if @level.is_a?(String) || @level.is_a?(Symbol)

  # Basic sanity checking
  raise Error.new('A message is required for all events') unless @message && !@message.empty?
  raise Error.new('A timestamp is required for all events') unless @timestamp
end

Instance Attribute Details

#culpritObject

Returns the value of attribute culprit.



25
26
27
# File 'lib/raven/event.rb', line 25

def culprit
  @culprit
end

#extraObject

Returns the value of attribute extra.



25
26
27
# File 'lib/raven/event.rb', line 25

def extra
  @extra
end

#idObject (readonly)

Returns the value of attribute id.



23
24
25
# File 'lib/raven/event.rb', line 23

def id
  @id
end

#levelObject

Returns the value of attribute level.



24
25
26
# File 'lib/raven/event.rb', line 24

def level
  @level
end

#loggerObject

Returns the value of attribute logger.



25
26
27
# File 'lib/raven/event.rb', line 25

def logger
  @logger
end

#messageObject

Returns the value of attribute message.



24
25
26
# File 'lib/raven/event.rb', line 24

def message
  @message
end

#modulesObject

Returns the value of attribute modules.



25
26
27
# File 'lib/raven/event.rb', line 25

def modules
  @modules
end

#projectObject

Returns the value of attribute project.



24
25
26
# File 'lib/raven/event.rb', line 24

def project
  @project
end

#server_nameObject

Returns the value of attribute server_name.



25
26
27
# File 'lib/raven/event.rb', line 25

def server_name
  @server_name
end

#tagsObject

Returns the value of attribute tags.



25
26
27
# File 'lib/raven/event.rb', line 25

def tags
  @tags
end

#timestampObject

Returns the value of attribute timestamp.



24
25
26
# File 'lib/raven/event.rb', line 24

def timestamp
  @timestamp
end

Class Method Details

._source_lines(path, from, to) ⇒ Object

Because linecache can go to hell



157
158
# File 'lib/raven/event.rb', line 157

def self._source_lines(path, from, to)
end

.capture_exception(exc, options = {}, &block) ⇒ Object Also known as: captureException



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/raven/event.rb', line 97

def self.capture_exception(exc, options={}, &block)
  configuration = options[:configuration] || Raven.configuration
  if exc.is_a?(Raven::Error)
    # Try to prevent error reporting loops
    Raven.logger.info "Refusing to capture Raven error: #{exc.inspect}"
    return nil
  end
  if configuration[:excluded_exceptions].any? { |x| x === exc || x == exc.class.name }
    Raven.logger.info "User excluded error: #{exc.inspect}"
    return nil
  end

  context_lines = configuration[:context_lines]

  new(options) do |evt|
    evt.message = "#{exc.class.to_s}: #{exc.message}"
    evt.level = options[:level] || :error
    evt.parse_exception(exc)
    if (exc.backtrace)
      evt.interface :stack_trace do |int|
        backtrace = Backtrace.parse(exc.backtrace)
        int.frames = backtrace.lines.reverse.map do |line|
          int.frame do |frame|
            frame.abs_path = line.file
            frame.function = line.method
            frame.lineno = line.number
            frame.in_app = line.in_app
            if context_lines
              frame.pre_context, frame.context_line, frame.post_context = \
                evt.get_context(frame.abs_path, frame.lineno, context_lines)
            end
          end
        end
        evt.culprit = evt.get_culprit(int.frames)
      end
    end
    block.call(evt) if block
  end
end

.capture_message(message, options = {}) ⇒ Object Also known as: captureMessage



146
147
148
149
150
151
152
153
154
# File 'lib/raven/event.rb', line 146

def self.capture_message(message, options={})
  new(options) do |evt|
    evt.message = message
    evt.level = options[:level] || :error
    evt.interface :message do |int|
      int.message = message
    end
  end
end

.capture_rack_exception(exc, rack_env, options = {}, &block) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/raven/event.rb', line 137

def self.capture_rack_exception(exc, rack_env, options={}, &block)
  capture_exception(exc, options) do |evt|
    evt.interface :http do |int|
      int.from_rack(rack_env)
    end
    block.call(evt) if block
  end
end

Instance Method Details

#[](key) ⇒ Object



69
70
71
# File 'lib/raven/event.rb', line 69

def [](key)
  interface(key)
end

#[]=(key, value) ⇒ Object



73
74
75
# File 'lib/raven/event.rb', line 73

def []=(key, value)
  interface(key, value)
end

#get_context(filename, lineno, context) ⇒ Object



160
161
162
163
164
165
# File 'lib/raven/event.rb', line 160

def get_context(filename, lineno, context)
  lines = (2 * context + 1).times.map do |i|
    Raven::LineCache::getline(filename, lineno - context + i)
  end
  [lines[0..(context-1)], lines[context], lines[(context+1)..-1]]
end

#get_culprit(frames) ⇒ Object



167
168
169
170
# File 'lib/raven/event.rb', line 167

def get_culprit(frames)
  lastframe = frames[-1]
  "#{lastframe.filename} in #{lastframe.function}" if lastframe
end

#interface(name, value = nil, &block) ⇒ Object

Raises:



62
63
64
65
66
67
# File 'lib/raven/event.rb', line 62

def interface(name, value=nil, &block)
  int = Raven::find_interface(name)
  raise Error.new("Unknown interface: #{name}") unless int
  @interfaces[int.name] = int.new(value, &block) if value || block
  @interfaces[int.name]
end

#parse_exception(exception) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/raven/event.rb', line 172

def parse_exception(exception)
  interface(:exception) do |int|
    int.type = exception.class.to_s
    int.value = exception.message
    int.module = exception.class.to_s.split('::')[0...-1].join('::')
  end
end

#to_hashObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/raven/event.rb', line 77

def to_hash
  data = {
    'event_id' => self.id,
    'message' => self.message,
    'timestamp' => self.timestamp,
    'level' => self.level,
    'project' => self.project,
    'logger' => self.logger,
  }
  data['culprit'] = self.culprit if self.culprit
  data['server_name'] = self.server_name if self.server_name
  data['modules'] = self.modules if self.modules
  data['extra'] = self.extra if self.extra
  data['tags'] = self.tags if self.tags
  @interfaces.each_pair do |name, int_data|
    data[name] = int_data.to_hash
  end
  data
end