lib/syslogger.rb in syslogger-1.4.1 vs lib/syslogger.rb in syslogger-1.4.2

- old
+ new

@@ -2,13 +2,13 @@ require 'logger' require 'thread' class Syslogger - VERSION = "1.4.1" + VERSION = "1.4.2" - attr_reader :level, :ident, :options, :facility + attr_reader :level, :ident, :options, :facility, :max_octets MAPPING = { Logger::DEBUG => Syslog::LOG_DEBUG, Logger::INFO => Syslog::LOG_INFO, Logger::WARN => Syslog::LOG_WARNING, @@ -74,24 +74,39 @@ add(Logger::INFO, msg) end # Low level method to add a message. # +severity+:: the level of the message. One of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN - # +message+:: the message string. - # If nil, the method will call the block and use the result as the message string. + # +message+:: the message string. + # 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 @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) - ) + communication = clean(message || (block && block.call) || progname) + if self.max_octets + buffer = "" + communication.bytes do |byte| + buffer.concat(byte) + if buffer.bytesize >= self.max_octets + s.log(MAPPING[severity],buffer) + buffer = "" + end + end + s.log(MAPPING[severity],buffer) unless buffer.empty? + else + s.log(MAPPING[severity],communication) + end end end + end + + # Set the max octets of the messages written to the log + def max_octets=(max_octets) + @max_octets = max_octets 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)