Class: LogStashLogger::Device::Connectable
- Inherits:
-
Base
- Object
- Base
- LogStashLogger::Device::Connectable
show all
- Includes:
- Buffer
- Defined in:
- lib/logstash-logger/device/connectable.rb
Instance Attribute Summary
Attributes inherited from Base
#error_logger, #io, #sync
Instance Method Summary
collapse
Methods included from Buffer
#buffer_flush, #buffer_full?, #buffer_initialize, #buffer_receive, #reset_buffer
Methods inherited from Base
#close!, #unrecoverable_error?
Constructor Details
#initialize(opts = {}) ⇒ Connectable
Returns a new instance of Connectable.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/logstash-logger/device/connectable.rb', line 8
def initialize(opts = {})
super
if opts[:batch_events]
warn "The :batch_events option is deprecated. Please use :buffer_max_items instead"
end
if opts[:batch_timeout]
warn "The :batch_timeout option is deprecated. Please use :buffer_max_interval instead"
end
@buffer_group = nil
@buffer_max_items = opts[:batch_events] || opts[:buffer_max_items]
@buffer_max_interval = opts[:batch_timeout] || opts[:buffer_max_interval]
@drop_messages_on_flush_error =
if opts.key?(:drop_messages_on_flush_error)
opts.delete(:drop_messages_on_flush_error)
else
false
end
@drop_messages_on_full_buffer =
if opts.key?(:drop_messages_on_full_buffer)
opts.delete(:drop_messages_on_full_buffer)
else
true
end
buffer_initialize(
max_items: @buffer_max_items,
max_interval: @buffer_max_interval,
autoflush: @sync,
drop_messages_on_flush_error: @drop_messages_on_flush_error,
drop_messages_on_full_buffer: @drop_messages_on_full_buffer
)
end
|
Instance Method Details
#close(opts = {}) ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/logstash-logger/device/connectable.rb', line 62
def close(opts = {})
if opts.fetch(:flush, true)
buffer_flush(final: true)
end
super
end
|
#connect ⇒ Object
Implemented by subclasses
93
94
95
|
# File 'lib/logstash-logger/device/connectable.rb', line 93
def connect
fail NotImplementedError
end
|
#connected? ⇒ Boolean
76
77
78
|
# File 'lib/logstash-logger/device/connectable.rb', line 76
def connected?
!!@io
end
|
#flush(*args) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/logstash-logger/device/connectable.rb', line 49
def flush(*args)
if args.empty?
buffer_flush
else
messages, group = *args
write_batch(messages, group)
end
end
|
#on_full_buffer_receive(data) ⇒ Object
58
59
60
|
# File 'lib/logstash-logger/device/connectable.rb', line 58
def on_full_buffer_receive(data)
log_warning("Buffer Full - #{data}")
end
|
#reconnect ⇒ Object
97
98
99
100
|
# File 'lib/logstash-logger/device/connectable.rb', line 97
def reconnect
close(flush: false)
connect
end
|
#to_io ⇒ Object
70
71
72
73
74
|
# File 'lib/logstash-logger/device/connectable.rb', line 70
def to_io
with_connection do
super
end
end
|
#with_connection(&block) ⇒ Object
Ensure the block is executed with a valid connection
103
104
105
106
107
108
109
110
|
# File 'lib/logstash-logger/device/connectable.rb', line 103
def with_connection(&block)
connect unless connected?
yield
rescue => e
log_error(e)
close(flush: false)
raise
end
|
#write(message) ⇒ Object
45
46
47
|
# File 'lib/logstash-logger/device/connectable.rb', line 45
def write(message)
buffer_receive message, @buffer_group
end
|
#write_batch(messages, group = nil) ⇒ Object
86
87
88
89
90
|
# File 'lib/logstash-logger/device/connectable.rb', line 86
def write_batch(messages, group = nil)
with_connection do
super
end
end
|
#write_one(message) ⇒ Object
80
81
82
83
84
|
# File 'lib/logstash-logger/device/connectable.rb', line 80
def write_one(message)
with_connection do
super
end
end
|