lib/remote_syslog/reader.rb in remote_syslog-1.1.1 vs lib/remote_syslog/reader.rb in remote_syslog-1.2.0

- old
+ new

@@ -1,5 +1,6 @@ +require 'socket' require 'eventmachine' require 'eventmachine-tail' require 'em-dns-resolver' require 'syslog_protocol' @@ -11,10 +12,11 @@ super(path, -1) @destination_address = destination_address @destination_port = destination_port.to_i + @parse_fields = options[:parse_fields] @strip_color = options[:strip_color] @socket = options[:socket] || EventMachine.open_datagram_socket('0.0.0.0', 0) @buffer = BufferedTokenizer.new @@ -29,10 +31,15 @@ @packet.hostname = local_hostname @packet.facility = options[:facility] || 'user' @packet.severity = options[:severity] || 'notice' @packet.tag = options[:program] || File.basename(path) || File.basename($0) + # Make sure the tag isn't too long + if @packet.tag.length > 32 + @packet.tag = @packet.tag[0..31] + end + # Try to resolve the destination address resolve_destination_address # Every 60 seconds we'll see if the address has changed EventMachine.add_periodic_timer(60) do @@ -60,9 +67,17 @@ def transmit(message) message = message.gsub(COLORED_REGEXP, '') if @strip_color packet = @packet.dup packet.content = message + + if @parse_fields + if message =~ @parse_fields + packet.hostname = $2 if $2 && $2 != '' + packet.tag = $3 if $3 && $2 != '' + packet.content = $4 if $4 && $4 != '' + end + end @socket.send_datagram(packet.assemble, destination_address, @destination_port) end end end