Class: Lumberjack::Device::Multi

Inherits:
Lumberjack::Device show all
Defined in:
lib/lumberjack/device/multi.rb

Overview

A multiplexing logging device that broadcasts log entries to multiple target devices simultaneously. This device enables sophisticated logging architectures where a single log entry needs to be processed by multiple output destinations, each potentially with different formatting, filtering, or storage mechanisms.

The Multi device acts as a fan-out mechanism, ensuring that all configured devices receive every log entry while maintaining independent processing pipelines. This is particularly useful for creating redundant logging systems, separating log streams by concern, or implementing complex routing logic.

All device lifecycle methods (flush, close, reopen) are propagated to all child devices, ensuring consistent state management across the entire logging topology.

Examples:

Basic multi-device setup

file_device = Lumberjack::Device::Writer.new("/var/log/app.log")
console_device = Lumberjack::Device::Writer.new(STDOUT, template: "{{message}}")
multi_device = Lumberjack::Device::Multi.new(file_device, console_device)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Lumberjack::Device

#dev, open_device

Constructor Details

#initialize(*devices) ⇒ Multi

Initialize a new Multi device with the specified target devices. The device accepts multiple devices either as individual arguments or as arrays, automatically flattening nested arrays for convenient configuration.

Examples:

Individual device arguments

multi = Multi.new(file_device, console_device, database_device)

Parameters:

  • devices (Array<Lumberjack::Device>)

    The target devices to receive log entries. Can be passed as individual arguments or arrays. All devices must implement the standard Lumberjack::Device interface.



35
36
37
# File 'lib/lumberjack/device/multi.rb', line 35

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

Instance Attribute Details

#devicesObject (readonly)

Returns the value of attribute devices.



23
24
25
# File 'lib/lumberjack/device/multi.rb', line 23

def devices
  @devices
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close all configured devices and release their resources. This method calls close on each device in sequence, ensuring proper cleanup of file handles, network connections, and other resources across all output destinations.



68
69
70
71
72
# File 'lib/lumberjack/device/multi.rb', line 68

def close
  devices.each do |device|
    device.close
  end
end

#datetime_formatString?

Get the datetime format from the first device that has one configured. This method searches through the configured devices and returns the datetime format from the first device that provides one.

Returns:

  • (String, nil)

    The datetime format string from the first device that has one configured, or nil if no devices have a format set



93
94
95
# File 'lib/lumberjack/device/multi.rb', line 93

def datetime_format
  devices.detect(&:datetime_format).datetime_format
end

#datetime_format=(format) ⇒ void

This method returns an undefined value.

Set the datetime format on all configured devices that support it. This method propagates the format setting to each device, allowing coordinated timestamp formatting across all output destinations.

Parameters:

  • format (String)

    The datetime format string to apply to all devices



103
104
105
106
107
# File 'lib/lumberjack/device/multi.rb', line 103

def datetime_format=(format)
  devices.each do |device|
    device.datetime_format = format
  end
end

#flushvoid

This method returns an undefined value.

Flush all configured devices to ensure buffered data is written to their respective destinations. This method calls flush on each device in sequence, ensuring consistent state across all output destinations.



57
58
59
60
61
# File 'lib/lumberjack/device/multi.rb', line 57

def flush
  devices.each do |device|
    device.flush
  end
end

#reopen(logdev = nil) ⇒ void

This method returns an undefined value.

Reopen all configured devices, optionally with a new log destination. This method calls reopen on each device in sequence, which is typically used for log rotation scenarios or when changing output destinations.

Parameters:

  • logdev (Object, nil) (defaults to: nil)

    Optional new log device or destination to pass to each device’s reopen method



81
82
83
84
85
# File 'lib/lumberjack/device/multi.rb', line 81

def reopen(logdev = nil)
  devices.each do |device|
    device.reopen(logdev = nil)
  end
end

#write(entry) ⇒ void

This method returns an undefined value.

Broadcast a log entry to all configured devices. Each device receives the same LogEntry object and processes it according to its own configuration, formatting, and output logic. Devices are processed sequentially in the order they were configured.

Parameters:



46
47
48
49
50
# File 'lib/lumberjack/device/multi.rb', line 46

def write(entry)
  devices.each do |device|
    device.write(entry)
  end
end