Module: TestaLogger::Logger::Persistence

Defined in:
lib/testa_logger/logger/persistence.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



4
5
6
7
# File 'lib/testa_logger/logger/persistence.rb', line 4

def self.extended(base)
  require "aws-sdk-s3"
  base.init_s3_client
end

Instance Method Details

#init_s3_clientObject



9
10
11
12
13
14
15
16
# File 'lib/testa_logger/logger/persistence.rb', line 9

def init_s3_client
  Aws.config.update(
    region: options.s3_creds[:region],
    credentials: Aws::Credentials.new(options.s3_creds[:access_key_id], options.s3_creds[:secret_access_key])
  )
  @s3 = Aws::S3::Client.new
  at_exit { persist rescue false }
end

#persistObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/testa_logger/logger/persistence.rb', line 34

def persist
  return if @s3.nil?

  begin
    write_thread["stop"] = true

    key = "logs/#{app}/#{group}"
    key += "/#{subgroup}" unless subgroup.nil?

    extension = File.extname(options.filepath)
    filename = File.basename(options.filepath, extension)
    time_string = Time.now.strftime("%H_%M_%S__%d_%m_%Y")
    key += "/#{filename}_#{time_string}#{extension}"

    # stop writing into log file with it is being attached, otherwise checksum integrity will fail.
    response = @s3.put_object(
      bucket: options.s3_creds[:bucket_name],
      key: key,
      body: IO.read(options.filepath)
    )
    error(TAG, "Failed to persist log file #{options.filepath}. Response: #{response.body}") unless response.etag
  rescue StandardError => e
    error(TAG, e)
    raise
  ensure
    write_thread["stop"] = false
    write_thread.run
  end
end

#persist_with_record(record, attachment_name) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/testa_logger/logger/persistence.rb', line 18

def persist_with_record(record, attachment_name)
  raise IoPersistenceError if @log_device.is_a?(IO)

  begin
    # stop writing into log file with it is being attached, otherwise checksum integrity will fail.
    write_thread["stop"] = true
    record.send(attachment_name).attach(io: File.open(options.filepath), filename: "#{attachment_name}.log")
  rescue StandardError => e
    error(TAG, e)
    raise
  ensure
    write_thread["stop"] = false
    write_thread.run
  end
end