Class: Onlylogs::HttpLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/onlylogs/http_logger.rb

Constant Summary collapse

DEFAULT_BATCH_SIZE =
100
DEFAULT_FLUSH_INTERVAL =
0.5

Instance Method Summary collapse

Constructor Details

#initialize(local_fallback: $stdout, drain_url: ENV["ONLYLOGS_DRAIN_URL"], batch_size: ENV.fetch("ONLYLOGS_BATCH_SIZE", DEFAULT_BATCH_SIZE).to_i, flush_interval: ENV.fetch("ONLYLOGS_FLUSH_INTERVAL", DEFAULT_FLUSH_INTERVAL).to_f) ⇒ HttpLogger

Returns a new instance of HttpLogger.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/onlylogs/http_logger.rb', line 15

def initialize(
  local_fallback: $stdout,
  drain_url: ENV["ONLYLOGS_DRAIN_URL"],
  batch_size: ENV.fetch("ONLYLOGS_BATCH_SIZE", DEFAULT_BATCH_SIZE).to_i,
  flush_interval: ENV.fetch("ONLYLOGS_FLUSH_INTERVAL", DEFAULT_FLUSH_INTERVAL).to_f
)
  super(local_fallback)
  @drain_url = drain_url
  @batch_size = batch_size
  @flush_interval = flush_interval
  @queue = Queue.new
  @mutex = Mutex.new

  if @drain_url
    start_sender
  else
    $stderr.puts "Onlylogs::HttpLogger error: ONLYLOGS_DRAIN_URL is not set; logger is disabled." # rubocop:disable Style/StderrPuts
  end
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/onlylogs/http_logger.rb', line 35

def add(severity, message = nil, progname = nil, &block)
  return true unless @drain_url

  if message.nil?
    if block_given?
      message = block.call
    else
      message = progname
      progname = nil
    end
  end

  formatted = format_message(format_severity(severity), Time.now, progname, message.to_s)
  @queue << formatted.chomp if formatted && @drain_url
  super
end

#closeObject



52
53
54
55
56
# File 'lib/onlylogs/http_logger.rb', line 52

def close
  flush
  @running = false
  @sender_thread&.join(2)
end

#flushObject



58
59
60
61
# File 'lib/onlylogs/http_logger.rb', line 58

def flush
  send_batch(drain_queue)
  super
end