Class: LogStash::Outputs::Gcs::LogRotate

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/outputs/gcs/log_rotate.rb

Instance Method Summary collapse

Constructor Details

#initialize(path_factory, max_file_size_bytes, gzip, flush_interval_secs, gzip_encoded = false) ⇒ LogRotate

Returns a new instance of LogRotate.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/logstash/outputs/gcs/log_rotate.rb', line 9

def initialize(path_factory, max_file_size_bytes, gzip, flush_interval_secs, gzip_encoded=false)
  @path_factory = path_factory
  @max_file_size_bytes = max_file_size_bytes
  @gzip = gzip
  @flush_interval_secs = flush_interval_secs
  @gzip_encoded = gzip_encoded

  @lock = Concurrent::ReentrantReadWriteLock.new
  @rotate_callback = nil

  rotate_log!
end

Instance Method Details

#on_rotate(&block) ⇒ Object

on_rotate sets a handler to be called when the log gets rotated. The handler receives the path to the rotated out log as a string.



57
58
59
60
61
# File 'lib/logstash/outputs/gcs/log_rotate.rb', line 57

def on_rotate(&block)
  @lock.with_write_lock do
    @rotate_callback = block
  end
end

#rotate_log!Object

rotate_log! closes the current log (if it exists), notifies the handler, rolls the path over and opens a new log.

Invariant: the old log will ALWAYS be closed and a new one will ALWAYS be open at the completion of this function.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/logstash/outputs/gcs/log_rotate.rb', line 41

def rotate_log!
  @lock.with_write_lock do
    unless @temp_file.nil?
      @temp_file.close!
      @rotate_callback.call(@temp_file.path) unless @rotate_callback.nil?
    end

    @path_factory.rotate_path!

    path = @path_factory.current_path
    @temp_file = LogStash::Outputs::Gcs::LogFileFactory.create(path, @gzip, true, @gzip_encoded)
  end
end

#writeln(message = nil) ⇒ Object

writeln writes a message and carriage-return character to the open log file, rotating and syncing it if necessary.

nil messages do not get written, but may cause the log to rotate



26
27
28
29
30
31
32
33
34
# File 'lib/logstash/outputs/gcs/log_rotate.rb', line 26

def writeln(message=nil)
  @lock.with_write_lock do
    rotate_log! if should_rotate?

    @temp_file.write(message, "\n") unless message.nil?

    @temp_file.fsync if @temp_file.time_since_sync >= @flush_interval_secs
  end
end