Class: Logging::Appenders::LogentriesLogging

Inherits:
Logging::Appender
  • Object
show all
Defined in:
lib/logging/appenders/logentries_logging.rb

Overview

Provides an appender that can send log messages to loggly

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ LogentriesLogging

Returns a new instance of LogentriesLogging.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
# File 'lib/logging/appenders/logentries_logging.rb', line 19

def initialize( name, opts = {} )
  opts[:header] = false
  super(name, opts)
  # customer token for logentries
  self.token = opts.fetch(:token)
  raise ArgumentError, 'Must specify token' if @token.nil?
  self.logentries = open_connection
  self.layout.items = %w(timestamp level logger message pid hostname thread_id mdc)
  self.levels = Logging::LEVELS.invert
end

Instance Attribute Details

#levelsObject

Returns the value of attribute levels.



18
19
20
# File 'lib/logging/appenders/logentries_logging.rb', line 18

def levels
  @levels
end

#logentriesObject

Returns the value of attribute logentries.



18
19
20
# File 'lib/logging/appenders/logentries_logging.rb', line 18

def logentries
  @logentries
end

#tokenObject

Returns the value of attribute token.



18
19
20
# File 'lib/logging/appenders/logentries_logging.rb', line 18

def token
  @token
end

Instance Method Details

#close(*args) ⇒ Object



79
80
81
82
# File 'lib/logging/appenders/logentries_logging.rb', line 79

def close( *args )
  close_connection
  super(false)
end

#close_connectionObject



58
59
60
61
62
63
64
# File 'lib/logging/appenders/logentries_logging.rb', line 58

def close_connection
  if @logentries.respond_to?(:sysclose)
    @logentries.sysclose
  elsif @logentries.respond_to?(:close)
    @logentries.close
  end
end

#open_connectionObject

SSL socket setup/closing pulled from github.com/logentries/le_ruby



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/logging/appenders/logentries_logging.rb', line 31

def open_connection
  host = 'api.logentries.com'
  socket = TCPSocket.new(host, 20000)

  cert_store = OpenSSL::X509::Store.new
  cert_store.set_default_paths

  ssl_context = OpenSSL::SSL::SSLContext.new()
  ssl_context.cert_store = cert_store

  ssl_version_candidates = [:TLSv1_2, :TLSv1_1, :TLSv1]
  ssl_version_candidates = ssl_version_candidates.select { |version| OpenSSL::SSL::SSLContext::METHODS.include? version }
  if ssl_version_candidates.empty?
    raise "Could not find suitable TLS version"
  end
  # currently we only set the version when we have no choice
  ssl_context.ssl_version = ssl_version_candidates[0] if ssl_version_candidates.length == 1
  ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
  ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
  ssl_socket.hostname = host if ssl_socket.respond_to?(:hostname=)
  ssl_socket.sync_close = true
  Timeout::timeout(10) do
    ssl_socket.connect
  end
  ssl_socket
end

#reopenObject



75
76
77
# File 'lib/logging/appenders/logentries_logging.rb', line 75

def reopen
  @logentries = open_connection
end

#write(event) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/logging/appenders/logentries_logging.rb', line 66

def write(event)
  data = "#{@token} #{self.layout.format(event)} \n"
  @logentries.write(data)
rescue TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError
  close_connection
  @logentries = open_connection
  @logentries.write(data)
end