Class: Logplex::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/logplex/publisher.rb

Instance Method Summary collapse

Constructor Details

#initialize(logplex_url = nil, bearer_token: nil) ⇒ Publisher

Returns a new instance of Publisher.



11
12
13
14
15
# File 'lib/logplex/publisher.rb', line 11

def initialize(logplex_url = nil, bearer_token: nil)
  @logplex_url = logplex_url || Logplex.configuration.logplex_url
  @token = URI(@logplex_url).password || Logplex.configuration.app_name
  @auth_headers = bearer_token ? { "Authorization" => "Bearer #{bearer_token}" } : {}
end

Instance Method Details

#publish(messages, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/logplex/publisher.rb', line 17

def publish(messages, opts = {})
  message_list = messages.dup
  unless messages.is_a? Array
    message_list = [message_list]
  end
  message_list.map! { |m| Message.new(m, { app_name: @token }.merge(opts)) }
  message_list.each(&:validate)
  if message_list.inject(true) { |accum, m| m.valid? }
    Timeout.timeout(Logplex.configuration.publish_timeout) do
      api_post(message_list.map(&:syslog_frame).join(""), message_list.length)
      true
    end
  end
rescue Errno::ECONNRESET => e
  raise Logplex::HTTP::ConnectionResetError, e.message
rescue ::SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
  raise Logplex::HTTP::SocketError, e.message
rescue Timeout::Error => e
  raise Logplex::HTTP::TimeoutError, e.message
end