Class: Cosmos::TextLogWriter

Inherits:
LogWriter show all
Defined in:
lib/cosmos/logs/text_log_writer.rb

Overview

Creates a text log. Can automatically cycle the log based on an elasped time period or when the log file reaches a predefined size.

Constant Summary

Constants inherited from LogWriter

LogWriter::CYCLE_TIME_INTERVAL

Instance Attribute Summary

Attributes inherited from LogWriter

#filename, #logging_enabled

Instance Method Summary collapse

Methods inherited from LogWriter

#close_file, #create_unique_filename, #cycle_thread_body, #first_time, #first_timestamp, #graceful_kill, #initialize, #last_time, #last_timestamp, #prepare_write, #shutdown, #start, #start_new_file, #stop

Constructor Details

This class inherits a constructor from Cosmos::LogWriter

Instance Method Details

#extensionObject



64
65
66
# File 'lib/cosmos/logs/text_log_writer.rb', line 64

def extension
  '.txt'.freeze
end

#s3_filenameObject



56
57
58
59
60
61
62
# File 'lib/cosmos/logs/text_log_writer.rb', line 56

def s3_filename
  # Put the name of the redis topic in the filename, but remove the scope
  # because we're already in a directory with the scope name
  split_index = @redis_topic.index("__") + 2
  topic_name = @redis_topic[split_index, @redis_topic.length - split_index]
  "#{first_timestamp}__#{last_timestamp}__#{topic_name}" + extension
end

#write(time_nsec_since_epoch, data, redis_offset) ⇒ Object

Write to the log file.

If no log file currently exists in the filesystem, a new file will be created.

Parameters:

  • time_nsec_since_epoch (Integer)

    64 bit integer nsecs since EPOCH

  • data (String)

    String of data

  • redis_offset (Integer)

    The offset of this packet in its Redis stream



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cosmos/logs/text_log_writer.rb', line 34

def write(time_nsec_since_epoch, data, redis_offset)
  return if !@logging_enabled

  @mutex.synchronize do
    prepare_write(time_nsec_since_epoch, data.length, redis_offset)
    write_entry(time_nsec_since_epoch, data) if @file
  end
rescue => err
  Logger.instance.error "Error writing #{@filename} : #{err.formatted}"
  Cosmos.handle_critical_exception(err)
end

#write_entry(time_nsec_since_epoch, data) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/cosmos/logs/text_log_writer.rb', line 46

def write_entry(time_nsec_since_epoch, data)
  @entry.clear
  @entry << "#{time_nsec_since_epoch}\t"
  @entry << "#{data}\n"
  @file.write(@entry)
  @file_size += @entry.length
  @first_time = time_nsec_since_epoch if !@first_time or time_nsec_since_epoch < @first_time
  @last_time = time_nsec_since_epoch if !@last_time or time_nsec_since_epoch > @last_time
end