Module: Lumber
- Extended by:
- Lumber, MonitorMixin
- Included in:
- Lumber
- Defined in:
- lib/lumber.rb,
lib/lumber/lumber.rb,
lib/lumber/server.rb,
lib/lumber/railtie.rb,
lib/lumber/version.rb,
lib/lumber/level_util.rb,
lib/lumber/json_formatter.rb,
lib/lumber/logger_support.rb,
lib/lumber/inheritance_registry.rb,
lib/lumber/prevent_rails_override.rb
Defined Under Namespace
Modules: InheritanceRegistry, LevelUtil, LoggerSupport, PreventRailsOverride Classes: JsonFormatter, Railtie, Server
Constant Summary collapse
- BASE_LOGGER =
name of top level logger (can’t be root as you can’t have outputters on root)
'rails'
- VERSION =
"1.1.1"
Instance Method Summary collapse
- #find_or_create_logger(fullname) ⇒ Object
-
#format_mdc ⇒ Object
Helper to make it easier to log context through log4r.yml.
-
#init(opts = {}) ⇒ Object
Initializes log4r system.
- #logger_for(clazz) ⇒ Object
- #logger_name(clazz) ⇒ Object
-
#setup_logger_hierarchy(class_name, class_logger_fullname) ⇒ Object
Makes :logger exist independently for subclasses and sets that logger to one that inherits from base_class for each subclass as it is created.
Instance Method Details
#find_or_create_logger(fullname) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/lumber/lumber.rb', line 107 def find_or_create_logger(fullname) synchronize do logger = Log4r::Logger[fullname] if logger.nil? # build the loggers from the lhs up to ensure the name based logger inheritance gets applied parts = fullname.split(Log4r::Log4rConfig::LoggerPathDelimiter) aggregate_name = nil parts.each do |part| if aggregate_name.nil? aggregate_name = part else aggregate_name = "#{aggregate_name}#{Log4r::Log4rConfig::LoggerPathDelimiter}#{part}" end logger = Log4r::Logger[aggregate_name] || Log4r::Logger.new(aggregate_name) end end logger end end |
#format_mdc ⇒ Object
Helper to make it easier to log context through log4r.yml
156 157 158 159 160 |
# File 'lib/lumber/lumber.rb', line 156 def format_mdc() ctx = Log4r::MDC.get_context.collect {|k, v| k.to_s + "=" + v.to_s }.join(" ") ctx.gsub!('%', '%%') return ctx end |
#init(opts = {}) ⇒ Object
Initializes log4r system. Needs to happen in config/environment.rb before Rails::Initializer.run
Options:
-
:root - defaults to RAILS_ROOT if defined
-
:env - defaults to RAILS_ENV if defined
-
: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
All config options get passed through to the log4r configurator for use in defining outputters
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lumber/lumber.rb', line 47 def init(opts = {}) opts[:root] ||= RAILS_ROOT.to_s if defined?(RAILS_ROOT) 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 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]) # 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)) Lumber::InheritanceRegistry.clear if opts[:monitor_store] # Setting to Rails.cache handled by a post initialize_cache rails initializer # since Rails.cache is not available when lumber is initialized LevelUtil.cache_provider = opts[:monitor_store] end LevelUtil.start_monitor(opts[:monitor_interval]) if opts[:monitor_enabled] end |
#logger_for(clazz) ⇒ Object
101 102 103 104 105 |
# File 'lib/lumber/lumber.rb', line 101 def logger_for(clazz) synchronize do Lumber.find_or_create_logger(logger_name(clazz)) end end |
#logger_name(clazz) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/lumber/lumber.rb', line 81 def logger_name(clazz) # Use the registered logger name if this class is in the registry logger_name = Lumber::InheritanceRegistry[clazz.name] if logger_name.nil? # if not, find the first registered logger name in the superclass chain, if any logger_name = Lumber::InheritanceRegistry.find_registered_logger(clazz.superclass) if logger_name.nil? # use name from clazz as we aren't inheriting logger_name = "#{Lumber::BASE_LOGGER}#{Log4r::Log4rConfig::LoggerPathDelimiter}#{clazz.name}" else # base name on inherited logger and clazz since we are inheriting # In log4r, a logger's parent is looked up from the name, and # Lumber.find_or_create_logger ensures that loggers are created for # all pieces of the name logger_name = "#{logger_name}#{Log4r::Log4rConfig::LoggerPathDelimiter}#{clazz.name}" end end logger_name end |
#setup_logger_hierarchy(class_name, class_logger_fullname) ⇒ Object
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
for example:
Lumber.setup_logger_hierarchy("ActiveRecord::Base", "rails::models")
causes all models that get created to have a log4r logger named “rails::models::<class_name>”. This class can individually be put into debug log mode in production (see log4r docs), and log output will include “<class_name>” on every log from this class so that you can tell where a log statement came from
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/lumber/lumber.rb', line 142 def setup_logger_hierarchy(class_name, class_logger_fullname) Lumber::InheritanceRegistry.register_inheritance_handler Lumber::InheritanceRegistry[class_name] = class_logger_fullname begin clazz = class_name.constantize clazz.send(:include, Lumber::LoggerSupport) rescue NameError # The class hasn't been defined yet. No problem, we've registered # the logger for when the class is created. end end |