summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--NEWS3
-rw-r--r--ext/syslog/lib/syslog/logger.rb22
-rw-r--r--test/syslog/test_syslog_logger.rb89
4 files changed, 97 insertions, 23 deletions
diff --git a/ChangeLog b/ChangeLog
index 37beb6c..4eb41db 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Fri Jul 26 20:12:07 2013 Akinori MUSHA <knu@iDaemons.org>
+
+ * ext/syslog/lib/syslog/logger.rb (Syslog::Logger): Add facility
+ to Syslog::Logger. [Fixes GH-305] patch by Max Shytikov
+ https://github.com/ruby/ruby/pull/305
+
Fri Jul 26 19:25:17 2013 Koichi Sasada <ko1@atdot.net>
* vm_exec.h, tool/instruction.rb: not an error, but a BUG if stack
diff --git a/NEWS b/NEWS
index f5fe388..2d40dcf 100644
--- a/NEWS
+++ b/NEWS
@@ -116,6 +116,9 @@ with all sufficient information, see the ChangeLog file.
* extended methods:
* StringScanner#[] supports named captures.
+* Syslog::Logger
+ * Added facility.
+
* Tempfile
* New methods:
* Tempfile.create
diff --git a/ext/syslog/lib/syslog/logger.rb b/ext/syslog/lib/syslog/logger.rb
index 1e1f9fe..086f83c 100644
--- a/ext/syslog/lib/syslog/logger.rb
+++ b/ext/syslog/lib/syslog/logger.rb
@@ -24,6 +24,14 @@ require 'logger'
# log = Syslog::Logger.new 'my_program'
# log.info 'this line will be logged via syslog(3)'
#
+# Also the facility may be set to specify the facility level which will be used:
+#
+# log.info 'this line will be logged using Syslog default facility level'
+#
+# log_local1 = Syslog::Logger.new 'my_program', Syslog::LOG_LOCAL1
+# log_local1.info 'this line will be logged using local1 facility level'
+#
+#
# You may need to perform some syslog.conf setup first. For a BSD machine add
# the following lines to /etc/syslog.conf:
#
@@ -168,17 +176,24 @@ class Syslog::Logger
attr_accessor :formatter
##
+ # The facility argument is used to specify what type of program is logging the message.
+
+ attr_accessor :facility
+
+ ##
# Fills in variables for Logger compatibility. If this is the first
# instance of Syslog::Logger, +program_name+ may be set to change the logged
- # program name.
+ # program name. The +facility+ may be set to specify the facility level which will be used.
#
# Due to the way syslog works, only one program name may be chosen.
- def initialize program_name = 'ruby'
+ def initialize program_name = 'ruby', facility = nil
@level = ::Logger::DEBUG
@formatter = Formatter.new
@@syslog ||= Syslog.open(program_name)
+
+ @facility = (facility || @@syslog.facility)
end
##
@@ -187,8 +202,7 @@ class Syslog::Logger
def add severity, message = nil, progname = nil, &block
severity ||= ::Logger::UNKNOWN
@level <= severity and
- @@syslog.log LEVEL_MAP[severity], '%s', formatter.call(severity, Time.now, progname, (message || block.call))
+ @@syslog.log( (LEVEL_MAP[severity] | @facility), '%s', formatter.call(severity, Time.now, progname, (message || block.call)) )
true
end
end
-
diff --git a/test/syslog/test_syslog_logger.rb b/test/syslog/test_syslog_logger.rb
index 65b4a94..d1998a1 100644
--- a/test/syslog/test_syslog_logger.rb
+++ b/test/syslog/test_syslog_logger.rb
@@ -12,30 +12,31 @@ end
class TestSyslogRootLogger < Test::Unit::TestCase
module MockSyslog
- LEVEL_LABEL_MAP = {}
- class << self
+ PRIMASK = Syslog::Level.constants.inject(0) { |mask, name| mask | Syslog::Level.const_get(name) }
- @line = nil
+ LEVEL_LABEL_MAP = {
+ Syslog::LOG_ALERT => 'ALERT',
+ Syslog::LOG_ERR => 'ERR',
+ Syslog::LOG_WARNING => 'WARNING',
+ Syslog::LOG_NOTICE => 'NOTICE',
+ Syslog::LOG_INFO => 'INFO',
+ Syslog::LOG_DEBUG => 'DEBUG'
+ }
- %w[ALERT ERR WARNING NOTICE INFO DEBUG].each do |name|
- level = Syslog.const_get("LOG_#{name}")
- LEVEL_LABEL_MAP[level] = name
+ @facility = Syslog::LOG_USER
- eval <<-EOM
- def #{name.downcase}(format, *args)
- log(#{level}, format, *args)
- end
- EOM
- end
-
- def log(level, format, *args)
- @line = "#{LEVEL_LABEL_MAP[level]} - #{format % args}"
- end
+ class << self
+ attr_reader :facility
attr_reader :line
attr_reader :program_name
+ def log(priority, format, *args)
+ level = priority & PRIMASK
+ @line = "<#{priority}> #{LEVEL_LABEL_MAP[level]} - #{format % args}"
+ end
+
def open(program_name)
@program_name = program_name
end
@@ -472,6 +473,12 @@ end if defined?(Syslog)
class TestSyslogLogger < TestSyslogRootLogger
+ @facility = Syslog::LOG_USER
+
+ def facility
+ self.class.instance_variable_get("@facility")
+ end
+
def setup
super
@logger = Syslog::Logger.new
@@ -486,12 +493,13 @@ class TestSyslogLogger < TestSyslogRootLogger
}
class Log
- attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
+ attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg, :priority
def initialize(line)
@line = line
- return unless /\A(\w+) - (.*)\Z/ =~ @line
- severity, @msg = $1, $2
+ return unless /\A<(\d+)> (\w+) - (.*)\Z/ =~ @line
+ priority, severity, @msg = $1, $2, $3
@severity = SEVERITY_MAP[severity]
+ @priority = priority.to_i
end
end
@@ -518,4 +526,47 @@ class TestSyslogLogger < TestSyslogRootLogger
assert_equal false, @logger.unknown?
end
+ def test_facility
+ assert_equal facility, @logger.facility
+ end
+
+ def test_priority
+ msg = log_add nil, 'unknown level message' # nil == unknown
+ assert_equal facility|Syslog::LOG_ALERT, msg.priority
+
+ msg = log_add Logger::FATAL, 'fatal level message'
+ assert_equal facility|Syslog::LOG_ERR, msg.priority
+
+ msg = log_add Logger::ERROR, 'error level message'
+ assert_equal facility|Syslog::LOG_WARNING, msg.priority
+
+ msg = log_add Logger::WARN, 'warn level message'
+ assert_equal facility|Syslog::LOG_NOTICE, msg.priority
+
+ msg = log_add Logger::INFO, 'info level message'
+ assert_equal facility|Syslog::LOG_INFO, msg.priority
+
+ msg = log_add Logger::DEBUG, 'debug level message'
+ assert_equal facility|Syslog::LOG_DEBUG, msg.priority
+ end
+
+end if defined?(Syslog)
+
+
+# Create test class for each available facility
+
+Syslog::Facility.constants.each do |facility_symb|
+
+ test_syslog_class = Class.new(TestSyslogLogger) do
+
+ @facility = Syslog.const_get(facility_symb)
+
+ def setup
+ super
+ @logger.facility = facility
+ end
+
+ end
+ Object.const_set("TestSyslogLogger_#{facility_symb}", test_syslog_class)
+
end if defined?(Syslog)