Class: LogStashLogger::Device::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash-logger/device/base.rb

Direct Known Subclasses

Balancer, Connectable, File, IO, MultiDelegator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base


8
9
10
11
# File 'lib/logstash-logger/device/base.rb', line 8

def initialize(opts={})
  @sync = opts[:sync]
  @error_logger = opts.fetch(:error_logger, LogStashLogger.configuration.default_error_logger)
end

Instance Attribute Details

#error_loggerObject

Returns the value of attribute error_logger


6
7
8
# File 'lib/logstash-logger/device/base.rb', line 6

def error_logger
  @error_logger
end

#ioObject (readonly)

Returns the value of attribute io


4
5
6
# File 'lib/logstash-logger/device/base.rb', line 4

def io
  @io
end

#syncObject

Returns the value of attribute sync


5
6
7
# File 'lib/logstash-logger/device/base.rb', line 5

def sync
  @sync
end

Instance Method Details

#close(opts = {}) ⇒ Object


42
43
44
45
46
# File 'lib/logstash-logger/device/base.rb', line 42

def close(opts = {})
  close!
rescue => e
  log_error(e)
end

#close!Object


48
49
50
51
52
# File 'lib/logstash-logger/device/base.rb', line 48

def close!
  @io && @io.close
ensure
  @io = nil
end

#flushObject


38
39
40
# File 'lib/logstash-logger/device/base.rb', line 38

def flush
  @io && @io.flush
end

#to_ioObject


13
14
15
# File 'lib/logstash-logger/device/base.rb', line 13

def to_io
  @io
end

#unrecoverable_error?(e) ⇒ Boolean


54
55
56
# File 'lib/logstash-logger/device/base.rb', line 54

def unrecoverable_error?(e)
  e.is_a?(JSON::GeneratorError)
end

#write(message) ⇒ Object


17
18
19
# File 'lib/logstash-logger/device/base.rb', line 17

def write(message)
  write_one(message)
end

#write_batch(messages, group = nil) ⇒ Object


32
33
34
35
36
# File 'lib/logstash-logger/device/base.rb', line 32

def write_batch(messages, group = nil)
  messages.each do |message|
    write_one(message)
  end
end

#write_one(message) ⇒ Object


21
22
23
24
25
26
27
28
29
30
# File 'lib/logstash-logger/device/base.rb', line 21

def write_one(message)
  @io.write(message)
rescue => e
  if unrecoverable_error?(e)
    log_error(e)
    log_warning("unrecoverable error, aborting write")
  else
    raise
  end
end