lib/loggability.rb in loggability-0.7.0 vs lib/loggability.rb in loggability-0.8.0.pre.65

- old
+ new

@@ -7,14 +7,14 @@ # A mixin that provides a top-level logging subsystem based on Logger. module Loggability # Package version constant - VERSION = '0.7.0' + VERSION = '0.8.0' # VCS revision - REVISION = %q$Revision: 7e7b1c51eb3e $ + REVISION = %q$Revision: 44b025b728e6 $ # The key for the global logger (Loggability's own logger) GLOBAL_KEY = :__global__ # The methods that are delegated across all loggers @@ -51,10 +51,16 @@ # The Hash of modules that have a Logger, keyed by the name they register with class << self; attr_reader :log_hosts; end @log_hosts = {} + # Automatically log the log host and log client mixins when they're referenced + autoload :LogHost, 'loggability/loghost' + autoload :LogClient, 'loggability/logclient' + autoload :Override, 'loggability/override' + + ### Return the library's version string def self::version_string( include_buildnum=false ) vstring = "%s %s" % [ self.name, VERSION ] vstring << " (build %s)" % [ REVISION[/: ([[:xdigit:]]+)/, 1] || '0' ] if include_buildnum return vstring @@ -143,10 +149,25 @@ def self::level=( newlevel ) self.aggregate( :level=, newlevel ) end + ### Aggregate method: set the log level on all loggers to +level+ for the duration + ### of the +block+, restoring the original levels afterward. If no block is given, returns a + ### Loggability::Override object that set the log level to +level+ while its +#call+ + ### method is being called. + def self::with_level( level, &block ) + override = Loggability::Override.with_level( level ) + + if block + return override.call( &block ) + else + return override + end + end + + ## # :method: output_to # :call-seq: # output_to( destination ) # write_to( destination ) @@ -159,10 +180,25 @@ class << self alias_method :write_to, :output_to end + ### Aggregate method: set all loggers to log to +destination+ for the duration of the + ### +block+, restoring the original destination afterward. If no block is given, returns a + ### Loggability::Override object that will log to +destination+ whenever its +#call+ method is + ### called. + def self::outputting_to( newdevice, &block ) + override = Loggability::Override.outputting_to( newdevice ) + + if block + return override.call( &block ) + else + return override + end + end + + ## # :method: format_with # :call-seq: # format_with( formatter ) # format_as( formatter ) @@ -177,86 +213,22 @@ alias_method :format_as, :format_with alias_method :formatter=, :format_with end - # Extension for 'log hosts'. A <b>log host</b> is an object that hosts a Loggability::Logger - # object, and is typically the top of some kind of hierarchy, like a namespace - # module for a project: - # - # module MyProject - # - # end - # - # This module isn't mean to be used directly -- it's installed via the Loggability#log_as - # declaration, which also does some other initialization that you'll likely want. - # - # - module LogHost + ### Aggregate method: set all loggers to log with the given +formatter+ for the duration + ### of the +block+, restoring the original formatters afterward. If no block is given, + ### returns a Loggability::Override object that will override all formatters whenever its + ### +#call+ method is called. + def self::formatted_with( formatter, &block ) + override = Loggability::Override.formatted_with( formatter ) - # The logger that will be used when the logging subsystem is reset - attr_accessor :default_logger - - # The logger that's currently in effect - attr_reader :logger - alias_method :log, :logger - - # The key associated with the logger for this host - attr_accessor :log_host_key - - - ### Set the logger associated with the LogHost to +newlogger+. If +newlogger+ isn't a - ### Loggability::Logger, it will be converted to one. - def logger=( newlogger ) - @logger = Loggability::Logger( newlogger ) + if block + return override.call( &block ) + else + return override end - alias_method :log=, :logger= - - end # module LogHost - - - # Methods to install for objects which call +log_to+. - module LogClient - - ## - # The key of the log host this client targets - attr_accessor :log_host_key - - ### Return the Loggability::Logger object associated with the log host the - ### client is logging to. - ### :TODO: Use delegation for efficiency. - def log - @__log ||= Loggability[ self ].proxy_for( self ) - end - - - ### Inheritance hook -- set the log host key of subclasses to the same - ### thing as the extended class. - def inherited( subclass ) - super - Loggability.log.debug "Setting up subclass %p of %p to log to %p" % - [ subclass, self, self.log_host_key ] - subclass.log_host_key = self.log_host_key - end - - - # Stuff that gets added to instances of Classes that are log hosts. - module InstanceMethods - - ### Fetch the key of the log host the instance of this client targets - def log_host_key - return self.class.log_host_key - end - - - ### Delegate to the class's logger. - def log - @__log ||= Loggability[ self.class ].proxy_for( self ) - end - - end # module InstanceMethods - - end # module LogClient + end # # :section: LogHost API #