Class: Vigilant::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/vigilant-ruby/logger.rb

Overview

A thread-safe logger that batches logs and sends them to Vigilant asynchronously

Instance Method Summary collapse

Constructor Details

#initialize(name:, token:, endpoint:, insecure: false, passthrough: false) ⇒ Logger

Initialize a Vigilant::Logger instance.

Parameters:

  • name (String)

    The name of the application.

  • endpoint (String)

    The base endpoint for the Vigilant API (e.g. “ingress.vigilant.run”).

  • token (String)

    The authentication token for the Vigilant API.

  • insecure (Boolean) (defaults to: false)

    Whether to use HTTP instead of HTTPS (optional, defaults to false).

  • passthrough (Boolean) (defaults to: false)

    Whether to also print logs to stdout/stderr (optional, defaults to false).



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vigilant-ruby/logger.rb', line 26

def initialize(name:, token:, endpoint:, insecure: false, passthrough: false)
  @name = name
  @token = token

  protocol = insecure ? 'http://' : 'https://'
  endpoint = endpoint.sub(%r{^https?://}, '') # remove any existing protocol
  @endpoint = URI.parse("#{protocol}#{endpoint}/api/message")

  @insecure = insecure
  @passthrough = passthrough

  @batch_size     = DEFAULT_BATCH_SIZE
  @flush_interval = DEFAULT_FLUSH_INTERVAL

  @queue = Queue.new
  @mutex = Mutex.new
  @batch = []

  @original_stdout = $stdout
  @original_stderr = $stderr

  @autocapture_enabled = false

  start_dispatcher
end

Instance Method Details

#autocapture_disableObject



81
82
83
84
85
86
87
# File 'lib/vigilant-ruby/logger.rb', line 81

def autocapture_disable
  return unless @autocapture_enabled

  @autocapture_enabled = false
  $stdout = @original_stdout
  $stderr = @original_stderr
end

#autocapture_enableObject



73
74
75
76
77
78
79
# File 'lib/vigilant-ruby/logger.rb', line 73

def autocapture_enable
  return if @autocapture_enabled

  @autocapture_enabled = true
  $stdout = StdoutInterceptor.new(self, @original_stdout)
  $stderr = StderrInterceptor.new(self, @original_stderr)
end

#debug(body, attributes = {}) ⇒ Object



52
53
54
# File 'lib/vigilant-ruby/logger.rb', line 52

def debug(body, attributes = {})
  enqueue_log(DEBUG, body, attributes)
end

#error(body, error = nil, attributes = {}) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/vigilant-ruby/logger.rb', line 64

def error(body, error = nil, attributes = {})
  if error.nil?
    enqueue_log(ERROR, body, attributes)
  else
    attributes_with_error = { error: error.message, **attributes }
    enqueue_log(ERROR, body, attributes_with_error)
  end
end

#info(body, attributes = {}) ⇒ Object



56
57
58
# File 'lib/vigilant-ruby/logger.rb', line 56

def info(body, attributes = {})
  enqueue_log(INFO, body, attributes)
end

#shutdownObject



89
90
91
92
93
# File 'lib/vigilant-ruby/logger.rb', line 89

def shutdown
  flush_if_needed(force: true)
  @mutex.synchronize { @shutdown = true }
  @dispatcher_thread&.join
end

#warn(body, attributes = {}) ⇒ Object



60
61
62
# File 'lib/vigilant-ruby/logger.rb', line 60

def warn(body, attributes = {})
  enqueue_log(WARNING, body, attributes)
end