ruby-changes:30143
From: knu <ko1@a...>
Date: Fri, 26 Jul 2013 20:15:17 +0900 (JST)
Subject: [ruby-changes:30143] knu:r42195 (trunk): Add facility to Syslog::Logger.
knu 2013-07-26 20:15:06 +0900 (Fri, 26 Jul 2013) New Revision: 42195 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42195 Log: Add facility to Syslog::Logger. * 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 Modified files: trunk/ChangeLog trunk/NEWS trunk/ext/syslog/lib/syslog/logger.rb trunk/test/syslog/test_syslog_logger.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 42194) +++ ChangeLog (revision 42195) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Jul 26 20:12:07 2013 Akinori MUSHA <knu@i...> + + * 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@a...> * vm_exec.h, tool/instruction.rb: not an error, but a BUG if stack Index: ext/syslog/lib/syslog/logger.rb =================================================================== --- ext/syslog/lib/syslog/logger.rb (revision 42194) +++ ext/syslog/lib/syslog/logger.rb (revision 42195) @@ -24,6 +24,14 @@ require 'logger' https://github.com/ruby/ruby/blob/trunk/ext/syslog/lib/syslog/logger.rb#L24 # 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 https://github.com/ruby/ruby/blob/trunk/ext/syslog/lib/syslog/logger.rb#L176 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 https://github.com/ruby/ruby/blob/trunk/ext/syslog/lib/syslog/logger.rb#L202 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 - Index: NEWS =================================================================== --- NEWS (revision 42194) +++ NEWS (revision 42195) @@ -116,6 +116,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L116 * extended methods: * StringScanner#[] supports named captures. +* Syslog::Logger + * Added facility. + * Tempfile * New methods: * Tempfile.create Index: test/syslog/test_syslog_logger.rb =================================================================== --- test/syslog/test_syslog_logger.rb (revision 42194) +++ test/syslog/test_syslog_logger.rb (revision 42195) @@ -12,30 +12,31 @@ end https://github.com/ruby/ruby/blob/trunk/test/syslog/test_syslog_logger.rb#L12 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 - - eval <<-EOM - def #{name.downcase}(format, *args) - log(#{level}, format, *args) - end - EOM - end + @facility = Syslog::LOG_USER - 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) https://github.com/ruby/ruby/blob/trunk/test/syslog/test_syslog_logger.rb#L473 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 < TestSyslogRootL https://github.com/ruby/ruby/blob/trunk/test/syslog/test_syslog_logger.rb#L493 } 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 < TestSyslogRootL https://github.com/ruby/ruby/blob/trunk/test/syslog/test_syslog_logger.rb#L526 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) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/