lib/semantic_logger/appender/bugsnag.rb in semantic_logger-3.0.1 vs lib/semantic_logger/appender/bugsnag.rb in semantic_logger-3.1.0

- old
+ new

@@ -5,55 +5,57 @@ end # Send log messages to Bugsnag # # Example: -# SemanticLogger.add_appender(SemanticLogger::Appender::Bugsnag.new) +# SemanticLogger.add_appender(appender: :bugsnag) # class SemanticLogger::Appender::Bugsnag < SemanticLogger::Appender::Base - # Create Appender + # Create Bugsnag Error / Exception Appender # # Parameters # level: [:trace | :debug | :info | :warn | :error | :fatal] # Override the log level for this appender. # Default: :error # + # formatter: [Object|Proc] + # An instance of a class that implements #call, or a Proc to be used to format + # the output from this appender + # Default: Use the built-in formatter (See: #call) + # # 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) + # Backward compatibility + options = {level: options} unless options.is_a?(Hash) + options = options.dup + options[:level] = :error unless options.has_key?(:level) - raise 'Bugsnag only supports :info, :warn, or :error log levels' unless [:info, :warn, :error].include?(level) + raise 'Bugsnag only supports :info, :warn, or :error log levels' unless [:info, :warn, :error].include?(options[:level]) # 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) + + super(options, &block) end # Returns [Hash] of parameters to send to Bugsnag. - def default_formatter - Proc.new do |log| - h = log.to_h - h[:severity] = log_level(log) - h.delete(:time) - h.delete(:exception) - h - end + def call(log, logger) + h = log.to_h + h[:severity] = log_level(log) + h.delete(:time) + h.delete(:exception) + h 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) || # Ignore logs coming from Bugsnag itself (log.name == 'Bugsnag') || # Filtered out? !include_message?(log) @@ -73,9 +75,17 @@ true end private + # Bugsnag supports: error, warning or info def log_level(log) - log.level == :warn ? 'warning' : log.level.to_s + case log.level + when :error, :fatal + 'error' + when :warn + 'warning' + else + 'info' + end end end