Class: HttpLoader::Client::Logger

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/http_loader/client/logger.rb

Overview

Handles asynchronous file logging to prevent blocking main connections.

Instance Method Summary collapse

Constructor Details

#initialize(verbose) ⇒ Logger

Returns a new instance of Logger.



15
16
17
18
19
20
# File 'lib/http_loader/client/logger.rb', line 15

def initialize(verbose)
  @verbose = verbose
  @log_dir = T.let(File.expand_path('../../../logs', __dir__), String)
  @log_queue = T.let(Queue.new, Queue)
  @logger_task = T.let(nil, T.nilable(T.untyped))
end

Instance Method Details

#error(message) ⇒ Object



59
60
61
# File 'lib/http_loader/client/logger.rb', line 59

def error(message)
  @log_queue << [:error, "[#{Time.now.utc.iso8601}] #{message}"]
end

#flush_synchronously!Object



41
42
43
44
45
46
47
48
49
# File 'lib/http_loader/client/logger.rb', line 41

def flush_synchronously!
  File.open(File.join(@log_dir, 'client.log'), 'a') do |log|
    File.open(File.join(@log_dir, 'client.err'), 'a') do |err|
      drain_queue(log, err)
    end
  end
rescue StandardError
  nil
end

#info(message) ⇒ Object



52
53
54
55
56
# File 'lib/http_loader/client/logger.rb', line 52

def info(message)
  return unless @verbose

  @log_queue << [:info, "[#{Time.now.utc.iso8601}] #{message}"]
end

#run_task(task) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/http_loader/client/logger.rb', line 30

def run_task(task)
  @logger_task = task.async do
    File.open(File.join(@log_dir, 'client.log'), 'a') do |log|
      File.open(File.join(@log_dir, 'client.err'), 'a') do |err|
        poll_queue(task, log, err)
      end
    end
  end
end

#setup_files!Object



23
24
25
26
27
# File 'lib/http_loader/client/logger.rb', line 23

def setup_files!
  FileUtils.mkdir_p(@log_dir)
  File.write(File.join(@log_dir, 'client.err'), '')
  File.write(File.join(@log_dir, 'client.log'), '') if @verbose
end