mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42195 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
621da98309
commit
9d00f3f536
4 changed files with 99 additions and 25 deletions
|
@ -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
|
||||
|
|
3
NEWS
3
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
|
||||
|
|
|
@ -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:
|
||||
#
|
||||
|
@ -167,18 +175,25 @@ class Syslog::Logger
|
|||
# set.
|
||||
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
|
||||
|
||||
|
|
|
@ -12,30 +12,31 @@ end
|
|||
class TestSyslogRootLogger < Test::Unit::TestCase
|
||||
|
||||
module MockSyslog
|
||||
LEVEL_LABEL_MAP = {}
|
||||
|
||||
PRIMASK = Syslog::Level.constants.inject(0) { |mask, name| mask | Syslog::Level.const_get(name) }
|
||||
|
||||
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'
|
||||
}
|
||||
|
||||
@facility = Syslog::LOG_USER
|
||||
|
||||
class << self
|
||||
|
||||
@line = nil
|
||||
|
||||
%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
|
||||
|
||||
def log(level, format, *args)
|
||||
@line = "#{LEVEL_LABEL_MAP[level]} - #{format % args}"
|
||||
end
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue