Class: Onlylogs::MultiDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/onlylogs/multi_device.rb

Overview

A Logger log device that fans each line out to several underlying devices, e.g. the local $stdout fallback plus a remote sink (Onlylogs::SocketDevice / Onlylogs::HttpDevice).

Instance Method Summary collapse

Constructor Details

#initialize(*devices) ⇒ MultiDevice

Returns a new instance of MultiDevice.



7
8
9
# File 'lib/onlylogs/multi_device.rb', line 7

def initialize(*devices)
  @devices = devices.compact
end

Instance Method Details

#closeObject



17
18
19
20
21
22
23
24
# File 'lib/onlylogs/multi_device.rb', line 17

def close
  @devices.each do |device|
    # Never close the process' standard streams — the app owns them, not us.
    next if device.equal?($stdout) || device.equal?($stderr)

    device.close if device.respond_to?(:close)
  end
end

#flushObject



26
27
28
# File 'lib/onlylogs/multi_device.rb', line 26

def flush
  @devices.each { |device| device.flush if device.respond_to?(:flush) }
end

#write(message) ⇒ Object



11
12
13
14
15
# File 'lib/onlylogs/multi_device.rb', line 11

def write(message)
  return if message.nil? || message.empty?

  @devices.each { |device| device.write(message) }
end