Class: Google::Cloud::Logging::AsyncWriter
- Inherits:
-
Object
- Object
- Google::Cloud::Logging::AsyncWriter
- Includes:
- Stackdriver::Core::AsyncActor
- Defined in:
- lib/google/cloud/logging/async_writer.rb
Overview
AsyncWriter
An object that batches and transmits log entries asynchronously.
Use this object to transmit log entries efficiently. It keeps a queue of log entries, and runs a background thread that transmits them to the logging service in batches. Generally, adding to the queue will not block.
This object is thread-safe; it may accept write requests from multiple threads simultaneously, and will serialize them when executing in the background thread.
Constant Summary collapse
- DEFAULT_MAX_QUEUE_SIZE =
10000
- CLEANUP_TIMEOUT =
Stackdriver::Core::AsyncActor::CLEANUP_TIMEOUT
- WAIT_INTERVAL =
Stackdriver::Core::AsyncActor::WAIT_INTERVAL
Instance Attribute Summary collapse
-
#last_exception ⇒ Object
readonly
The last exception thrown by the background thread, or nil if nothing has been thrown.
Instance Method Summary collapse
-
#logger(log_name, resource, labels = {}) ⇒ Google::Cloud::Logging::Logger
Creates a logger instance that is API-compatible with Ruby's standard library Logger.
-
#stop!(timeout, force: false) ⇒ Symbol
Stop this asynchronous writer and block until it has been stopped.
-
#write_entries(entries, log_name: nil, resource: nil, labels: nil) ⇒ Google::Cloud::Logging::AsyncWriter
Asynchronously write one or more log entries to the Stackdriver Logging service.
Instance Attribute Details
#last_exception ⇒ Object (readonly)
The last exception thrown by the background thread, or nil if nothing has been thrown.
95 96 97 |
# File 'lib/google/cloud/logging/async_writer.rb', line 95 def last_exception @last_exception end |
Instance Method Details
#logger(log_name, resource, labels = {}) ⇒ Google::Cloud::Logging::Logger
Creates a logger instance that is API-compatible with Ruby's standard library Logger.
The logger will use AsyncWriter to transmit log entries on a background thread.
199 200 201 |
# File 'lib/google/cloud/logging/async_writer.rb', line 199 def logger log_name, resource, labels = {} Logger.new self, log_name, resource, labels end |
#stop!(timeout, force: false) ⇒ Symbol
Stop this asynchronous writer and block until it has been stopped.
DEPRECATED. Use #async_stop! instead.
311 312 313 314 315 316 |
# File 'lib/google/cloud/logging/async_writer.rb', line 311 def stop! timeout, force: false @cleanup_options[:timeout] = timeout unless timeout.nil? @cleanup_options[:force] = force unless force.nil? async_stop! end |
#write_entries(entries, log_name: nil, resource: nil, labels: nil) ⇒ Google::Cloud::Logging::AsyncWriter
Asynchronously write one or more log entries to the Stackdriver Logging service.
Unlike the main write_entries method, this method usually does not block. The actual write RPCs will happen in the background, and may be batched with related calls. However, if the queue is full, this method will block until enough space has cleared out.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/google/cloud/logging/async_writer.rb', line 151 def write_entries entries, log_name: nil, resource: nil, labels: nil ensure_thread entries = Array(entries) synchronize do raise "AsyncWriter has been stopped" unless writable? queue_item = QueueItem.new entries, log_name, resource, labels if @queue.empty? || !@queue.last.try_combine(queue_item) @queue.push queue_item end @queue_size += entries.size @queue_resource.broadcast while @max_queue_size && @queue_size > @max_queue_size @queue_resource.wait end end self end |