lib/semantic_logger/appender/bugsnag.rb in semantic_logger-2.21.0 vs lib/semantic_logger/appender/bugsnag.rb in semantic_logger-3.0.0
- old
+ new
@@ -1,76 +1,59 @@
-=begin
-Bugsnag appender for SemanticLogger
-
-Skips the fatal log level because unrescued exceptions get logged as fatal and will be reported automatically by Bugsnag.
-
-Note: Payload information is not filtered, so take care not to push any sensitive information when logging with tags or a payload.
-
-Example 1
-
-Adding the Bugsnag appender will send :error log entries to Bugsnag with the error severity.
-
-For a Rails application already configured to use SemanticLogger and Bugsnag, create a file called <Rails Root>/config/initializers/bugsnag_appender.rb with the following contents and restart the application:
-
-# Send :error and log messages to Bugsnag
-SemanticLogger.add_appender(SemanticLogger::Appender::Bugsnag.new)
-Rails.logger.info 'SemanticLogger Bugsnag Appender added.'
-
-Example 2
-
-For a non-Rails application, send :info and more severe log entries to a file called application.log and also send :error log entries to Bugsnag.
-
-require 'semantic_logger'
-require 'bugsnag'
-
-# Bugsnag setup
-Bugsnag.configure do |config|
- config.api_key = 'abc123'
+begin
+ require 'bugsnag'
+rescue LoadError
+ raise 'Gem bugsnag is required for logging purposes. Please add the gem "bugsnag" to your Gemfile.'
end
-# SemanticLogger setup
-SemanticLogger.default_level = :info
-SemanticLogger.add_appender('application.log')
-SemanticLogger.add_appender(SemanticLogger::Appender::Bugsnag.new)
-logger = SemanticLogger['Example']
+# Send log messages to Bugsnag
+#
+# Example:
+# SemanticLogger.add_appender(SemanticLogger::Appender::Bugsnag.new)
+#
+class SemanticLogger::Appender::Bugsnag < SemanticLogger::Appender::Base
+ # Create Appender
+ #
+ # Parameters
+ # level: [:trace | :debug | :info | :warn | :error | :fatal]
+ # Override the log level for this appender.
+ # Default: :error
+ #
+ # filter: [Regexp|Proc]
+ # RegExp: Only include log messages where the class name matches the supplied.
+ # regular expression. All other messages will be ignored.
+ # Proc: Only include log messages where the supplied Proc returns true
+ # The Proc must return true or false.
+ def initialize(options = {}, &block)
+ options = {level: options} unless options.is_a?(Hash)
+ @options = options.dup
+ level = @options.delete(:level) || :error
+ filter = @options.delete(:filter)
-# Log some messages
-logger.info 'This is only written to application.log'
-logger.error 'This is written to application.log and will also be sent to Bugsnag as an error event'
+ raise 'Bugsnag only supports :info, :warn, or :error log levels' unless [:info, :warn, :error].include?(level)
-# The appender will send payloads to Bugsnag
-logger.error 'Something bad happened', info: 'Related information'
-=end
-
-require 'bugsnag'
-
-class SemanticLogger::Appender::Bugsnag < SemanticLogger::Appender::Base
- # Allow the level for this appender to be overwritten
- # Default: :error
- # Note: Not recommended to set the log level to :info, :debug, or :trace as that would flood Bugsnag with Error notices
- def initialize(level = :error, &block)
# Replace the Bugsnag logger so that we can identify its log messages and not forward them to Bugsnag
Bugsnag.configure { |config| config.logger = SemanticLogger[Bugsnag] }
super(level, &block)
end
# Returns [Hash] of parameters to send to Bugsnag.
def default_formatter
- proc do |log|
- h = {severity: log_level(log), tags: log.tags, class: log.name}
- h[:message] = log.message if log.exception
- h.merge!(log.payload) if log.payload
+ Proc.new do |log|
+ h = log.to_h
+ h[:severity] = log_level(log)
+ h.delete(:time)
+ h.delete(:exception)
h
end
end
# Send an error notification to Bugsnag
def log(log)
# Only log if level is warn, or error.
return false if (level_index > (log.level_index || 0)) ||
# We don't want to send fatal as those are already captured by Bugsnag.
- (log.level == :fatal) ||
+ #(log.level == :fatal) ||
# Ignore logs coming from Bugsnag itself
(log.name == 'Bugsnag') ||
# Filtered out?
!include_message?(log)
@@ -81,12 +64,13 @@
else
error = RuntimeError.new(log.message)
error.set_backtrace(log.backtrace) if log.backtrace
error
end
+
# For more documentation on the Bugsnag.notify method see:
# https://bugsnag.com/docs/notifiers/ruby#sending-handled-exceptions
- Bugsnag.notify(exception, formatter.call(log))
+ Bugsnag.notify(exception, formatter.call(log, self))
true
end
private