Permalink
Browse files

Made LoggerSupport module actually set up inherited loggers.

  • Loading branch information...
1 parent 41f70e8 commit 6758d73bc743de5f065b305a99745da1645be795 @nirvdrum nirvdrum committed Oct 7, 2010
Showing with 30 additions and 40 deletions.
  1. +2 −15 lib/lumber/logger_support.rb
  2. +16 −20 lib/lumber/lumber.rb
  3. +12 −5 test/logger_support_test.rb
@@ -5,22 +5,9 @@ module Lumber
module LoggerSupport
def self.included(receiver)
- receiver.class_eval do
- class_inheritable_accessor :logger
- last_logger = nil
- name_parts = self.name.split("::")
- name_parts.insert(0, Lumber::BASE_LOGGER)
- name_parts.each_with_index do |part, i|
- partial = name_parts[0..i].join("::")
- last_logger = Log4r::Logger[partial]
- if ! last_logger
- last_logger = Log4r::Logger.new(partial)
- end
- end
- self.logger = last_logger
- end
+ Lumber.setup_logger_hierarchy(receiver.name, "#{Lumber::BASE_LOGGER}::#{receiver.name}")
end
end
-
+
end
View
@@ -1,5 +1,7 @@
require "socket"
+require "active_support/core_ext/string/inflections"
+
begin
# rails(active_support) 2
require "active_support/core_ext/duplicable"
@@ -72,23 +74,18 @@ def self.init(opts = {})
def self.setup_logger_hierarchy(class_name, class_logger_fullname)
@@registered_loggers[class_name] = class_logger_fullname
- obj = nil
- names = class_name.split '::'
- names.each do |name|
- root ||= Object
- if root.const_defined?(name)
- obj = root.const_get(name)
- root = obj
- else
- obj = nil
- end
- end
+ begin
+ clazz = class_name.constantize
- if obj
- obj.class_eval do
- class_inheritable_accessor :logger
- self.logger = Log4r::Logger.new(class_logger_fullname)
+ if clazz.respond_to? :class_inheritable_accessor
+ clazz.class_eval do
+ class_inheritable_accessor :logger
+ self.logger = Log4r::Logger.new(class_logger_fullname)
+ end
end
+
+ rescue NameError
+ # The class hasn't been defined yet. No problem, we've registered the logger for when the class is created.
end
end
@@ -98,9 +95,9 @@ def self.setup_logger_hierarchy(class_name, class_logger_fullname)
# for classes as they get defined.
def self.register_inheritance_handler()
return if defined?(Object.inherited_with_lumber_log4r)
-
+
Object.class_eval do
-
+
class << self
def inherited_with_lumber_log4r(subclass)
@@ -115,7 +112,7 @@ def inherited_with_lumber_log4r(subclass)
Lumber.derive_lumber_logger(subclass)
end
end
-
+
alias_method_chain :inherited, :lumber_log4r
end
@@ -126,7 +123,6 @@ def inherited_with_lumber_log4r(subclass)
def self.add_lumber_logger(clazz, logger_name)
clazz.class_eval do
-
class_inheritable_accessor :logger
self.logger = Log4r::Logger.new(logger_name)
@@ -153,7 +149,7 @@ def self.derive_lumber_logger(clazz)
parent_logger_name = parent.logger.fullname rescue ''
parent_is_registered = @@registered_loggers.values.find {|v| parent_logger_name.index(v) == 0}
if parent_is_registered && parent.method_defined?(:logger=)
- clazz.logger = Log4r::Logger.new("#{parent_logger_name}::#{clazz.name}")
+ clazz.logger = Log4r::Logger.new("#{parent_logger_name}::#{clazz.name.nil? ? 'anonymous' : clazz.name.split('::').last}")
break
end
parent = parent.superclass
@@ -2,19 +2,26 @@
class LoggerSupportTest < Test::Unit::TestCase
+ def setup
+ root = "#{File.dirname(__FILE__)}/.."
+ Lumber.init(:root => root,
+ :env => 'test',
+ :config_file => "#{root}/generators/lumber/templates/log4r.yml",
+ :log_file => "/tmp/lumber-test.log")
+ end
+
def teardown
- Log4r::Logger::Repository.instance.loggers.clear
LoggerSupportTest.constants.grep(/^Foo/).each do |c|
LoggerSupportTest.send(:remove_const, c)
end
end
should "create logger for chain" do
class Foo; include Lumber::LoggerSupport; end
- assert_equal Log4r::Logger["rails::LoggerSupportTest::Foo"], Foo.logger
- assert_equal Log4r::Logger["rails::LoggerSupportTest"], Foo.logger.parent
- assert_equal Log4r::Logger["rails"], Foo.logger.parent.parent
- assert_equal Log4r::Logger.root, Foo.logger.parent.parent.parent
+ class Bar < Foo; end;
+ assert_equal Foo.logger, Log4r::Logger["rails::LoggerSupportTest::Foo"]
+ assert_equal Bar.logger, Log4r::Logger["rails::LoggerSupportTest::Foo::Bar"]
+ assert_equal Bar.logger.parent, Log4r::Logger["rails::LoggerSupportTest::Foo"]
end
should "have a logger instance accessible from an instance method" do

0 comments on commit 6758d73

Please sign in to comment.