Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obey Syslog size limits #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Ensure default tag is within size limits
RFC 3164 section 4.1.3 specifies that the tag MUST NOT exceed 32
characters.  This is enforced by
https://github.com/eric/syslog_protocol/blob/001000ffe27a4557c3ec312b9c3c50385e6a923b/lib/syslog_protocol/packet.rb#L48-L50

Ensure that the default tag generated from the program name and PID is
within these bounds.
  • Loading branch information
jscheid committed Dec 19, 2014
commit b9ab8315046186096574f4c490f61d3fd7a07960
8 changes: 7 additions & 1 deletion lib/remote_syslog_logger/udp_sender.rb
Original file line number Diff line number Diff line change
@@ -17,7 +17,13 @@ def initialize(remote_hostname, remote_port, options = {})

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

def default_tag
pid_suffix = "[#{$$}]"
max_basename_size = 32 - pid_suffix.size
"#{File.basename($0)}"[0...max_basename_size].gsub(/[^\x21-\x7E]/, '_') + pid_suffix
end

def transmit(message)
21 changes: 20 additions & 1 deletion test/test_remote_syslog_logger.rb
Original file line number Diff line number Diff line change
@@ -25,4 +25,23 @@ def test_logger_multiline
message, addr = *@socket.recvfrom(1024)
assert_match /This is the second line/, message
end
end

def test_logger_default_tag
$0 = 'foo'
logger = RemoteSyslogLogger.new('127.0.0.1', @server_port)
logger.info ""

message, addr = *@socket.recvfrom(1024)
assert_match "foo[#{$$}]: I,", message
end

def test_logger_long_default_tag
$0 = 'x' * 64
pid_suffix = "[#{$$}]"
logger = RemoteSyslogLogger.new('127.0.0.1', @server_port)
logger.info ""

message, addr = *@socket.recvfrom(1024)
assert_match 'x' * (32 - pid_suffix.size) + pid_suffix + ': I,', message
end
end