Class: RemoteSyslogLogger::UdpSender
- Inherits:
-
Object
- Object
- RemoteSyslogLogger::UdpSender
- Defined in:
- lib/remote_syslog_logger/udp_sender.rb
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(remote_hostname, remote_port, options = {}) ⇒ UdpSender
constructor
A new instance of UdpSender.
- #transmit(message) ⇒ Object (also: #write)
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 21 22 |
# File 'lib/remote_syslog_logger/udp_sender.rb', line 6 def initialize(remote_hostname, remote_port, = {}) @remote_hostname = remote_hostname @remote_port = remote_port @whinyerrors = [:whinyerrors] @max_size = [:max_size] @socket = UDPSocket.new @packet = SyslogProtocol::Packet.new local_hostname = [:local_hostname] || (Socket.gethostname rescue `hostname`.chomp) local_hostname = 'localhost' if local_hostname.nil? || local_hostname.empty? @packet.hostname = local_hostname @packet.facility = [:facility] || 'user' @packet.severity = [:severity] || 'notice' @packet.tag = [:program] || "#{File.basename($0)}[#{$$}]" end |
Instance Method Details
#close ⇒ Object
42 43 44 |
# File 'lib/remote_syslog_logger/udp_sender.rb', line 42 def close @socket.close end |
#transmit(message) ⇒ Object Also known as: write
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/remote_syslog_logger/udp_sender.rb', line 24 def transmit() .split(/\r?\n/).each do |line| begin next if line =~ /^\s*$/ packet = @packet.dup packet.content = line payload = @max_size ? packet.assemble(@max_size) : packet.assemble @socket.send(payload, 0, @remote_hostname, @remote_port) rescue $stderr.puts "#{self.class} error: #{$!.class}: #{$!}\nOriginal message: #{line}" raise if @whinyerrors end end end |