Class: GELF::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/gelf/notifier.rb

Overview

Graylog2 notifier.

Direct Known Subclasses

Logger

Constant Summary

MAX_CHUNKS =

Maximum number of GELF chunks as per GELF spec

128

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 12201, max_size = 'WAN', default_options = {}) ⇒ Notifier

host and port are host/ip and port of graylog2-server. max_size is passed to max_chunk_size=. default_options is used in notify!



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

def initialize(host = 'localhost', port = 12201, max_size = 'WAN', default_options = {})
  @enabled = true
  @collect_file_and_line = true
  @random = Random.new

  self.level = GELF::DEBUG
  self.max_chunk_size = max_size
  self.rescue_network_errors = false

  self.default_options = default_options
  self.default_options['version'] = SPEC_VERSION
  self.default_options['host'] ||= Socket.gethostname
  self.default_options['level'] ||= GELF::UNKNOWN
  self.default_options['facility'] ||= 'gelf-rb'
  self.default_options['protocol'] ||= GELF::Protocol::UDP

  if self.default_options['protocol'] == GELF::Protocol::TCP
    @sender = GELF::Transport::TCP.new([[host, port]])
  else
    @sender = GELF::Transport::UDP.new([[host, port]])
  end
  self.level_mapping = :logger
end

Instance Attribute Details

#collect_file_and_lineObject

Returns the value of attribute collect_file_and_line



10
11
12
# File 'lib/gelf/notifier.rb', line 10

def collect_file_and_line
  @collect_file_and_line
end

#default_optionsObject

Returns the value of attribute default_options



11
12
13
# File 'lib/gelf/notifier.rb', line 11

def default_options
  @default_options
end

#enabledObject

Returns the value of attribute enabled



10
11
12
# File 'lib/gelf/notifier.rb', line 10

def enabled
  @enabled
end

#levelObject

Returns the value of attribute level



11
12
13
# File 'lib/gelf/notifier.rb', line 11

def level
  @level
end

#level_mappingObject

Returns the value of attribute level_mapping



11
12
13
# File 'lib/gelf/notifier.rb', line 11

def level_mapping
  @level_mapping
end

#max_chunk_sizeObject

Returns the value of attribute max_chunk_size



11
12
13
# File 'lib/gelf/notifier.rb', line 11

def max_chunk_size
  @max_chunk_size
end

#rescue_network_errorsObject

Returns the value of attribute rescue_network_errors



10
11
12
# File 'lib/gelf/notifier.rb', line 10

def rescue_network_errors
  @rescue_network_errors
end

Instance Method Details

#addressesObject

Get a list of receivers.

notifier.addresses  # => [['localhost', 12201], ['localhost', 12202]]


42
43
44
# File 'lib/gelf/notifier.rb', line 42

def addresses
  @sender.addresses
end

#addresses=(addrs) ⇒ Object

Set a list of receivers.

notifier.addresses = [['localhost', 12201], ['localhost', 12202]]


48
49
50
# File 'lib/gelf/notifier.rb', line 48

def addresses=(addrs)
  @sender.addresses = addrs
end

#closeObject

Closes sender



109
110
111
# File 'lib/gelf/notifier.rb', line 109

def close
  @sender.close
end

#disableObject



100
101
102
# File 'lib/gelf/notifier.rb', line 100

def disable
  @enabled = false
end

#enableObject



104
105
106
# File 'lib/gelf/notifier.rb', line 104

def enable
  @enabled = true
end

#hostObject



52
53
54
55
# File 'lib/gelf/notifier.rb', line 52

def host
  warn "GELF::Notifier#host is deprecated. Use #addresses instead."
  self.addresses.first[0]
end

#notify(*args) ⇒ Object

Same as notify!, but rescues all exceptions (including ArgumentError) and sends them instead.



115
116
117
# File 'lib/gelf/notifier.rb', line 115

def notify(*args)
  notify_with_level(nil, *args)
end

#notify!(*args) ⇒ Object

Sends message to Graylog2 server. args can be:

  • hash-like object (any object which responds to to_hash, including Hash instance):

    notify!(:short_message => 'All your rebase are belong to us', :user => 'AlekSi')
  • exception with optional hash-like object:

    notify!(SecurityError.new('ALARM!'), :trespasser => 'AlekSi')
  • string-like object (anything which responds to to_s) with optional hash-like object:

    notify!('Plain olde text message', :scribe => 'AlekSi')

Resulted fields are merged with default_options, the latter will never overwrite the former. This method will raise ArgumentError if arguments are wrong. Consider using notify instead.



129
130
131
# File 'lib/gelf/notifier.rb', line 129

def notify!(*args)
  notify_with_level!(nil, *args)
end

#portObject



57
58
59
60
# File 'lib/gelf/notifier.rb', line 57

def port
  warn "GELF::Notifier#port is deprecated. Use #addresses instead."
  self.addresses.first[1]
end