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!



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gelf/notifier.rb', line 33

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['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.



27
28
29
# File 'lib/gelf/notifier.rb', line 27

def collect_file_and_line
  @collect_file_and_line
end

#default_optionsObject

Returns the value of attribute default_options.



28
29
30
# File 'lib/gelf/notifier.rb', line 28

def default_options
  @default_options
end

#enabledObject

Returns the value of attribute enabled.



27
28
29
# File 'lib/gelf/notifier.rb', line 27

def enabled
  @enabled
end

#levelObject

Returns the value of attribute level.



28
29
30
# File 'lib/gelf/notifier.rb', line 28

def level
  @level
end

#level_mappingObject

Returns the value of attribute level_mapping.



28
29
30
# File 'lib/gelf/notifier.rb', line 28

def level_mapping
  @level_mapping
end

#max_chunk_sizeObject

Returns the value of attribute max_chunk_size.



28
29
30
# File 'lib/gelf/notifier.rb', line 28

def max_chunk_size
  @max_chunk_size
end

#rescue_network_errorsObject

Returns the value of attribute rescue_network_errors.



27
28
29
# File 'lib/gelf/notifier.rb', line 27

def rescue_network_errors
  @rescue_network_errors
end

Instance Method Details

#addressesObject

Get a list of receivers.

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


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

def addresses
  @sender.addresses
end

#addresses=(addrs) ⇒ Object

Set a list of receivers.

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


60
61
62
# File 'lib/gelf/notifier.rb', line 60

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

#closeObject

Closes sender



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

def close
  @sender.close
end

#disableObject



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

def disable
  @enabled = false
end

#enableObject



106
107
108
# File 'lib/gelf/notifier.rb', line 106

def enable
  @enabled = true
end

#notify(*args) ⇒ Object

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



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

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.



131
132
133
# File 'lib/gelf/notifier.rb', line 131

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