lib/timber/log_devices/http.rb in timber-2.1.0.rc2 vs lib/timber/log_devices/http.rb in timber-2.1.0.rc3

- old
+ new

@@ -92,11 +92,11 @@ # the current process is forked. This is the case with various web servers, # such as phusion passenger. ensure_flush_threads_are_started if @msg_queue.full? - debug { "Flushing HTTP buffer via write" } + Timber::Config.instance.debug { "Flushing HTTP buffer via write" } flush_async end true end @@ -120,22 +120,10 @@ # Kill the request queue thread. Flushing ensures that no requests are pending. @request_outlet_thread.kill if @request_outlet_thread end private - def debug_logger - Timber::Config.instance.debug_logger - end - - # Convenience method for writing debug messages. - def debug(&block) - if debug_logger - message = yield - debug_logger.debug(message) - end - end - # This is a convenience method to ensure the flush thread are # started. This is called lazily from {#write} so that we # only start the threads as needed, but it also ensures # threads are started after process forking. def ensure_flush_threads_are_started @@ -170,25 +158,25 @@ # imposed limit. def flush_async @last_async_flush = Time.now req = build_request if !req.nil? - debug { "New request placed on queue" } + Timber::Config.instance.debug { "New request placed on queue" } @request_queue.enq(req) end end # Waits on the request queue. This is used in {#flush} to ensure # the log data has been delivered before returning. def wait_on_request_queue # Wait 20 seconds 40.times do |i| if @request_queue.size == 0 && @requests_in_flight == 0 - debug { "Request queue is empty and no requests are in flight, finish waiting" } + Timber::Config.instance.debug { "Request queue is empty and no requests are in flight, finish waiting" } return true end - debug do + Timber::Config.instance.debug do "Request size #{@request_queue.size}, reqs in-flight #{@requests_in_flight}, " \ "continue waiting (iteration #{i + 1})" end sleep 0.5 end @@ -202,17 +190,17 @@ sleep @flush_interval loop do begin if intervaled_flush_ready? - debug { "Flushing HTTP buffer via the interval" } + Timber::Config.instance.debug { "Flushing HTTP buffer via the interval" } flush_async end sleep(0.5) rescue Exception => e - debug { "Intervaled HTTP flush failed: #{e.inspect}\n\n#{e.backtrace}" } + Timber::Config.instance.debug { "Intervaled HTTP flush failed: #{e.inspect}\n\n#{e.backtrace}" } end end end # Determines if the loop in {#intervaled_flush} is ready to be flushed again. It @@ -223,11 +211,11 @@ end # Builds an `Net::HTTP` object to deliver requests over. def build_http http = Net::HTTP.new(@timber_url.host, @timber_url.port) - http.set_debug_output(debug_logger) if debug_logger + http.set_debug_output(Config.instance.debug_logger) if Config.instance.debug_logger http.use_ssl = true if @timber_url.scheme == 'https' http.read_timeout = 30 http.ssl_timeout = 10 http.open_timeout = 10 http @@ -237,19 +225,19 @@ def request_outlet loop do http = build_http begin - debug { "Starting HTTP connection" } + Timber::Config.instance.debug { "Starting HTTP connection" } http.start do |conn| deliver_requests(conn) end rescue => e - debug { "#request_outlet error: #{e.message}" } + Timber::Config.instance.debug { "#request_outlet error: #{e.message}" } ensure - debug { "Finishing HTTP connection" } + Timber::Config.instance.debug { "Finishing HTTP connection" } http.finish if http.started? end end end @@ -259,28 +247,28 @@ # and establish a new connection. def deliver_requests(conn) num_reqs = 0 while num_reqs < @requests_per_conn - debug { "Waiting on next request, threads waiting: #{@request_queue.num_waiting}" } + Timber::Config.instance.debug { "Waiting on next request, threads waiting: #{@request_queue.num_waiting}" } # Blocks waiting for a request. req = @request_queue.deq @requests_in_flight += 1 begin resp = conn.request(req) rescue => e - debug { "#deliver_request error: #{e.message}" } + Timber::Config.instance.debug { "#deliver_request error: #{e.message}" } @successive_error_count += 1 # Back off so that we don't hammer the Timber API. calculated_backoff = @successive_error_count * 2 backoff = calculated_backoff > 30 ? 30 : calculated_backoff - debug { "Backing off #{backoff} seconds, error ##{@successive_error_count}" } + Timber::Config.instance.debug { "Backing off #{backoff} seconds, error ##{@successive_error_count}" } sleep backoff # Throw the request back on the queue for a retry @request_queue.enq(req) @@ -289,10 +277,10 @@ @requests_in_flight -= 1 end @successive_error_count = 0 num_reqs += 1 - debug { "Request successful: #{resp.code}" } + Timber::Config.instance.debug { "Request successful: #{resp.code}" } end end # Builds the `Authorization` header value for HTTP delivery to the Timber API. def authorization_payload \ No newline at end of file