Class: GELF::Notifier

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

Overview

Graylog2 notifier.

Direct Known Subclasses

Logger

Constant Summary

Class Attribute Summary collapse

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!



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gelf/notifier.rb', line 15

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

  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'

  @sender = RubyUdpSender.new([[host, port]])
  self.level_mapping = :logger
end

Class Attribute Details

.last_chunk_idObject

Returns the value of attribute last_chunk_id



6
7
8
# File 'lib/gelf/notifier.rb', line 6

def last_chunk_id
  @last_chunk_id
end

Instance Attribute Details

#collect_file_and_lineObject

Returns the value of attribute collect_file_and_line



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

def collect_file_and_line
  @collect_file_and_line
end

#default_optionsObject

Returns the value of attribute default_options



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

def default_options
  @default_options
end

#enabledObject

Returns the value of attribute enabled



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

def enabled
  @enabled
end

#levelObject

Returns the value of attribute level



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

def level
  @level
end

#level_mappingObject

Returns the value of attribute level_mapping



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

def level_mapping
  @level_mapping
end

#max_chunk_sizeObject

Returns the value of attribute max_chunk_size



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

def max_chunk_size
  @max_chunk_size
end

#rescue_network_errorsObject

Returns the value of attribute rescue_network_errors



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

def rescue_network_errors
  @rescue_network_errors
end

Instance Method Details

#addressesObject

Get a list of receivers.

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


35
36
37
# File 'lib/gelf/notifier.rb', line 35

def addresses
  @sender.addresses
end

#addresses=(addrs) ⇒ Object

Set a list of receivers.

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


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

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

#disableObject



93
94
95
# File 'lib/gelf/notifier.rb', line 93

def disable
  @enabled = false
end

#enableObject



97
98
99
# File 'lib/gelf/notifier.rb', line 97

def enable
  @enabled = true
end

#hostObject



45
46
47
48
# File 'lib/gelf/notifier.rb', line 45

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.



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

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.



117
118
119
# File 'lib/gelf/notifier.rb', line 117

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

#portObject



50
51
52
53
# File 'lib/gelf/notifier.rb', line 50

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