module Rollbar
The Rollbar
module. It stores a Rollbar::Notifier
per thread and provides some module methods in order to use the current thread notifier.
Allows a Ruby String to be used to create native Javascript objects when calling JSON#generate.
Example: JSON.generate({ foo: Rollbar::JSON::Value.new('function(){ alert(“bar”) }') })
> '{“foo”:function(){ alert("bar") }}'¶ ↑
MUST use the Ruby JSON
encoder, as in the example. The ActiveSupport encoder, which is installed with Rails
, is invoked when calling Hash#to_json and as_json, and will not work.
Constants
- PUBLIC_NOTIFIER_METHODS
- VERSION
Attributes
Public Class Methods
Clears the current thread notifier and the root notifier. In the practice this should be used only on the specs
# File lib/rollbar.rb, line 119 def clear_notifier! self.notifier = nil self.root_notifier = nil end
Returns the configuration for the current notifier. The current notifier is Rollbar.notifier
and exists one per thread.
# File lib/rollbar.rb, line 84 def configuration notifier.configuration end
Configures the root notifier and loads the plugins
# File lib/rollbar.rb, line 65 def configure(&block) root_notifier.configure(&block) plugins.load! end
# File lib/rollbar.rb, line 96 def last_report Thread.current[:_rollbar_last_report] end
# File lib/rollbar.rb, line 100 def last_report=(report) Thread.current[:_rollbar_last_report] = report end
# File lib/rollbar.rb, line 37 def notifier # Use the global instance @root_notifier so we don't fall # in a infinite loop Thread.current[:_rollbar_notifier] ||= Notifier.new(@root_notifier) end
# File lib/rollbar.rb, line 43 def notifier=(notifier) Thread.current[:_rollbar_notifier] = notifier end
# File lib/rollbar.rb, line 92 def plugins @plugins ||= Rollbar::Plugins.new end
# File lib/rollbar.rb, line 60 def preconfigure(&block) root_notifier.preconfigure(&block) end
Reconfigures the root notifier
# File lib/rollbar.rb, line 72 def reconfigure(&block) root_notifier.reconfigure(&block) end
Backwards compatibility methods
# File lib/rollbar.rb, line 163 def report_exception(exception, request_data = nil, person_data = nil, level = 'error') Kernel.warn('[DEPRECATION] Rollbar.report_exception has been deprecated, ' \ 'please use log() or one of the level functions') scope = {} scope[:request] = request_data if request_data scope[:person] = person_data if person_data Rollbar.scoped(scope) do Rollbar.notifier.log(level, exception, :use_exception_level_filters => true) end end
# File lib/rollbar.rb, line 177 def report_message(message, level = 'info', extra_data = nil) Kernel.warn('[DEPRECATION] Rollbar.report_message has been deprecated, ' \ 'please use log() or one of the level functions') Rollbar.notifier.log(level, message, extra_data) end
# File lib/rollbar.rb, line 184 def report_message_with_request(message, level = 'info', request_data = nil, person_data = nil, extra_data = nil) Kernel.warn('[DEPRECATION] Rollbar.report_message_with_request has been ' \ 'deprecated, please use log() or one of the level functions') scope = {} scope[:request] = request_data if request_data scope[:person] = person_data if person_data Rollbar.scoped(:request => request_data, :person => person_data) do Rollbar.notifier.log(level, message, extra_data) end end
Resets the scope for the current thread notifier. The notifier reference is kept so we reuse the notifier. This is a change from version 2.13.0. Before this version this method clears the notifier.
It was used in order to reset the scope and reusing the global configuration Rollbar.configuration
. Since now Rollbar.configuration
points to the current notifier configuration, we can resue the notifier instance and just reset the scope.
# File lib/rollbar.rb, line 113 def reset_notifier! notifier.reset! end
It's the first notifier instantiated in the process. We store it so all the next per-thread notifiers can inherit its configuration The methods Rollbar.configure
, Rollbar.reconfigure
, Rollbar.preconfigure
and Rollbar.unconfigure
work on this notifier. Before v2.13.0 these methods worked on the global configuration, so in the practice the behavior is the same, since they work on the root notifier's configuration
# File lib/rollbar.rb, line 56 def root_notifier @root_notifier ||= notifier end
# File lib/rollbar.rb, line 88 def safely? configuration.safely? end
# File lib/rollbar.rb, line 157 def scope!(options = {}) notifier.scope!(options) end
Create a new Notifier
instance using the received options and set it as the current thread notifier. The calls to Rollbar
inside the received block will use then this new Notifier
object.
@example
new_scope = { job_type: 'scheduled' } new_config = { use_async: false } Rollbar.scoped(new_scope, new_config) do begin # do stuff rescue => e Rollbar.error(e) end end
# File lib/rollbar.rb, line 141 def scoped(options = {}, config_overrides = {}) old_notifier = notifier self.notifier = old_notifier.scope(options, config_overrides) result = yield result ensure self.notifier = old_notifier end
Unconfigures the root notifier
# File lib/rollbar.rb, line 77 def unconfigure root_notifier.unconfigure end
Create a new Notifier
instance with a new configuration using the current one but merging the passed options.
# File lib/rollbar.rb, line 153 def with_config(overrides, &block) scoped(nil, overrides, &block) end