lib/semantic_logger/appender/http.rb in semantic_logger-4.11.0 vs lib/semantic_logger/appender/http.rb in semantic_logger-4.12.0
- old
+ new
@@ -19,11 +19,11 @@
module SemanticLogger
module Appender
class Http < SemanticLogger::Subscriber
attr_accessor :username, :compress, :header,
:open_timeout, :read_timeout, :continue_timeout
- attr_reader :http, :url, :server, :port, :path, :ssl_options
+ attr_reader :http, :url, :server, :port, :path, :ssl_options, :proxy_url
# Create HTTP(S) log appender
#
# Parameters:
# url: [String]
@@ -55,10 +55,20 @@
# ssl: [Hash]
# Specific SSL options: For more details see NET::HTTP.start
# ca_file, ca_path, cert, cert_store, ciphers, key, ssl_timeout,
# ssl_version, verify_callback, verify_depth and verify_mode.
#
+ # proxy_url: [String]
+ # URL of proxy server to use for HTTP(s) connections. Should
+ # include username and password if required.
+ # Example: http://user@pass:example.com/some_path
+ # To enable SSL include https in the URL.
+ # Example: https://example.com/some_path
+ # If this is set to :ENV, Net::HTTP will use the environment http_proxy*
+ # variables if they are set. If set to nil then no proxy will be used,
+ # even if the environment variables are set.
+ #
# level: [:trace | :debug | :info | :warn | :error | :fatal]
# Override the log level for this appender.
# Default: SemanticLogger.default_level
#
# formatter: [Object|Proc]
@@ -83,17 +93,19 @@
def initialize(url:,
compress: false,
ssl: {},
username: nil,
password: nil,
+ proxy_url: :ENV,
open_timeout: 2.0,
read_timeout: 1.0,
continue_timeout: 1.0,
**args,
&block)
@url = url
+ @proxy_url = proxy_url
@ssl_options = ssl
@username = username
@password = password
@compress = compress
@open_timeout = open_timeout
@@ -127,10 +139,13 @@
@ssl_options[:verify_mode] ||= OpenSSL::SSL::VERIFY_PEER
@port ||= HTTP.https_default_port
else
@port ||= HTTP.http_default_port
end
+
+ @proxy_uri = URI.parse(@proxy_url) if @proxy_url && @proxy_url != :ENV
+
@http = nil
super(**args, &block)
reopen
end
@@ -142,10 +157,14 @@
@http&.finish
rescue IOError
nil
end
- @http = Net::HTTP.new(server, port)
+ @http = if @proxy_uri
+ Net::HTTP.new(server, port, @proxy_uri.host, @proxy_uri.port, @proxy_uri.user, @proxy_uri.password)
+ else
+ Net::HTTP.new(server, port, @proxy_url)
+ end
if @ssl_options
@http.methods.grep(/\A(\w+)=\z/) do |meth|
key = Regexp.last_match(1).to_sym
@ssl_options.key?(key) || next