Class: Vigilant::Logger
- Inherits:
-
Object
- Object
- Vigilant::Logger
- 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
- #autocapture_disable ⇒ Object
- #autocapture_enable ⇒ Object
- #debug(body, attributes = {}) ⇒ Object
- #error(body, error = nil, attributes = {}) ⇒ Object
- #info(body, attributes = {}) ⇒ Object
-
#initialize(name:, token:, endpoint:, insecure: false, passthrough: false) ⇒ Logger
constructor
Initialize a Vigilant::Logger instance.
- #shutdown ⇒ Object
- #warn(body, attributes = {}) ⇒ Object
Constructor Details
#initialize(name:, token:, endpoint:, insecure: false, passthrough: false) ⇒ Logger
Initialize a Vigilant::Logger instance.
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_disable ⇒ Object
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_enable ⇒ Object
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., **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 |
#shutdown ⇒ Object
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 |