Class: LogStashLogger::Device::File

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

Instance Attribute Summary

Attributes inherited from Base

#error_logger, #io, #sync

Instance Method Summary collapse

Methods inherited from Base

#close, #close!, #flush, #reset, #unrecoverable_error?, #write, #write_batch, #write_one

Constructor Details

#initialize(opts) ⇒ File

Returns a new instance of File.



5
6
7
8
9
10
11
12
13
# File 'lib/logstash-logger/device/file.rb', line 5

def initialize(opts)
  super
  @path = opts[:path] || fail(ArgumentError, "Path is required")
  @shift_age = opts[:shift_age]
  @shift_size = opts[:shift_size]
  @shift_period_suffix = opts[:shift_period_suffix]
  @use_log_device = opts.key?(:shift_age) || opts.key?(:shift_size) || opts.key?(:shift_period_suffix)
  open
end

Instance Method Details

#openObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/logstash-logger/device/file.rb', line 15

def open
  unless ::File.exist? ::File.dirname @path
    ::FileUtils.mkdir_p ::File.dirname @path
  end

  if @use_log_device
    require 'logger'
    log_device_options = { binmode: true }
    log_device_options[:shift_age] = @shift_age unless @shift_age.nil?
    log_device_options[:shift_size] = @shift_size unless @shift_size.nil?
    log_device_options[:shift_period_suffix] = @shift_period_suffix unless @shift_period_suffix.nil?
    @io = ::Logger::LogDevice.new(@path, **log_device_options)
    @io.dev.sync = self.sync unless self.sync.nil?
  else
    @io = ::File.open @path, ::File::WRONLY | ::File::APPEND | ::File::CREAT
    @io.binmode
    @io.sync = self.sync unless self.sync.nil?
  end
end

#to_ioObject



35
36
37
# File 'lib/logstash-logger/device/file.rb', line 35

def to_io
  @io.respond_to?(:dev) ? @io.dev : @io
end