Class: SyslogSD::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/syslog-sd/notifier.rb

Overview

syslog notifier.

Direct Known Subclasses

Logger

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 514, default_options = {}) ⇒ Notifier

host and port are host/ip and port of syslog server. default_options is used in notify!



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/syslog-sd/notifier.rb', line 9

def initialize(host = 'localhost', port = 514, default_options = {})
  @enabled = true
  @collect_file_and_line = true
  @sd_id = "_@37797"

  self.level = SyslogSD::DEBUG
  self.timestamp_as_float = false
  self.rescue_network_errors = false

  self.default_options = default_options
  self.default_options['host'] ||= Socket.gethostname
  self.default_options['level'] ||= SyslogSD::UNKNOWN
  self.default_options['facility'] ||= 'syslog-sd-rb'
  self.default_options['procid'] ||= Process.pid

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

Instance Attribute Details

#collect_file_and_lineObject

Returns the value of attribute collect_file_and_line.



4
5
6
# File 'lib/syslog-sd/notifier.rb', line 4

def collect_file_and_line
  @collect_file_and_line
end

#default_optionsObject

Returns the value of attribute default_options.



5
6
7
# File 'lib/syslog-sd/notifier.rb', line 5

def default_options
  @default_options
end

#enabledObject

Returns the value of attribute enabled.



4
5
6
# File 'lib/syslog-sd/notifier.rb', line 4

def enabled
  @enabled
end

#levelObject

Returns the value of attribute level.



5
6
7
# File 'lib/syslog-sd/notifier.rb', line 5

def level
  @level
end

#level_mappingObject

Returns the value of attribute level_mapping.



5
6
7
# File 'lib/syslog-sd/notifier.rb', line 5

def level_mapping
  @level_mapping
end

#rescue_network_errorsObject

Returns the value of attribute rescue_network_errors.



4
5
6
# File 'lib/syslog-sd/notifier.rb', line 4

def rescue_network_errors
  @rescue_network_errors
end

#timestamp_as_floatObject

Returns the value of attribute timestamp_as_float.



4
5
6
# File 'lib/syslog-sd/notifier.rb', line 4

def timestamp_as_float
  @timestamp_as_float
end

Instance Method Details

#addressesObject

Get a list of receivers.

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


30
31
32
# File 'lib/syslog-sd/notifier.rb', line 30

def addresses
  @sender.addresses
end

#addresses=(addrs) ⇒ Object

Set a list of receivers.

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


36
37
38
# File 'lib/syslog-sd/notifier.rb', line 36

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

#disableObject



75
76
77
# File 'lib/syslog-sd/notifier.rb', line 75

def disable
  @enabled = false
end

#enableObject



79
80
81
# File 'lib/syslog-sd/notifier.rb', line 79

def enable
  @enabled = true
end

#hostObject



40
41
42
43
# File 'lib/syslog-sd/notifier.rb', line 40

def host
  warn "SyslogSD::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.



85
86
87
# File 'lib/syslog-sd/notifier.rb', line 85

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.



99
100
101
# File 'lib/syslog-sd/notifier.rb', line 99

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

#portObject



45
46
47
48
# File 'lib/syslog-sd/notifier.rb', line 45

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