lib/semantic_logger/appender/splunk.rb in semantic_logger-3.2.1 vs lib/semantic_logger/appender/splunk.rb in semantic_logger-3.3.0
- old
+ new
@@ -16,12 +16,12 @@
# host: 'localhost',
# port: 8089,
# scheme: :https,
# index: 'main'
# )
-class SemanticLogger::Appender::Splunk < SemanticLogger::Appender::Base
- attr_reader :config, :index, :service, :service_index
+class SemanticLogger::Appender::Splunk < SemanticLogger::Subscriber
+ attr_reader :config, :index, :service, :service_index, :source_type
# Write to Splunk.
#
# Parameters
# :username [String]
@@ -35,11 +35,11 @@
# :token
# Supply a preauthenticated Splunk token instead of username and password.
# Not required if username and password are supplied.
#
# :host [String]
- # Splunk host name.
+ # Splunk server host name.
# Default: 'localhost'
#
# :port [Integer]
# The Splunk management port.
# Default: 8089
@@ -59,10 +59,21 @@
# Client certificate.
#
# :ssl_client_key [OpenSSL::PKey::RSA | OpenSSL::PKey::DSA]
# Client key.
#
+ # source_type: [String]
+ # Optional: Source type to display in Splunk
+ #
+ # application: [String]
+ # The :source forwarded to Splunk
+ # Default: SemanticLogger.application
+ #
+ # host: [String]
+ # Name of this host to appear in log messages.
+ # Default: SemanticLogger.host
+ #
# level: [:trace | :debug | :info | :warn | :error | :fatal]
# Override the log level for this appender.
# Default: SemanticLogger.default_level
#
# formatter: [Object|Proc]
@@ -73,25 +84,20 @@
# 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, _deprecated_level = nil, &block)
+ def initialize(options = {}, _deprecated_level = nil, &block)
@config = options.dup
@config[:level] = _deprecated_level if _deprecated_level
@index = @config.delete(:index) || 'main'
+ @source_type = options.delete(:source_type)
- options = {
- level: @config.delete(:level) || :error,
- formatter: @config.delete(:formatter),
- filter: @config.delete(:filter)
- }
-
- reopen
-
+ options = extract_subscriber_options!(@config)
# Pass on the level and custom formatter if supplied
super(options, &block)
+ reopen
end
# After forking an active process call #reopen to re-open
# open the handles to resources
def reopen
@@ -103,22 +109,27 @@
end
# Log the message to Splunk
def log(log)
return false unless should_log?(log)
-
- service_index.submit(log.message, formatter.call(log, self))
+ event = formatter.call(log, self)
+ service_index.submit(event.delete(:message), event)
true
end
- # Returns [String] JSON to send to Splunk
+ # Returns [Hash] To send to Splunk
# For splunk format requirements see:
# http://dev.splunk.com/view/event-collector/SP-CAAAE6P
- def call(log, _logger)
- h = log.to_h
- h.delete(:message)
- h.delete(:application)
- h.delete(:host)
+ def call(log, logger)
+ h = log.to_h(nil, nil)
h.delete(:time)
- h
+ message = {
+ source: logger.application,
+ host: logger.host,
+ time: log.time.utc.to_f,
+ message: h.delete(:message),
+ event: h
+ }
+ message[:source_type] = source_type if source_type
+ message
end
end