lib/semantic_logger/appender/honeybadger.rb in semantic_logger-3.2.1 vs lib/semantic_logger/appender/honeybadger.rb in semantic_logger-3.3.0
- old
+ new
@@ -7,50 +7,65 @@
# Send log messages to honeybadger
#
# Example:
# SemanticLogger.add_appender(appender: :honeybadger)
#
-class SemanticLogger::Appender::Honeybadger < SemanticLogger::Appender::Base
+class SemanticLogger::Appender::Honeybadger < SemanticLogger::Subscriber
# Create Appender
#
# Parameters
# level: [:trace | :debug | :info | :warn | :error | :fatal]
# Override the log level for this appender.
# Default: :error
#
- # formatter: [Object|Proc]
+ # formatter: [Object|Proc|Symbol|Hash]
# 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.
+ #
+ # host: [String]
+ # Name of this host to appear in log messages.
+ # Default: SemanticLogger.host
+ #
+ # application: [String]
+ # Name of this application to appear in log messages.
+ # Default: SemanticLogger.application
def initialize(options = {}, &block)
- options = {level: options} unless options.is_a?(Hash)
- @options = options.dup
- level = @options.delete(:level) || :error
-
- super(level, &block)
+ options = options.is_a?(Hash) ? options.dup : {level: options}
+ options[:level] ||= :error
+ super(options, &block)
end
# Send an error notification to honeybadger
def log(log)
return false unless should_log?(log)
+ context = formatter.call(log, self)
if log.exception
- Honeybadger.notify(log.exception, log.to_h)
+ context.delete(:exception)
+ Honeybadger.notify(log.exception, context)
else
message = {
- error_class: log.name,
- error_message: log.message,
- backtrace: log.backtrace,
- context: log.to_h,
+ error_class: context.delete(:name),
+ error_message: context.delete(:message),
+ context: context
}
+ message[:backtrace] = log.backtrace if log.backtrace
Honeybadger.notify(message)
end
true
+ end
+
+ private
+
+ # Use Raw Formatter by default
+ def default_formatter
+ SemanticLogger::Formatters::Raw.new
end
end