Module: TestaLogger::Logger::Persistence

Included in:
TestaLogger::Logger
Defined in:
lib/testa_logger/logger/persistence.rb

Instance Method Summary collapse

Instance Method Details

#init_s3_clientObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/testa_logger/logger/persistence.rb', line 4

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

#persistObject



32
33
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
# File 'lib/testa_logger/logger/persistence.rb', line 32

def persist
  return if @s3.nil?

  begin
    write_thread["stop"] = true

    key = "logs/#{app}/#{group}"
    key += "/#{subgroup}" unless subgroup.nil?
    key += "/#{Time.now.strftime('%d.%m.%Y')}"

    extension = File.extname(options.filepath)
    filename = File.basename(options.filepath, extension)
    time_string = Time.now.strftime("%H_%M_%S")
    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_credentials[: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:



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

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