lib/semantic_logger/appender/http.rb in semantic_logger-4.1.1 vs lib/semantic_logger/appender/http.rb in semantic_logger-4.2.0
- old
+ new
@@ -1,9 +1,10 @@
require 'net/http'
require 'uri'
require 'socket'
require 'json'
+require 'openssl'
# Log to any HTTP(S) server that accepts log messages in JSON form
#
# Features:
# * JSON Formatted messages.
@@ -14,11 +15,11 @@
# SemanticLogger.add_appender(
# appender: :http,
# url: 'http://localhost:8088/path'
# )
class SemanticLogger::Appender::Http < SemanticLogger::Subscriber
- attr_accessor :username, :application, :host, :compress, :header,
+ attr_accessor :username, :compress, :header,
:open_timeout, :read_timeout, :continue_timeout
attr_reader :http, :url, :server, :port, :path, :ssl_options
# Create HTTP(S) log appender
#
@@ -75,26 +76,38 @@
# read_timeout: [Float]
# Default: 1.0
#
# continue_timeout: [Float]
# Default: 1.0
- def initialize(url:, compress: false, ssl: {}, username: nil, password: nil, open_timeout: 2.0, read_timeout: 1.0, continue_timeout: 1.0,
- level: nil, formatter: nil, filter: nil, application: nil, host: nil, &block)
+ def initialize(url:,
+ compress: false,
+ ssl: {},
+ username: nil,
+ password: nil,
+ open_timeout: 2.0,
+ read_timeout: 1.0,
+ continue_timeout: 1.0,
+ level: nil,
+ formatter: nil,
+ filter: nil,
+ application: nil,
+ host: nil,
+ &block)
@url = url
@ssl_options = ssl
@username = username
@password = password
@compress = compress
@open_timeout = open_timeout
@read_timeout = read_timeout
@continue_timeout = continue_timeout
+ # On Ruby v2.0 and greater, Net::HTTP.new already uses a persistent connection if the server allows it
@header = {
'Accept' => 'application/json',
'Content-Type' => 'application/json',
- # On Ruby v2.0 and greater, Net::HTTP.new already uses a persistent connection if the server allows it
'Connection' => 'keep-alive',
'Keep-Alive' => '300'
}
@header['Content-Encoding'] = 'gzip' if @compress
@@ -146,13 +159,13 @@
@http.start
end
# Forward log messages to HTTP Server
def log(log)
- return false unless should_log?(log)
-
- post(formatter.call(log, self))
+ message = formatter.call(log, self)
+ logger.trace(message)
+ post(message)
end
private
# Use JSON Formatter by default
@@ -187,20 +200,22 @@
end
# Process HTTP Request
def process_request(request, body = nil)
if body
- body = compress_data(body) if compress
- request.body = body
+ request.body = compress ? compress_data(body) : body
end
request.basic_auth(@username, @password) if @username
response = @http.request(request)
if response.code == '200' || response.code == '201'
true
else
# Failures are logged to the global semantic logger failsafe logger (Usually stderr or file)
- SemanticLogger::Processor.logger.error("Bad HTTP response from: #{url} code: #{response.code}, #{response.body}")
+ logger.error("Bad HTTP response from: #{url} code: #{response.code}, #{response.body}")
false
end
+ rescue RuntimeError => exc
+ reopen
+ raise exc
end
end