Class: GELF::Notifier

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

Overview

Graylog2 notifier.

Direct Known Subclasses

Logger

Constant Summary collapse

MAX_CHUNKS =

Maximum number of GELF chunks as per GELF spec

128
MAX_CHUNK_SIZE_WAN =
1420
MAX_CHUNK_SIZE_LAN =
8154

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!



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gelf/notifier.rb', line 25

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.dup
  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

  self.level_mapping = :logger
  @sender = create_sender(host, port)
end

Instance Attribute Details

#collect_file_and_lineObject

Returns the value of attribute collect_file_and_line.



19
20
21
# File 'lib/gelf/notifier.rb', line 19

def collect_file_and_line
  @collect_file_and_line
end

#default_optionsObject

Returns the value of attribute default_options.



20
21
22
# File 'lib/gelf/notifier.rb', line 20

def default_options
  @default_options
end

#enabledObject

Returns the value of attribute enabled.



19
20
21
# File 'lib/gelf/notifier.rb', line 19

def enabled
  @enabled
end

#levelObject

Returns the value of attribute level.



20
21
22
# File 'lib/gelf/notifier.rb', line 20

def level
  @level
end

#level_mappingObject

Returns the value of attribute level_mapping.



20
21
22
# File 'lib/gelf/notifier.rb', line 20

def level_mapping
  @level_mapping
end

#max_chunk_sizeObject

Returns the value of attribute max_chunk_size.



20
21
22
# File 'lib/gelf/notifier.rb', line 20

def max_chunk_size
  @max_chunk_size
end

#rescue_network_errorsObject

Returns the value of attribute rescue_network_errors.



19
20
21
# File 'lib/gelf/notifier.rb', line 19

def rescue_network_errors
  @rescue_network_errors
end

Instance Method Details

#addressesObject

Get a list of receivers.

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


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

def addresses
  @sender.addresses
end

#addresses=(addrs) ⇒ Object

Set a list of receivers.

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


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

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

#closeObject

Closes sender



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

def close
  @sender.close
end

#disableObject



95
96
97
# File 'lib/gelf/notifier.rb', line 95

def disable
  @enabled = false
end

#enableObject



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

def enable
  @enabled = true
end

#notify(*args) ⇒ Object

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



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

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.



124
125
126
# File 'lib/gelf/notifier.rb', line 124

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