lib/semantic_logger/appender/http.rb in semantic_logger-3.0.1 vs lib/semantic_logger/appender/http.rb in semantic_logger-3.1.0
- old
+ new
@@ -9,18 +9,14 @@
# * JSON Formatted messages.
# * Uses a persistent http connection, if the server supports it.
# * SSL encryption (https).
#
# Example:
-# appender = SemanticLogger::Appender::Http.new(
-# url: 'http://localhost:8088/path'
+# SemanticLogger.add_appender(
+# appender: :http,
+# url: 'http://localhost:8088/path'
# )
-#
-# # Optional: Exclude health_check log entries, etc.
-# appender.filter = Proc.new { |log| log.message !~ /(health_check|Not logged in)/}
-#
-# SemanticLogger.add_appender(appender)
class SemanticLogger::Appender::Http < SemanticLogger::Appender::Base
attr_accessor :username, :application, :host, :compress, :header
attr_reader :http, :url, :server, :port, :path, :ssl_options
# Create HTTP(S) log appender
@@ -59,27 +55,32 @@
#
# level: [:trace | :debug | :info | :warn | :error | :fatal]
# Override the log level for this appender.
# Default: SemanticLogger.default_level
#
+ # 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 = options.dup
- level = @options.delete(:level)
- filter = @options.delete(:filter)
+ options = options.dup
@url = options.delete(:url)
@ssl_options = options.delete(:ssl)
@username = options.delete(:username)
@password = options.delete(:password)
@application = options.delete(:application) || 'Semantic Logger'
@host = options.delete(:host) || SemanticLogger.host
@compress = options.delete(:compress) || false
- raise(ArgumentError, "Unknown options: #{options.inspect}") if options.size > 0
+ unless options.has_key?(:formatter)
+ options[:formatter] = block || (respond_to?(:call) ? self : SemanticLogger::Formatters::Json.new)
+ end
raise(ArgumentError, 'Missing mandatory parameter :url') unless @url
@header = {
'Accept' => 'application/json',
@@ -94,16 +95,16 @@
raise(ArgumentError, "Invalid format for :url: #{@url.inspect}. Should be similar to: 'http://hostname:port/path'") unless @url
@port = uri.port
@username = uri.user if !@username && uri.user
@password = uri.password if !@password && uri.password
- @path = uri.request_uri
+ @path = uri.path
reopen
# Pass on the level and custom formatter if supplied
- super(level, filter, &block)
+ super(options)
end
# Re-open after process fork
def reopen
# On Ruby v2.0 and greater, Net::HTTP.new uses a persistent connection if the server allows it
@@ -112,16 +113,10 @@
# Forward log messages to HTTP Server
def log(log)
return false if (level_index > (log.level_index || 0)) ||
!include_message?(log) # Filtered out?
-
post(formatter.call(log, self))
- end
-
- # Use the JSON formatter
- def default_formatter
- self.class.json_formatter
end
private
def compress_data(data)