Class: Raven::Event
- Inherits:
-
Object
- Object
- Raven::Event
- 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 `(.+?)')?$/
- PLATFORM =
"ruby"
Instance Attribute Summary collapse
-
#culprit ⇒ Object
Returns the value of attribute culprit.
-
#extra ⇒ Object
Returns the value of attribute extra.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#level ⇒ Object
Returns the value of attribute level.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#message ⇒ Object
Returns the value of attribute message.
-
#modules ⇒ Object
Returns the value of attribute modules.
-
#project ⇒ Object
Returns the value of attribute project.
-
#server_name ⇒ Object
Returns the value of attribute server_name.
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Class Method Summary collapse
-
._source_lines(path, from, to) ⇒ Object
Because linecache can go to hell.
- .capture_exception(exc, options = {}, &block) ⇒ Object (also: captureException)
- .capture_message(message, options = {}) ⇒ Object (also: captureMessage)
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #get_culprit(frames) ⇒ Object
- #get_file_context(filename, lineno, context) ⇒ Object
- #get_hostname ⇒ Object
- #get_modules ⇒ Object
-
#initialize(options = {}, &block) ⇒ Event
constructor
A new instance of Event.
- #interface(name, value = nil, &block) ⇒ Object
- #parse_exception(exception) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(options = {}, &block) ⇒ Event
Returns a new instance of Event.
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 61 62 63 |
# File 'lib/raven/event.rb', line 29 def initialize(={}, &block) @configuration = [:configuration] || Raven.configuration @interfaces = {} context = [:context] || Raven.context @id = [:id] || UUIDTools::UUID.random_create.hexdigest @message = [:message] @timestamp = [:timestamp] || Time.now.utc @level = [:level] || :error @logger = [:logger] || 'root' @culprit = [:culprit] @server_name = [:server_name] || @configuration.server_name || get_hostname if @configuration.send_modules [:modules] ||= get_modules end @modules = [:modules] @user = [:user] || {} @user.merge!(context.user) @extra = [:extra] || {} @extra.merge!(context.extra) @tags = [:tags] || {} @tags.merge!(context.) 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) end |
Instance Attribute Details
#culprit ⇒ Object
Returns the value of attribute culprit.
27 28 29 |
# File 'lib/raven/event.rb', line 27 def culprit @culprit end |
#extra ⇒ Object
Returns the value of attribute extra.
27 28 29 |
# File 'lib/raven/event.rb', line 27 def extra @extra end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
25 26 27 |
# File 'lib/raven/event.rb', line 25 def id @id end |
#level ⇒ Object
Returns the value of attribute level.
26 27 28 |
# File 'lib/raven/event.rb', line 26 def level @level end |
#logger ⇒ Object
Returns the value of attribute logger.
27 28 29 |
# File 'lib/raven/event.rb', line 27 def logger @logger end |
#message ⇒ Object
Returns the value of attribute message.
26 27 28 |
# File 'lib/raven/event.rb', line 26 def @message end |
#modules ⇒ Object
Returns the value of attribute modules.
27 28 29 |
# File 'lib/raven/event.rb', line 27 def modules @modules end |
#project ⇒ Object
Returns the value of attribute project.
26 27 28 |
# File 'lib/raven/event.rb', line 26 def project @project end |
#server_name ⇒ Object
Returns the value of attribute server_name.
27 28 29 |
# File 'lib/raven/event.rb', line 27 def server_name @server_name end |
#tags ⇒ Object
Returns the value of attribute tags.
27 28 29 |
# File 'lib/raven/event.rb', line 27 def @tags end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
26 27 28 |
# File 'lib/raven/event.rb', line 26 def @timestamp end |
Class Method Details
._source_lines(path, from, to) ⇒ Object
Because linecache can go to hell
164 165 |
# File 'lib/raven/event.rb', line 164 def self._source_lines(path, from, to) end |
.capture_exception(exc, options = {}, &block) ⇒ Object Also known as: captureException
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/raven/event.rb', line 113 def self.capture_exception(exc, ={}, &block) configuration = [: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() do |evt| evt. = "#{exc.class.to_s}: #{exc.}" evt.level = [: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 { |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 and frame.abs_path frame.pre_context, frame.context_line, frame.post_context = \ evt.get_file_context(frame.abs_path, frame.lineno, context_lines) end end }.select{ |f| f.filename } 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
153 154 155 156 157 158 159 160 161 |
# File 'lib/raven/event.rb', line 153 def self.(, ={}) new() do |evt| evt. = evt.level = [:level] || :error evt.interface :message do |int| int. = end end end |
Instance Method Details
#[](key) ⇒ Object
83 84 85 |
# File 'lib/raven/event.rb', line 83 def [](key) interface(key) end |
#[]=(key, value) ⇒ Object
87 88 89 |
# File 'lib/raven/event.rb', line 87 def []=(key, value) interface(key, value) end |
#get_culprit(frames) ⇒ Object
174 175 176 177 |
# File 'lib/raven/event.rb', line 174 def get_culprit(frames) lastframe = frames.reverse.detect { |f| f.in_app } || frames.last "#{lastframe.filename} in #{lastframe.function}" if lastframe end |
#get_file_context(filename, lineno, context) ⇒ Object
167 168 169 170 171 172 |
# File 'lib/raven/event.rb', line 167 def get_file_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_hostname ⇒ Object
65 66 67 68 69 |
# File 'lib/raven/event.rb', line 65 def get_hostname # 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 end |
#get_modules ⇒ Object
71 72 73 74 |
# File 'lib/raven/event.rb', line 71 def get_modules # Older versions of Rubygems don't support iterating over all specs Hash[Gem::Specification.map {|spec| [spec.name, spec.version.to_s]}] if Gem::Specification.respond_to?(:map) end |
#interface(name, value = nil, &block) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/raven/event.rb', line 76 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
179 180 181 182 183 184 185 |
# File 'lib/raven/event.rb', line 179 def parse_exception(exception) interface(:exception) do |int| int.type = exception.class.to_s int.value = exception. int.module = exception.class.to_s.split('::')[0...-1].join('::') end end |
#to_hash ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/raven/event.rb', line 91 def to_hash data = { 'event_id' => @id, 'message' => @message, 'timestamp' => @timestamp, 'level' => @level, 'project' => @project, 'logger' => @logger, 'platform' => PLATFORM, } data['culprit'] = @culprit if @culprit data['server_name'] = @server_name if @server_name data['modules'] = @modules if @modules data['extra'] = @extra if @extra data['tags'] = @tags if @tags data['sentry.interfaces.User'] = @user if @user @interfaces.each_pair do |name, int_data| data[name] = int_data.to_hash end data end |