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

Defined Under Namespace

Modules: InheritanceRegistry, LevelUtil, LoggerSupport 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.2.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#logger_concernObject

The logger concern (ActiveSupport::Concern) to include in each class.


32
33
34
# File 'lib/lumber/lumber.rb', line 32

def logger_concern
  @logger_concern
end

Instance Method Details

#find_or_create_logger(fullname) ⇒ Object


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lumber/lumber.rb', line 114

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_mdcObject

Helper to make it easier to log context through log4r.yml


166
167
168
169
170
# File 'lib/lumber/lumber.rb', line 166

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

  • :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


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
80
81
82
83
84
85
86
# File 'lib/lumber/lumber.rb', line 52

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 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))

  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


108
109
110
111
112
# File 'lib/lumber/lumber.rb', line 108

def logger_for(clazz)
  synchronize do
    Lumber.find_or_create_logger(logger_name(clazz))
  end
end

#logger_name(clazz) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lumber/lumber.rb', line 88

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


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/lumber/lumber.rb', line 149

def setup_logger_hierarchy(class_name, class_logger_fullname)
  Lumber::InheritanceRegistry.register_inheritance_handler

  begin
    clazz = class_name.constantize
    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