lib/syslogger.rb in syslogger-1.2.7 vs lib/syslogger.rb in syslogger-1.3.0
- old
+ new
@@ -1,11 +1,12 @@
require 'syslog'
require 'logger'
+require 'thread'
class Syslogger
- VERSION = "1.2.7"
+ VERSION = "1.3.0"
attr_reader :level, :ident, :options, :facility
MAPPING = {
Logger::DEBUG => Syslog::LOG_DEBUG,
@@ -42,10 +43,11 @@
def initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil)
@ident = ident
@options = options || (Syslog::LOG_PID | Syslog::LOG_CONS)
@facility = facility
@level = Logger::INFO
+ @mutex = Mutex.new
end
%w{debug info warn error fatal unknown}.each do |logger_method|
# Accepting *args as message could be nil.
# Default params not supported in ruby 1.8.7
@@ -78,16 +80,18 @@
# If nil, the method will call the block and use the result as the message string.
# If both are nil or no block is given, it will use the progname as per the behaviour of both the standard Ruby logger, and the Rails BufferedLogger.
# +progname+:: optionally, overwrite the program name that appears in the log message.
def add(severity, message = nil, progname = nil, &block)
progname ||= @ident
- Syslog.open(progname, @options, @facility) { |s|
- s.mask = Syslog::LOG_UPTO(MAPPING[@level])
- s.log(
- MAPPING[severity],
- clean(message || (block && block.call) || progname)
- )
- }
+ @mutex.synchronize do
+ Syslog.open(progname, @options, @facility) do |s|
+ s.mask = Syslog::LOG_UPTO(MAPPING[@level])
+ s.log(
+ MAPPING[severity],
+ clean(message || (block && block.call) || progname)
+ )
+ end
+ end
end
# Sets the minimum level for messages to be written in the log.
# +level+:: one of <tt>Logger::DEBUG</tt>, <tt>Logger::INFO</tt>, <tt>Logger::WARN</tt>, <tt>Logger::ERROR</tt>, <tt>Logger::FATAL</tt>, <tt>Logger::UNKNOWN</tt>
def level=(level)