Class: RemoteSyslogLogger::UdpSender

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_syslog_logger/udp_sender.rb

Instance Method Summary collapse

Constructor Details

#initialize(remote_hostname, remote_port, options = {}) ⇒ UdpSender

Returns a new instance of UdpSender.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/remote_syslog_logger/udp_sender.rb', line 6

def initialize(remote_hostname, remote_port, options = {})
  @remote_hostname = remote_hostname
  @remote_port     = remote_port
  
  @socket = UDPSocket.new
  @packet = SyslogProtocol::Packet.new

  local_hostname   = options[:local_hostname] || (Socket.gethostname rescue `hostname`.chomp)
  local_hostname   = 'localhost' if local_hostname.nil? || local_hostname.empty?
  @packet.hostname = local_hostname

  @packet.facility = options[:facility] || 'user'
  @packet.severity = options[:severity] || 'notice'
  @packet.tag      = options[:program]  || "#{File.basename($0)}[#{$$}]"
end

Instance Method Details

#closeObject



34
35
36
# File 'lib/remote_syslog_logger/udp_sender.rb', line 34

def close
  @socket.close
end

#transmit(message) ⇒ Object Also known as: write



22
23
24
25
26
27
28
29
# File 'lib/remote_syslog_logger/udp_sender.rb', line 22

def transmit(message)
  message.split(/\r?\n/).each do |line|
    next if line =~ /^\s*$/
    packet = @packet.dup
    packet.content = line
    @socket.send(packet.assemble, 0, @remote_hostname, @remote_port)
  end
end