Class: BBFS::Log::FileConsumer

Inherits:
Consumer
  • Object
show all
Defined in:
lib/log/log_consumer.rb

Overview

The file consumer logs the data to a file

Instance Method Summary collapse

Methods inherited from Consumer

#push_data

Constructor Details

#initialize(file_name) ⇒ FileConsumer

Returns a new instance of FileConsumer.



86
87
88
89
90
91
92
# File 'lib/log/log_consumer.rb', line 86

def initialize file_name
  super()
  @file_name = file_name
  if File.exist? @file_name then
    File.delete @file_name
  end
end

Instance Method Details

#consume(data) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/log/log_consumer.rb', line 94

def consume data
  begin
    file_handler = File.new @file_name, 'a'
    file_handler.puts data
  rescue
    Exception
    puts "Failed to open log file:#{@file_name}"
    puts $!.class
    puts $!
  ensure
    file_handler.close
  end
end