lib/logging/logger.rb in logging-1.8.2 vs lib/logging/logger.rb in logging-2.0.0
- old
+ new
@@ -26,54 +26,40 @@
@mutex = Mutex.new # :nodoc:
class << self
- # call-seq:
- # Logger.root
- #
# Returns the root logger.
- #
def root
::Logging::Repository.instance[:root]
end
- # :stopdoc:
+ alias_method :instantiate, :new # the "real" new
# Overrides the new method such that only one Logger will be created
# for any given logger name.
- #
def new( *args )
- return super if args.empty?
+ args.empty? ? super : self[args.shift]
+ end
+ # Returns a logger instance for the given name.
+ def []( name )
repo = ::Logging::Repository.instance
- name = repo.to_key(args.shift)
+ name = repo.to_key(name)
+ logger = repo[name]
+ return logger unless logger.nil?
@mutex.synchronize do
logger = repo[name]
- if logger.nil?
+ return logger unless logger.nil? # thread-safe double checking
- master = repo.master_for(name)
- if master
- if repo.has_logger?(master)
- logger = repo[master]
- else
- logger = super(master)
- repo[master] = logger
- repo.children(master).each {|c| c.__send__(:parent=, logger)}
- end
- repo[name] = logger
- else
- logger = super(name)
- repo[name] = logger
- repo.children(name).each {|c| c.__send__(:parent=, logger)}
- end
- end
+ logger = instantiate(name)
+ repo[name] = logger
+ repo.children(name).each { |c| c.__send__(:parent=, logger) }
logger
end
end
- alias :[] :new
# This is where the actual logging methods are defined. Two methods
# are created for each log level. The first is a query method used to
# determine if that perticular logging level is enabled. The second is
# the actual logging method that accepts a list of objects to be
@@ -108,25 +94,23 @@
else
code << <<-CODE
def #{name}?( ) true end
def #{name}( data = nil )
data = yield if block_given?
- log_event(::Logging::LogEvent.new(@name, #{num}, data, @trace))
+ log_event(::Logging::LogEvent.new(@name, #{num}, data, @caller_tracing))
true
end
CODE
end
logger._meta_eval(code, __FILE__, __LINE__)
end
logger
end
- # :startdoc:
-
end # class << self
- attr_reader :name, :parent, :additive, :trace
+ attr_reader :name, :parent, :additive, :caller_tracing
# call-seq:
# Logger.new( name )
# Logger[name]
#
@@ -181,11 +165,11 @@
#
def <<( msg )
@appenders.each {|a| a << msg}
@parent << msg if @additive
end
- alias :write :<<
+ alias_method :write, :<<
# call-seq:
# add( severity, message = nil ) {block}
#
# Log a message if the given severity is high enough. This is the generic
@@ -212,11 +196,11 @@
def add( lvl, data = nil, progname = nil )
lvl = Integer(lvl)
return false if lvl < level
data = yield if block_given?
- log_event(::Logging::LogEvent.new(@name, lvl, data, @trace))
+ log_event(::Logging::LogEvent.new(@name, lvl, data, @caller_tracing))
true
end
# call-seq:
# additive = true
@@ -232,22 +216,23 @@
when nil; @additive
else raise ArgumentError, 'expecting a boolean' end
end
# call-seq:
- # trace = true
+ # caller_tracing = true
#
- # Sets the tracing of the logger. Acceptable values are +true+,
- # 'true', +false+, 'false', or +nil+. In this case +nil+ does not
- # change the tracing.
+ # Sets the caller tracing of the logger. Acceptable values are +true+,
+ # 'true', +false+, 'false', or +nil+. In this case +nil+ does not change
+ # the tracing.
#
- def trace=( val )
- @trace = case val
- when true, 'true'; true
- when false, 'false'; false
- when nil; @trace
- else raise ArgumentError, 'expecting a boolean' end
+ def caller_tracing=( val )
+ @caller_tracing =
+ case val
+ when true, 'true'; true
+ when false, 'false'; false
+ when nil; @caller_tracing
+ else raise ArgumentError, 'expecting a boolean' end
end
# call-seq:
# level => integer
#
@@ -441,67 +426,69 @@
# Configures internal variables for the logger. This method can be used
# to avoid storing the logger in the repository.
#
def _setup( name, opts = {} )
@name = name
- @parent = opts.getopt(:parent)
- @appenders = opts.getopt(:appenders, [])
- @additive = opts.getopt(:additive, true)
- @trace = opts.getopt(:trace, false)
- @level = opts.getopt(:level)
+ @parent = opts.fetch(:parent, nil)
+ @appenders = opts.fetch(:appenders, [])
+ @additive = opts.fetch(:additive, true)
+ @level = opts.fetch(:level, nil)
+
+ @caller_tracing = opts.fetch(:caller_tracing, false)
+
::Logging::Logger.define_log_methods(self)
end
# call-seq:
# _dump_configuration( io = STDOUT, indent = 0 )
#
# An internal method that is used to dump this logger's configuration to
# the given _io_ stream. The configuration includes the logger's name,
- # level, additivity, and trace settings. The configured appenders are
- # also printed to the _io_ stream.
+ # level, additivity, and caller_tracing settings. The configured appenders
+ # are also printed to the _io_ stream.
#
- def _dump_configuration( io = STDOUT, indent = 0 )
+ def _dump_configuration( indent = 0 )
str, spacer, base = '', ' ', 50
indent_str = indent == 0 ? '' : ' ' * indent
str << indent_str
- str << self.name.reduce(base - indent)
+ str << self.name.shrink(base - indent)
if (str.length + spacer.length) < base
str << spacer
str << '.' * (base - str.length)
end
- io.write(str.ljust(base))
- io.write(spacer)
+ str = str.ljust(base)
+ str << spacer
level_str = @level.nil? ? '' : '*'
level_str << if level < ::Logging::LEVELS.length
::Logging.levelify(::Logging::LNAMES[level])
else
'off'
end
level_len = ::Logging::MAX_LEVEL_LENGTH + 1
- io.write("%#{level_len}s" % level_str)
- io.write(spacer)
+ str << sprintf("%#{level_len}s" % level_str)
+ str << spacer
if self.respond_to?(:additive)
- io.write(additive ? '+A' : '-A')
+ str << (additive ? '+A' : '-A')
else
- io.write(' ')
+ str << ' '
end
- io.write(spacer)
- io.write(trace ? '+T' : '-T')
- io.write("\n")
+ str << spacer
+ str << (caller_tracing ? '+T' : '-T')
+ str << "\n"
@appenders.each do |appender|
- io.write(indent_str)
- io.write('- ')
- io.write(appender.inspect)
- io.write("\n")
+ str << indent_str
+ str << '- '
+ str << appender.inspect
+ str << "\n"
end
- return io
+ return str
end
# :startdoc:
end # Logger
end # Logging