lib/lumber/lumber.rb in lumber-1.2.1 vs lib/lumber/lumber.rb in lumber-1.2.2
- old
+ new
@@ -26,10 +26,14 @@
BASE_LOGGER= 'rails'
extend MonitorMixin
extend self
+ # The logger concern (ActiveSupport::Concern) to include in each class.
+ attr_accessor :logger_concern
+ self.logger_concern = Lumber::LoggerSupport
+
# Initializes log4r system. Needs to happen in
# config/environment.rb before Rails::Initializer.run
#
# Options:
#
@@ -38,10 +42,11 @@
# * :config_file - defaults to <root>}/config/log4r.yml
# * :log_file - defaults to <root>}/log/<env>.log
# * :monitor_enabled - defaults to true
# * :monitor_interval - defaults to 60
# * :monitor_store - defaults to Rails.cache if defined, memory otherwise, see Lumber::LevelUtil::MemoryCacheProvider for interface
+ # * :logger_concern - the logger concern to include, defaults to Lumber::LoggerConcern
#
# All config options get passed through to the log4r
# configurator for use in defining outputters
#
def init(opts = {})
@@ -49,21 +54,23 @@
opts[:env] ||= RAILS_ENV if defined?(RAILS_ENV)
opts[:config_file] ||= "#{opts[:root]}/config/log4r.yml"
opts[:log_file] ||= "#{opts[:root]}/log/#{opts[:env]}.log"
opts[:monitor_enabled] = true unless opts[:monitor_enabled] == false
opts[:monitor_interval] ||= 60
-
+
raise "Lumber.init missing one of :root, :env" unless opts[:root] && opts[:env]
cfg = Log4r::YamlConfigurator
opts.each do |k, v|
cfg[k.to_s] = v.to_s
end
cfg['hostname'] = Socket.gethostname
cfg.load_yaml_file(opts[:config_file])
+ self.logger_concern = opts[:logger_concern] if opts[:logger_concern]
+
# Workaround for rails bug: http://dev.rubyonrails.org/ticket/8665
if defined?(RAILS_DEFAULT_LOGGER)
Object.send(:remove_const, :RAILS_DEFAULT_LOGGER)
end
Object.const_set('RAILS_DEFAULT_LOGGER', find_or_create_logger(BASE_LOGGER))
@@ -121,11 +128,11 @@
end
end
logger
end
end
-
+
# Makes :logger exist independently for subclasses and sets that logger
# to one that inherits from base_class for each subclass as it is created.
# This allows you to have a finer level of control over logging, for example,
# put just a single class, or hierarchy of classes, into debug log level
#
@@ -142,21 +149,21 @@
def setup_logger_hierarchy(class_name, class_logger_fullname)
Lumber::InheritanceRegistry.register_inheritance_handler
begin
clazz = class_name.constantize
- clazz.send(:include, Lumber::LoggerSupport)
+ clazz.send(:include, Lumber.logger_concern)
rescue NameError
# The class hasn't been defined yet. No problem, we register
# the logger for when the class is created below
end
# Don't register the class until after we see if it is already defined, that
# way LoggerSupport gets included _after_ class is defined and overrides logger
Lumber::InheritanceRegistry[class_name] = class_logger_fullname
end
- # Helper to make it easier to log context through log4r.yml
+ # Helper to make it easier to log context through log4r.yml
def format_mdc()
ctx = Log4r::MDC.get_context.collect {|k, v| k.to_s + "=" + v.to_s }.join(" ")
ctx.gsub!('%', '%%')
return ctx
end