lib/timber/log_devices/http.rb in timber-1.1.14 vs lib/timber/log_devices/http.rb in timber-2.0.0
- old
+ new
@@ -78,12 +78,10 @@
# @param [Hash] options the options to create a HTTP log device with.
# @option attributes [Symbol] :batch_size (1000) Determines the maximum of log lines in
# each HTTP payload. If the queue exceeds this limit an HTTP request will be issued. Bigger
# payloads mean higher throughput, but also use more memory. Timber will not accept
# payloads larger than 1mb.
- # @option attributes [Symbol] :debug (false) Whether to print debug output or not. This is also
- # inferred from ENV['debug']. Output will be sent to `Timber::Config.logger`.
# @option attributes [Symbol] :flush_interval (1) How often the client should
# attempt to deliver logs to the Timber API in fractional seconds. The HTTP client buffers
# logs and this options represents how often that will happen, assuming `:batch_byte_size`
# is not met.
# @option attributes [Symbol] :requests_per_conn (2500) The number of requests to send over a
@@ -103,12 +101,11 @@
# @example Dropping messages instead of applying back pressure
# http_log_device = Timber::LogDevices::HTTP.new("my_timber_api_key",
# request_queue: Timber::LogDevices::HTTP::DroppingSizedQueue.new(3))
# Timber::Logger.new(http_log_device)
def initialize(api_key, options = {})
- @api_key = api_key
- @debug = options[:debug] || ENV['debug']
+ @api_key = api_key || raise(ArgumentError.new("The api_key parameter cannot be blank"))
@timber_url = URI.parse(options[:timber_url] || ENV['TIMBER_URL'] || TIMBER_URL)
@batch_size = options[:batch_size] || 1_000
@flush_interval = options[:flush_interval] || 1 # 1 second
@requests_per_conn = options[:requests_per_conn] || 2_500
@msg_queue = LogMsgQueue.new(@batch_size)
@@ -124,11 +121,11 @@
# Write a new log line message to the buffer, and deliver if the msg exceeds the
# payload limit.
def write(msg)
@msg_queue.enqueue(msg)
if @msg_queue.full?
- logger.debug("Flushing timber buffer via write") if debug?
+ debug_logger.debug("Flushing timber buffer via write") if debug_logger
flush
end
true
end
@@ -138,12 +135,12 @@
@outlet_thread.kill if @outlet_thread
flush
end
private
- def debug?
- !@debug.nil?
+ def debug_logger
+ Timber::Config.instance.debug_logger
end
def flush
@last_flush = Time.now
msgs = @msg_queue.flush
@@ -161,11 +158,11 @@
# Wait specified time period before starting
sleep @flush_interval
loop do
begin
if intervaled_flush_ready?
- logger.debug("Flushing timber buffer via the interval") if debug?
+ debug_logger.debug("Flushing timber buffer via the interval") if debug_logger
flush
end
sleep(0.1)
rescue Exception => e
logger.error("Timber intervaled flush failed: #{e.inspect}\n\n#{e.backtrace}")
@@ -178,56 +175,52 @@
end
def outlet
loop do
http = Net::HTTP.new(@timber_url.host, @timber_url.port)
- http.set_debug_output(logger) if debug?
+ http.set_debug_output(debug_logger) if debug_logger
http.use_ssl = true if @timber_url.scheme == 'https'
http.read_timeout = 30
http.ssl_timeout = 10
http.open_timeout = 10
begin
- logger.info("Starting Timber HTTP connection") if debug?
+ debug_logger.info("Starting Timber HTTP connection") if debug_logger
http.start do |conn|
num_reqs = 0
while num_reqs < @requests_per_conn
- if debug?
- logger.debug("Waiting on next Timber request")
- logger.debug("Number of threads waiting on Timber request queue: #{@request_queue.num_waiting}")
+ if debug_logger
+ debug_logger.debug("Waiting on next Timber request")
+ debug_logger.debug("Number of threads waiting on Timber request queue: #{@request_queue.num_waiting}")
end
# Blocks waiting for a request.
req = @request_queue.deq
@requests_in_flight += 1
resp = nil
begin
resp = conn.request(req)
rescue => e
- logger.error("Timber request error: #{e.message}") if debug?
+ debug_logger.error("Timber request error: #{e.message}") if debug_logger
next
ensure
@requests_in_flight -= 1
end
num_reqs += 1
- logger.debug("Timber request successful: #{resp.code}") if debug?
+ debug_logger.debug("Timber request successful: #{resp.code}") if debug_logger
end
end
rescue => e
- logger.error("Timber request error: #{e.message}") if debug?
+ debug_logger.error("Timber request error: #{e.message}") if debug_logger
ensure
- logger.debug("Finishing Timber HTTP connection") if debug?
+ debug_logger.debug("Finishing Timber HTTP connection") if debug_logger
http.finish if http.started?
end
end
end
def authorization_payload
@authorization_payload ||= "Basic #{Base64.urlsafe_encode64(@api_key).chomp}"
- end
-
- def logger
- Config.instance.logger
end
end
end
end
\ No newline at end of file