lib/loggability/logger.rb in loggability-0.15.1 vs lib/loggability/logger.rb in loggability-0.16.0

- old
+ new

@@ -22,40 +22,10 @@ # Default 'shift size' DEFAULT_SHIFT_SIZE = 1048576 - # A log device that appends to the object it's constructed with instead of writing - # to a file descriptor or a file. - class AppendingLogDevice - - ### Create a new AppendingLogDevice that will append content to +target+ (a - ### object that responds to #>>). - def initialize( target ) - @target = target - end - - - ###### - public - ###### - - # The target of the log device - attr_reader :target - - - ### Append the specified +message+ to the target. - def write( message ) - @target << message - end - - ### No-op -- this is here just so Logger doesn't complain - def close; end - - end # class AppendingLogDevice - - # Proxy for the Logger that injects the name of the object it wraps as the 'progname' # of each log message. class ObjectNameProxy ### Create a proxy for the given +logger+ that will inject the name of the @@ -271,18 +241,21 @@ ### Change the log device to log to +target+ instead of what it was before. Any additional ### +args+ are passed to the LogDevice's constructor. In addition to Logger's support for ### logging to IO objects and files (given a filename in a String), this method can also ### set up logging to any object that responds to #<<. def output_to( target, *args ) - if target.is_a?( Logger::LogDevice ) || - target.is_a?( Loggability::Logger::AppendingLogDevice ) + if target.is_a?( Logger::LogDevice ) || target.is_a?( Loggability::LogDevice ) self.logdev = target elsif target.respond_to?( :write ) || target.is_a?( String ) opts = { :shift_age => args.shift || 0, :shift_size => args.shift || 1048576 } self.logdev = Logger::LogDevice.new( target, **opts ) + elsif target.respond_to?( :any? ) && target.any?( Loggability::LogDevice ) + self.logdev = MultiDevice.new( target ) elsif target.respond_to?( :<< ) - self.logdev = AppendingLogDevice.new( target ) + self.logdev = Loggability::LogDevice.create( :appending, target ) + elsif target.is_a?( Symbol ) + self.logdev = Loggability::LogDevice.create( target, *args ) else raise ArgumentError, "don't know how to output to %p (a %p)" % [ target, target.class ] end end alias_method :write_to, :output_to @@ -309,13 +282,14 @@ name = LOG_LEVEL_NAMES[ severity ] || severity.to_s return name.upcase end - ### Set a new +formatter+ for the logger. If +formatter+ is +nil+ or +:default+, this causes the - ### logger to fall back to its default formatter. If it's a Symbol other than +:default+, it looks - ### for a similarly-named formatter under loggability/formatter/ and uses that. If +formatter+ is - ### an object that responds to #call (e.g., a Proc or a Method object), that object is used directly. + ### Set a new +formatter+ for the logger. If +formatter+ is +nil+ or +:default+, this causes + ### the logger to fall back to its default formatter. If it's a Symbol other than +:default+, + ### it looks for a similarly-named formatter under loggability/formatter/ and uses that. If + ### +formatter+ is an object that responds to #call (e.g., a Proc or a Method object), that + ### object is used directly. ### ### Procs and methods should have the method signature: (severity, datetime, progname, msg). ### ### # Load and use the HTML formatter ### MyProject.logger.format_with( :html )